-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
33 lines (29 loc) · 901 Bytes
/
utils.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
import inflect
import re
tokenDict = {}
def tokens(str):
if(tokenDict.has_key(str)):
return tokenDict.get(str)
toks = re.findall(r"[\w]+|[.,!?;^()'/%*]", str)
tokenDict[str] = toks
return toks
stemmed = {}
#singularizes words, this version is good for units
inf = inflect.engine()
exclusions = []
exclusions.extend(("s", "S", "a", "A", "i", "I", "", "ME"))
def stemAll(str):
if(stemmed.has_key(str)):
return stemmed.get(str)
toks = tokens(str)
stems = []
for tok in toks:
single = inf.singular_noun(tok)
if(single != False) and not (single in exclusions): #and (not (single in exclusions)) and (tok.lower() != "s"):
stems.append(single)
else:
#if not (tok in exclusions):
stems.append(tok)
#else: stems.append("second")
stemmed[str] = stems
return stems