-
Notifications
You must be signed in to change notification settings - Fork 0
/
ta_distance_project.py
163 lines (145 loc) · 5.21 KB
/
ta_distance_project.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from nltk.stem.snowball import SnowballStemmer
import numpy as np
import nltk
import re
from databaseConnection import Database
import unicodedata
import string
def remove_accents(data):
return ''.join(x for x in unicodedata.normalize('NFKD', data) if x in string.ascii_letters).lower()
def tokenize_and_stem(text):
stemmer = SnowballStemmer('english')
tokens= [word for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)]
filtered_tokens = []
for token in tokens:
token = remove_accents(token)
if re.search('[a-zA-Z]', token):
if(len(token)>1 and len(token)<100):
filtered_tokens.append(token)
stems=[]
stems = [stemmer.stem(t) for t in filtered_tokens]
return stems
def tokenize_only(text):
tokens = [word.lower() for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)]
filtered_tokens = []
for token in tokens:
if re.search('[a-zA-Z]', token):
filtered_tokens.append(token)
return filtered_tokens
def count_main_list(dataset):
token_list = []
count_list = []
for set in dataset:
for temp_tuple in set:
token = temp_tuple[0]
tf = temp_tuple[1]
try:
tokenIndex = token_list.index(token)
count_list[tokenIndex][1]+= 1
count_list[tokenIndex][2] += tf
except ValueError:
token_list.append(token)
count_list.append([token, 1, tf])
return count_list
def count_words(string_list):
import string
#string_list = s.split(" ")
#string_list = sorted(s.split(" "), key=str)
#print(string_list)
#exit(0)
wordList = []
countList = []
for word in string_list:
#print(word + "| ")
word = word.strip()
try:
wordIndex = wordList.index(word)
countList[wordIndex][1]+= 1
except ValueError:
wordList.append(word)
countList.append([word, 1])
return countList
def normalizeString(content):
content = content.replace("'", '')
content = content.replace('"', '')
content = content.replace('/', ' ')
content = content.replace('\\', ' ')
content = content.replace(',', ' ')
content = content.replace('μ', 'µ')
content = content.replace('ß', 'β')
content = content.replace('(', ' ')
content = content.replace(')', ' ')
content = content.replace('%', ' ')
content = content.replace(':', ' ')
content = content.replace('.', ' ')
content = content.replace('∙', ' ')
content = content.replace('˚', '°')
content = content.replace('~', ' ')
content = content.replace('=', ' ')
content = content.replace('≤', ' ')
content = content.replace('−', '-')
content = content.replace('α', 'a')
content = content.replace('δ', 'b')
return content
data = Database()
stopwords = nltk.corpus.stopwords.words('english')
synopses = data.getAbstractByTherapeuticArea('Family Medicine & Internal Medicine')
abstract_list=[]
TotalCountList = []
for i in synopses:
abstract_id = i[0]
abstract_list.append(str(abstract_id))
content = normalizeString(i[1])
allwords_stemmed = tokenize_and_stem(content)
allwords_stemmed = [word for word in allwords_stemmed if word not in stopwords]
count_list = count_words(allwords_stemmed)
#print(tuple_count)
TotalCountList.append(count_list)
#calculate result for every 100 abstracts and insert result to database.
if (len(abstract_list) == 100):
result = count_main_list(TotalCountList)
TotalCountList = []
#join abstract_ids to a comma breaken string
abstract_str = ",".join(abstract_list)
abstract_list = []
token_list = []
df_value_list = []
tf_value_list = []
for temp_tuple in result:
token_list.append(temp_tuple[0].strip())
df_value_list.append(str(temp_tuple[1]))
tf_value_list.append(str(temp_tuple[2]))
#join tf and df lists to comma breaked strings
token_str = ",".join(token_list)
df_str = ",".join(df_value_list)
tf_str = ",".join(tf_value_list)
print(abstract_str)
print(token_str)
print(df_str)
print(tf_str)
data.insertTokenV1(abstract_str, token_str, df_str, tf_str)
print("inserted %s tokens into database" % str(len(token_list)))
if(len(abstract_list) > 0 ):
result = count_main_list(TotalCountList)
TotalCountList = []
# join abstract_ids to a comma breaken string
abstract_str = ",".join(abstract_list)
abstract_list = []
token_list = []
df_value_list = []
tf_value_list = []
for temp_tuple in result:
token_list.append(temp_tuple[0].strip())
df_value_list.append(str(temp_tuple[1]))
tf_value_list.append(str(temp_tuple[2]))
# join tf and df lists to comma breaked strings
token_str = ",".join(token_list)
df_str = ",".join(df_value_list)
tf_str = ",".join(tf_value_list)
print(abstract_str)
print(token_str)
print(df_str)
print(tf_str)
data.insertTokenV1(abstract_str, token_str, df_str, tf_str)
print("inserted %s tokens into database" % str(len(token_list)))
exit(0)