diff --git a/.gitignore b/.gitignore index 8af46f4..01f50d3 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,6 @@ Pods/Target Support Files/MDCDamerauLevenshtein/MDCDamerauLevenshtein.debug.xcco Pods/Target Support Files/MDCDamerauLevenshtein/MDCDamerauLevenshtein.release.xcconfig Pods/Pods.xcodeproj/xcuserdata/yuweidong.xcuserdatad/ *.xcuserdatad + +*.sqlite3-wal +*.sqlite3-shm diff --git a/dictionary/sqlite/create_english_db_with_translation_and_ipa.py b/dictionary/sqlite/create_english_db_with_translation_and_ipa.py new file mode 100644 index 0000000..8792bb1 --- /dev/null +++ b/dictionary/sqlite/create_english_db_with_translation_and_ipa.py @@ -0,0 +1,28 @@ +import sqlite3 +import json + +with open('../words_with_frequency_and_translation_and_ipa.json', encoding='utf-8') as f: + data = json.load(f) + +conn = sqlite3.connect('words_with_frequency_and_translation_and_ipa.sqlite3') +c = conn.cursor() + +# Create table +c.execute(''' +CREATE TABLE IF NOT EXISTS words ( + word TEXT PRIMARY KEY, + frequency INT, + translation TEXT, + ipa TEXT +) +''') + +# Insert data +for word, details in data.items(): + c.execute(''' + INSERT INTO words (word, frequency, translation, ipa) VALUES (?, ?, ?, ?) + ''', (word, details['frequency'], json.dumps(details['translation']), details['ipa'])) + +# Commit the changes and close the connection +conn.commit() +conn.close() diff --git a/dictionary/sqlite/query_english_db_with_translation_and_ipa.py b/dictionary/sqlite/query_english_db_with_translation_and_ipa.py new file mode 100644 index 0000000..6ae2cf4 --- /dev/null +++ b/dictionary/sqlite/query_english_db_with_translation_and_ipa.py @@ -0,0 +1,30 @@ +import sqlite3 +import json + + +def query_words_by_prefix(prefix): + with sqlite3.connect('words_with_frequency_and_translation_and_ipa.sqlite3') as conn: + c = conn.cursor() + # The LIKE operator is case-insensitive in SQLite by default + c.execute('SELECT * FROM words WHERE word LIKE ? ORDER BY frequency DESC limit 30', (prefix + '%',)) + results = c.fetchall() + # conn.close() + + words_details = [] + for result in results: + words_details.append({ + "word": result[0], + "translation": '|'.join(json.loads(result[2])), + "ipa": result[3] + }) + return words_details + +# Example usage: Find words starting with "pre" +prefix_match_words = query_words_by_prefix("TEst") +for word_detail in prefix_match_words: + print(111, word_detail) + +prefix_match_words2 = query_words_by_prefix("good") +for word_detail in prefix_match_words2: + print(222, word_detail) + diff --git a/dictionary/sqlite/words_with_frequency_and_translation_and_ipa.sqlite3 b/dictionary/sqlite/words_with_frequency_and_translation_and_ipa.sqlite3 new file mode 100644 index 0000000..567ca95 Binary files /dev/null and b/dictionary/sqlite/words_with_frequency_and_translation_and_ipa.sqlite3 differ