-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_word_popularity.py
executable file
·53 lines (43 loc) · 1.45 KB
/
calculate_word_popularity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
import sqlite3
import re
def main():
conn = sqlite3.connect('latin-db.sqlite3')
cur = conn.cursor()
cur.execute("SELECT id FROM words;")
word_ids = cur.fetchall()
count_map = {}
for id in word_ids:
count_map[id[0]] = 0
f = open('aeneid_counts.txt', 'r')
regex = re.compile(r"\s*(\d+)\s*(\w+)");
for line in f:
match = regex.match(line)
if not match:
next
count = int(match.group(1))
word = match.group(2)
cur = conn.cursor()
cur.execute(
"SELECT DISTINCT word_id FROM word_forms WHERE norm_value = ?;",
(word,))
word_ids = cur.fetchall();
print ((word, len(word_ids)))
for id in word_ids:
count_map[id[0]] += count
cur.execute("DROP TABLE IF EXISTS word_popularity");
cur.execute("CREATE TABLE word_popularity(word_id INTEGER REFERENCES word(ID), rank INTEGER)");
id_count_list = count_map.items()
sorted_id_count_list = reversed(sorted(id_count_list, key=lambda x: x[1]))
ranked_ids = []
current_count = 10000000
rank = 0
for (i, (id,count)) in enumerate(sorted_id_count_list):
if (count < current_count):
current_count = count
rank = i+1
ranked_ids.append((id,rank))
conn.executemany("INSERT INTO word_popularity(word_id, rank) VALUES (?, ?)",
ranked_ids)
conn.commit()
main()