|
|
|
|
|
|
|
|
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": |
|
|
os.system(f"afplay {audio_file} 2>/dev/null || open {audio_file}") |
|
|
elif os.name == "nt": |
|
|
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() |
|
|
|