-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringP.py
More file actions
68 lines (50 loc) · 1.59 KB
/
stringP.py
File metadata and controls
68 lines (50 loc) · 1.59 KB
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
import nltk
nltk.download('punkt')
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk import word_tokenize
import re
# removes common english words (listed in nltk.corpus.stopwords) from corpus
def removeCommon(str):
# creates the set of common words
sw = set(stopwords.words('english'))
# splits the string in to words.
l = str.split()
res = []
# if a word is not in set of common words, addes it to the result list
for x in l:
if not x in sw:
res.append(x)
# puts together the result list as a string and returns it
return ' '.join(res)
# boxes together several functions:
# removeCommon defined above
# removing any character other than letters, digits and spaces
# reduces multiple spaces to one.
# lower case every letter
def pipeLine(str):
pipeline = [removeCommon,
lambda s: re.sub('[^\\w\\s]', '', s),
lambda s: re.sub( '\\s+', ' ',s).strip(),
lambda s: s.lower(),
]
# applies all functions on input
for f in pipeline:
str = f(str)
return str
# removes stem parts of words, i.e., ing, s ...
def stem_tokens(tokens):
stemmer = PorterStemmer()
stemmed = []
for item in tokens:
stemmed.append(stemmer.stem(item))
return stemmed
# for a string returns a list of tokens
def tokenize(text):
# runs the pipeLine on the string
nt = pipeLine(text)
# tokenizes the string
tokens = nltk.word_tokenize(nt)
# removes stem parts of tokens
stems = stem_tokens(tokens)
return stems