Skip to content

Commit

Permalink
create and query words_with_frequency_and_translation_and_ipa in sqli…
Browse files Browse the repository at this point in the history
…te3 use Python3
  • Loading branch information
dongyuwei committed Apr 6, 2024
1 parent eddcbd1 commit 0c07932
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions dictionary/sqlite/create_english_db_with_translation_and_ipa.py
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 dictionary/sqlite/query_english_db_with_translation_and_ipa.py
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 not shown.

0 comments on commit 0c07932

Please sign in to comment.