-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
243 lines (236 loc) · 9.67 KB
/
utilities.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import _mysql
import MySQLdb as mdb
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import re
from MySQLdb.constants.ER import NEW_ABORTING_CONNECTION
class DB:
_conn = mdb.connect('localhost', 'root', '', 'ir')
_con = _mysql.connect('localhost', 'root', '', 'ir')
_cur = _conn.cursor()
def __init__(self):
try:
self.conn = mdb.connect('localhost', 'root', '', 'ir')
self.con = None
self.con = _mysql.connect('localhost', 'root', '', 'ir')
self.cur = self.conn.cursor()
except:
print "Cannot reach the Database..."
raise SystemExit
@staticmethod
def _execute(query):
DB._cur.execute(query)
rows = DB._cur.fetchall()
return rows
def Query(self,query):
return self.con.query(query)
@staticmethod
def _query(query):
return DB._con.query(query)
def Execute(self,query):
self.cur.execute(query)
rows = self.cur.fetchall()
return rows
def Insert(self,query):
x = self.conn.cursor()
try:
x.execute(query)
self.conn.commit()
except:
self.conn.rollback()
def Update(self,query):
x = self.conn.cursor()
try:
x.execute(query)
self.conn.commit()
except:
self.conn.rollback()
def Exec(self,query):
x = self.conn.cursor()
rows = ['Error']
try:
x.execute(query)
self.conn.commit()
rows = x.fetchall()
except:
self.conn.rollback()
finally:
return rows
def Close(self):
if self.con:
self.con.close()
class TextProcessor:
text_splitter = ','
_word_splitter = ' '
_text_splitter = ','
def Splitter(self):
return ','
def WordSplitter(self):
return ' '
def Clean(self,line):
line = line.replace("\n", '')
line = line.replace("\r", '')
line = line.replace("'", ' ')
line = line.replace('"', ' ')
line = line.replace('\t', ' ')
#line = line.replace(',', ' ')
line = line.replace('(', ' ')
line = line.replace(')', ' ')
line = line.replace(' 1 ', ' ')
line = line.replace(' 2 ', ' ')
line = line.replace(' 3 ', ' ')
line = line.replace(' 4 ', ' ')
line = line.replace(' 5 ', ' ')
line = line.replace(' 6 ', ' ')
line = line.replace(' 7 ', ' ')
line = line.replace(' 8 ', ' ')
line = line.replace(' 9 ', ' ')
line = line.replace(' 0 ', ' ')
line = line.replace('=', ' ')
line = line.replace('[', ' ')
line = line.replace(']', ' ')
line = line.replace(':', ' ')
line = line.replace('+', ' ')
line = line.replace(' -', ' ')
line = line.replace('- ', ' ')
return line
def CleanDoc(self,line):
line = line.replace("'", ' ')
line = line.replace('"', ' ')
#line = line.replace('(', ' ')
#line = line.replace(')', ' ')
return line
def CleanText(self,text):
bag = ["'",'"','(',')','%','/',':',';','.',',','[',']','}','{',' - ',' -','- ', ' x ' , ' x ']
for i in range(1000):
bag.append(str(i))
for item in bag:
text = text.replace(item, ' ')
text = self.CleanSpace(text)
return text
@staticmethod
def _clean_text(word_list):
bag = ['',' ',' ',"'",'"','(',')','%','/',':',';','.',',','[',']','}','{',' - ',' -','- ', ' x ' , ' x ']
for i in range(1000):
bag.append(str(i))
for item in bag:
if item in word_list:
word_list.remove(item)
return word_list
@staticmethod
def _normalize_word(word):
""" to lower
"""
word = word.lower()
bag = ['',' ',' ',"'",'"','(',')','%','/',':',';','.',',','[',']','}','{',' - ',' -','- ', ' x ']
for item in bag:
word = word.replace(item,'')
return word
def CleanPMID(self,PMID):
PMID = PMID.replace('\n','')
PMID = PMID.replace('\r','')
PMID = PMID.replace(' ','')
PMID = PMID.replace("PMID-",'')
return PMID
def CleanSpace(self,text):
while text.find(' ') != -1:
text = text.replace(" "," ")
return text
def RemoveStopWords(self, text):
'''
To remove Stopwords from a given document
'''
text = text.lower()
text = self.CleanText(text)
return text
stopwords = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','i','me','my','myself','we','our','ours','ourselves','you','your','yours','yourself','yourselves','he','him','his','himself','she','her','hers','herself','it','its','itself','they','them','their','theirs','themselves','what','which','who','whom','this','that','these','those','am','is','are','was','were','be','been','being','have','has','had','having','do','does','did','doing','a','an','the','and','but','if','or','because','as','until','while','of','at','by','for','with','about','against','between','into','through','during','before','after','above','below','to','from','up','down','in','out','on','off','over','under','again','further','then','once','here','there','when','where','why','how','all','any','both','each','few','more','most','other','some','such','no','nor','not','only','own','same','so','than','too','very','s','t','can','will','just','don','should','now']
u_list= text.split(self.WordSplitter())
u_list = [word for word in u_list if word not in stopwords]
return ' '.join(u_list)
@staticmethod
def _remove_stop_words(word_list):
'''
word_list is self explanatory
To remove Stopwords from a given document
'''
for word in word_list:
word = word.lower()
word_list = TextProcessor._clean_text(word_list)
stopwords = ['-','+','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','i','me','my','myself','we','our','ours','ourselves','you','your','yours','yourself','yourselves','he','him','his','himself','she','her','hers','herself','it','its','itself','they','them','their','theirs','themselves','what','which','who','whom','this','that','these','those','am','is','are','was','were','be','been','being','have','has','had','having','do','does','did','doing','a','an','the','and','but','if','or','because','as','until','while','of','at','by','for','with','about','against','between','into','through','during','before','after','above','below','to','from','up','down','in','out','on','off','over','under','again','further','then','once','here','there','when','where','why','how','all','any','both','each','few','more','most','other','some','such','no','nor','not','only','own','same','so','than','too','very','s','t','can','will','just','don','should','now']
u_list = [word for word in word_list if word not in stopwords]
punctuation = re.compile(r'[-+.?!,":;()|0-9]')
u_list = [punctuation.sub("", word) for word in u_list]
return u_list
def Tokenize(self,text):
return [t for t in word_tokenize(text)]
@staticmethod
def _tokenize(text):
results = [TextProcessor._normalize_word(t) for t in word_tokenize(text)]
new_result = []
for term in results:
if term != '':
new_result.append(term)
return new_result
def EliminateStopWords(self, wordList):
'''
Input: Tokenized word list
from nltk.tokenize import word_tokenize
word_list = [t for t in word_tokenize(doc)]
'''
filtered_words = [w for w in wordList if not w in stopwords.words('english')]
punctuation = re.compile(r'[-+.?!,":;()|0-9]')
filtered_words = [punctuation.sub("", word) for word in filtered_words]
return filtered_words
def Stem(self,wordList):
#LStem = LancasterStemmer()
PStem = PorterStemmer()
wordSet = []
for word in wordList:
temp = PStem.stem(word)
wordSet.append(temp)
return wordSet
@staticmethod
def _stem(wordList):
#LStem = LancasterStemmer()
PStem = PorterStemmer()
wordSet = []
for word in wordList:
temp = PStem.stem(word)
wordSet.append(temp)
return wordSet
@staticmethod
def CleanList(the_list, val):
while val in the_list:
the_list.remove(val)
def remove_values_from_list(self,the_list, val):
while val in the_list:
the_list.remove(val)
class Operation:
def unique(self,a):
""" return the list with duplicate elements removed """
return list(set(a))
def intersect(self,a, b):
""" return the intersection of two lists """
return list(set(a) & set(b))
def union(self,a, b):
""" return the union of two lists """
return list(set(a) | set(b))
@staticmethod
def _union(a, b):
""" return the union of two lists """
return list(set(a) | set(b))
class Parameter:
predecessors_depth = 4
def GetDocNumber(self):
return 500
def GetDocNumberForLocalContext(self):
return 5
class GlobalVariables():
remove_stop_words = True
stem = False
global_context_activated = False
ontology_based_IR_activated = False
add_concepts_multiple_times = False
adding_concept_times = 5
result_title = "Exp_T_F_F_F"