Skip to content

Commit 0c07932

Browse files
committed
create and query words_with_frequency_and_translation_and_ipa in sqlite3 use Python3
1 parent eddcbd1 commit 0c07932

4 files changed

+61
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ Pods/Target Support Files/MDCDamerauLevenshtein/MDCDamerauLevenshtein.debug.xcco
4747
Pods/Target Support Files/MDCDamerauLevenshtein/MDCDamerauLevenshtein.release.xcconfig
4848
Pods/Pods.xcodeproj/xcuserdata/yuweidong.xcuserdatad/
4949
*.xcuserdatad
50+
51+
*.sqlite3-wal
52+
*.sqlite3-shm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sqlite3
2+
import json
3+
4+
with open('../words_with_frequency_and_translation_and_ipa.json', encoding='utf-8') as f:
5+
data = json.load(f)
6+
7+
conn = sqlite3.connect('words_with_frequency_and_translation_and_ipa.sqlite3')
8+
c = conn.cursor()
9+
10+
# Create table
11+
c.execute('''
12+
CREATE TABLE IF NOT EXISTS words (
13+
word TEXT PRIMARY KEY,
14+
frequency INT,
15+
translation TEXT,
16+
ipa TEXT
17+
)
18+
''')
19+
20+
# Insert data
21+
for word, details in data.items():
22+
c.execute('''
23+
INSERT INTO words (word, frequency, translation, ipa) VALUES (?, ?, ?, ?)
24+
''', (word, details['frequency'], json.dumps(details['translation']), details['ipa']))
25+
26+
# Commit the changes and close the connection
27+
conn.commit()
28+
conn.close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sqlite3
2+
import json
3+
4+
5+
def query_words_by_prefix(prefix):
6+
with sqlite3.connect('words_with_frequency_and_translation_and_ipa.sqlite3') as conn:
7+
c = conn.cursor()
8+
# The LIKE operator is case-insensitive in SQLite by default
9+
c.execute('SELECT * FROM words WHERE word LIKE ? ORDER BY frequency DESC limit 30', (prefix + '%',))
10+
results = c.fetchall()
11+
# conn.close()
12+
13+
words_details = []
14+
for result in results:
15+
words_details.append({
16+
"word": result[0],
17+
"translation": '|'.join(json.loads(result[2])),
18+
"ipa": result[3]
19+
})
20+
return words_details
21+
22+
# Example usage: Find words starting with "pre"
23+
prefix_match_words = query_words_by_prefix("TEst")
24+
for word_detail in prefix_match_words:
25+
print(111, word_detail)
26+
27+
prefix_match_words2 = query_words_by_prefix("good")
28+
for word_detail in prefix_match_words2:
29+
print(222, word_detail)
30+
Binary file not shown.

0 commit comments

Comments
 (0)