-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeWordrank.py
More file actions
54 lines (40 loc) · 1.46 KB
/
Copy pathmakeWordrank.py
File metadata and controls
54 lines (40 loc) · 1.46 KB
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
import re
from collections import Counter
from os import listdir
from os.path import isfile, join
from nltk.corpus import reuters
myPath = 'data/email/training/'
onlyFiles = [f for f in listdir(myPath + 'untagged/') if isfile(join(myPath + 'untagged/', f))]
def words(text): return re.findall(r"(?i)(?<=[\s\-\/'(])(\w*[a-z]{2,}\w*)(?=[.,'):\-\/\s])", text.lower())
def get_prob(frequencies):
probability = {}
total = sum(frequencies.values())
for word in frequencies.most_common():
probability[word[0]] = word[1] / total
return probability
all_head = []
for email in onlyFiles:
full_email = open(myPath + 'untagged/' + str(email)).read()
if 'Topic:' in full_email:
for word in words(open(myPath + 'untagged/' + str(email)).read().split('Topic:')[1].split(':')[0]):
all_head.append(word)
head_freq = Counter(all_head)
head_prob = get_prob(head_freq)
comp_words = []
for word in reuters.words():
comp_words.append(word.lower())
comp_freq = Counter(comp_words)
comp_prob = get_prob(comp_freq)
delta = {}
for word in head_prob:
if word in comp_prob:
delta[word] = head_prob[word] - comp_prob[word]
else:
delta[word] = head_prob[word]
sorted_delta = sorted(delta, key=delta.get, reverse=True)
file_content = ""
for word in sorted_delta:
file_content = file_content + word + '\n'
with open('data/ontology/wordrank.txt', 'w') as head_file:
head_file.write(file_content[:-1])
print('fin makeWordrank.py')