File size: 2,419 Bytes
af61bfc
 
ee3f5c5
af61bfc
 
 
 
ee3f5c5
 
 
 
 
 
 
af61bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee3f5c5
af61bfc
ee3f5c5
af61bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee3f5c5
af61bfc
 
ee3f5c5
af61bfc
 
 
ee3f5c5
af61bfc
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3

import json
import pandas as pd
import os


def load_dataset(jsonl_file="vietnamese_ipa_dataset.jsonl"):
    records = []
    with open(jsonl_file, "r", encoding="utf-8") as f:
        for line in f:
            records.append(json.loads(line))

    df = pd.DataFrame(records)
    return df


def search_word(word, df):
    result = df[df["word"].str.contains(word, case=False, na=False)]
    if not result.empty:
        return result.to_dict("records")
    return None


def play_audio(audio_file):
    if os.path.exists(audio_file):
        if os.name == "posix":  # Unix/Linux/Mac
            os.system(f"afplay {audio_file} 2>/dev/null || open {audio_file}")
        elif os.name == "nt":  # Windows
            os.system(f"start {audio_file}")
        return True
    return False


def main():
    print("Vietnamese IPA Dataset Example Usage")
    print("=" * 50)

    df = load_dataset()

    print(f"\nDataset loaded: {len(df)} words")
    print("\nFirst 10 entries:")
    print("-" * 50)

    for idx, row in df.head(10).iterrows():
        audio_path = row["audio"]["path"] if isinstance(row["audio"], dict) else row["audio"]
        print(
            f"{idx + 1}. {row['word']:<15} IPA: {row['ipa']:<25} Audio: {audio_path}"
        )

    print("\n" + "=" * 50)
    print("\nInteractive Mode (type 'quit' to exit)")
    print("-" * 50)

    while True:
        query = input("\nEnter a Vietnamese word to search (or 'quit'): ").strip()

        if query.lower() == "quit":
            break

        results = search_word(query, df)

        if results:
            print(f"\nFound {len(results)} result(s):")
            for i, result in enumerate(results, 1):
                audio_path = result["audio"]["path"] if isinstance(result["audio"], dict) else result["audio"]
                print(f"\n  {i}. Word: {result['word']}")
                print(f"     IPA: {result['ipa']}")
                print(f"     Audio: {audio_path}")

                play_choice = input("     Play audio? (y/n): ").strip().lower()
                if play_choice == "y":
                    if play_audio(audio_path):
                        print("     ✓ Playing audio...")
                    else:
                        print("     ✗ Audio file not found")
        else:
            print(f"No results found for '{query}'")

    print("\nGoodbye!")


if __name__ == "__main__":
    main()