-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create and query words_with_frequency_and_translation_and_ipa in sqli…
…te3 use Python3
- Loading branch information
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
dictionary/sqlite/create_english_db_with_translation_and_ipa.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
30 changes: 30 additions & 0 deletions
30
dictionary/sqlite/query_english_db_with_translation_and_ipa.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
Binary file added
BIN
+15.7 MB
dictionary/sqlite/words_with_frequency_and_translation_and_ipa.sqlite3
Binary file not shown.