-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessing.py
231 lines (204 loc) · 10.3 KB
/
preprocessing.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
#!/usr/bin/python3
import re
from nltk.tokenize import sent_tokenize
#Function gets user input for multi-paragraph length for either '-specificity' or '-result' flags
def multParCheckValue(flag):
#'multi-paragraph' was entered for '-specificity' flag
if flag == 'specificity':
multParLength = input("Please enter multi-paragraph length for '-specificity': ")
#'multi-paragraph' was entered for '-result' flag
else:
multParLength = input("Please enter multi-paragraph length for '-results': ")
#Ensuring input is a valid integer
while (multParLength.isdigit() == False):
multParLength = input('Please enter a valid integer: ')
print('')
return int(multParLength)
class Preprocessing():
def __init__(self, specificity = None, fileName = None):
self.specificity = specificity
self.fileName = fileName
self.fileContents = None
self.tokenizedList = []
#Function reads contents of .txt files
def readFile(self):
with open(self.fileName, 'r') as openFile:
self.fileContents = openFile.read()
#Function tokenizes text either by sentence, paragraph, or user defined multi-paragraph length
def tokenizeText(self):
#Using nltk's sent_tokenize to tokenize by sentence
#Automatically handles white space and newline characters
if self.specificity == 0:
self.tokenizedList = sent_tokenize(self.fileContents)
#Tokenizing by paragraph
elif self.specificity == 1:
#Spliting based on newline characters
splitList = self.fileContents.split('\n')
for term in splitList:
#If multiple newline characters between paragraphs, extra ones are discarded
if (term != ' ') and (term != ''):
#Stripping paragraphs incase there is a space after last period
self.tokenizedList.append(term.strip())
#Tokenizing by multi-paragraph
else:
#Splitlines will group newline character immediately after text with the text
#Immediately sequential newline characters after this are tokenized separately
#ie: 'Hello\n\n\n'.splitlines(keepends=True) -> ['Hello\n', '\n', '\n']
paragraphList = self.fileContents.splitlines(keepends=True)
tempParStr = ''
paragraphCount = 0
for text in paragraphList:
#If on text, which ends with a newline character, this is a paragraph
if (text[-1] == '\n') & (text[0] != '\n'):
paragraphCount += 1
#Immediately sequential newline characters are added to string, does not affect paragraph count
tempParStr += text
#If number of paragraphs in tempParStr is equal to user defined multi-paragraph length, add to token list
if paragraphCount == self.specificity:
self.tokenizedList.append(tempParStr.strip())
tempParStr = ''
paragraphCount = 0 #Resetting counter
#If finished iterating, but last multi-paragraph chunk is below user defined number, add it to token list
if tempParStr != '':
#If just extra newline characters, do nothing
if tempParStr.strip() != '':
self.tokenizedList.append(tempParStr.strip())
#Function normalizes tokens by making them all lowercase
def normalizeTokens(self):
for count, token in enumerate(self.tokenizedList):
self.tokenizedList[count] = token.lower()
#Function checks parenthesis and makes sure # of '(' match # of ')'
def checkQueryParenthesis(self, queryString):
begParenCount = 0
endParenCount = 0
for character in queryString:
#Adds to '(' count
if character == '(':
begParenCount += 1
#Adds to ')' count
elif character == ')':
endParenCount += 1
if begParenCount == endParenCount:
return True
else:
return False
#Function converts boolean algebra query in infix notation to prefix notation
#ie: '(not(cat) and not(dog)) or bird' -> ['or', 'and', 'not', 'cat', 'not', 'dog', 'bird']
def convertToPrefixFromInfix(self, queryString):
#Priority list, 'not' has a higher priority than 'or' & 'and'
priorityMap = {'and': 1, 'or': 1, 'not': 2, '(': 0, ')':0}
operators = ['and', 'or', 'not', '(', ')']
prefix = []
stack = []
#Spliting by both spaces and '(' and ')'
#Removing empty strings and spaces from list
splitQuery = list(filter(lambda x: (x != ' ') & (x != ''), re.split('(\W)', queryString)))
reversedQuery = list(reversed(splitQuery))
#Infix to Prefix algorithm
for term in reversedQuery:
#Operand
if term not in operators:
prefix.append(term)
# ')'
elif term == ')':
stack.append(term)
# '('
elif term == '(':
x = stack[-1]
while x != ')':
prefix.append(stack.pop())
x = stack[-1]
stack.pop()
#Operator
else:
if len(stack) == 0:
stack.append(term)
else:
while True:
if len(stack) == 0:
stack.append(term)
break
#Current operator has >= priority than last operator on stack
if priorityMap[term] >= priorityMap[stack[-1]]:
stack.append(term)
break
#Current operator has < priority than last operator on stack
else:
prefix.append(stack.pop())
#Add any left over operator symbols from stack to prefix list
while len(stack) > 0:
prefix.append(stack.pop())
return list(reversed(prefix))
#Function gets boolean logic query from user for boolean search
def parseQuery(self):
while True:
#User can be shown examples, get help, or enter query
print("Please enter your boolean logic query\n"
"input 'example' to see previous examples\n"
"input 'help' for syntax clarification:")
#So case of operators does not matter
#All search is based on lowercase inputs
toParse = input().lower()
if toParse == 'example':
print("\n------------------------------------------\n"
"Example 1: NOT(cat AND dog) OR bird\n"
"Example 2: google AND NOT(apple) AND 95050\n"
"------------------------------------------\n")
elif toParse == 'help':
print("\n----------------------------------------------------------------------\n"
"Allowed operators: NOT, AND, OR \n"
"Operators may be capitalized or lowercase\n\n"
"Parenthesis and nested parenthesis are allowed\n"
"Words are numbers are allowed\n\n"
"Words, even proper nouns, with a space must be joined with an operator\n"
"Example: San AND Francisco\n"
"----------------------------------------------------------------------\n")
#User has inputed query
else:
#Checking parenthesis to make sure same # of '(' as ')'
result = self.checkQueryParenthesis(toParse)
if result == False:
print('\nQUERY IS NOT VALID, mismatching parenthesis\n')
else:
#Converting query from infix string to prefix list
prefixQS = self.convertToPrefixFromInfix(toParse)
return prefixQS
#Function gets free-form query from user for free-form search
def inputQuery(self, pastLimit):
limit = pastLimit
while True:
#User can be shown examples, get help, enter query, or modify result limit
print("Please enter your free-form search query\n"
"input 'example' to see previous examples\n"
"input 'help' for syntax clarification\n"
"input 'limit=<integer>' to limit number of responses\n"
"input 'limit=all' to show all responses:")
#All search is based on lowercase inputs
inputResponse = input().lower()
if inputResponse == 'example':
print("\n------------------------------------------\n"
"Example 1: What is the capital of California?\n"
"Example 2: Did the economy grow in 2020\n"
"------------------------------------------\n")
elif inputResponse == 'help':
print("\n----------------------------------------------------------------------\n"
"Any free-form text is allowed\n"
"Text may be uppercase or lowercase\n"
"Numbers, punctuation, and symbols are allowed\n"
"You cannot search exclusively by symbol or punctuation\n"
"----------------------------------------------------------------------\n")
#Query or result limit is being modified
else:
limitCheck = inputResponse.split('=')
#Seeing if result limit is being modified or not, if so, it must be 'all' or a digit
if (len(limitCheck) > 1) & (limitCheck[0] == 'limit'):
#If 'all' is present, no limit is set
if limitCheck[1] == 'all':
limit = None
#Must be a valid digit, if not, user input is ignored
elif limitCheck[1].isdigit() == True:
limit = int(limitCheck[1])
print('')
#Query was entered
else:
return inputResponse, limit