-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
75 lines (55 loc) · 1.85 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from utils import read, write
def count_occurrences(tweets):
words = {}
bigrams = {}
for tweet in tweets:
text = tweet['text'].split(' ')
last_word = None
for word in text:
words[word] = words.get(word, 0) + 1
if last_word:
bigram = last_word + ' ' + word
bigrams[bigram] = bigrams.get(bigram, 0) + 1
last_word = word
return words, bigrams
def create_index(words, bigrams):
word_index = {}
bigram_index = {}
i = 0
for word, count in words.items():
if count > 1:
word_index[word] = i
i += 1
i = 0
for bigram, count in bigrams.items():
if count > 1:
bigram_index[bigram] = i
i += 1
# words = {k: v for k, v in words.items() if word_index.get(k)}
# bigrams = {k: v for k, v in bigrams.items() if bigram_index.get(k)}
# write('data/count.json', {"words": words, "bigrams": bigrams})
return {"words": word_index, "bigrams": bigram_index}
def print_common_occurrences_counts(words, bigrams):
print(len(words))
for i in list(range(1, 10)) + [20, 40, 80, 160, 320, 640, 1280]:
common_words = 0
for word, count in words.items():
if count > i:
common_words += 1
print(i, common_words)
print(len(bigrams))
for i in list(range(1, 10)) + [20, 40, 80, 160, 320, 640, 1280]:
common_words = 0
for word, count in bigrams.items():
if count > i:
common_words += 1
print(i, common_words)
return
def main():
train = read('data/train.json')
tweets, bigrams = count_occurrences(train)
# print_common_occurrences_counts(tweets, bigrams)
index = create_index(tweets, bigrams)
write('data/index.json', index)
if __name__ == '__main__':
main()