-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextProcessing.py
50 lines (41 loc) · 1.13 KB
/
TextProcessing.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
#removes all punctuation excluding spaces
def removePuctuation(word):
for i in range(len(word)):
if not word[i].isalnum() and not word[i].isspace():
word = word[:i] + " " + word[i+1:]
return word
#returns a list of ngrams of sizes 1-5 with and without punctuation
def makeNGrams(query):
if query is None: return []
query += " " + removePuctuation(query)
words = query.split()
output = []
for n in range(1,6):
for i in range(len(words)-n+1):
output.append(words[i:i+n])
return output
#testing
def test():
print("test 1:")
test = ''
print(makeNGrams(test))
print("test 2:")
test = ' '
print(makeNGrams(test))
print("test 3:")
print(makeNGrams(None))
print("test 4:")
test = 'A'
print(makeNGrams(test))
print("test 5:")
test = 'A B C D E'
print(makeNGrams(test))
print("test 6:")
test = 'A B C D E F G H I'
print(makeNGrams(test))
print("test 7:")
test = 'A? B! C@ D$ E%'
print(makeNGrams(test))
print("test 8:")
test = 'How to change the world? in 20.5 days'
print(makeNGrams(test))