-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
63 lines (56 loc) · 2.02 KB
/
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
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
import json
import sys
from nltk.stem.porter import PorterStemmer
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
__author__= 'KD'
def json_writer(data, fname):
"""
Write multiple json files
Args:
data: list(dict): list of dictionaries to be written as json
fname: str: output file name
"""
with open(fname, mode="w") as fp:
for line in data:
json.dump(line, fp)
fp.write("\n")
def json_reader(fname):
"""
Read multiple json files
Args:
fname: str: input file
Returns:
generator: iterator over documents
"""
for line in open(fname, mode="r"):
yield json.loads(line)
def _stem(doc, p_stemmer, en_stop, return_tokens):
tokens = word_tokenize(doc.lower())
stopped_tokens = filter(lambda token: token not in en_stop, tokens)
stemmed_tokens = map(lambda token: p_stemmer.stem(token), stopped_tokens)
if not return_tokens:
return ' '.join(stemmed_tokens)
return list(stemmed_tokens)
def getStemmedDocuments(docs, return_tokens=True):
"""
Args:
docs: str/list(str): document or list of documents that need to be processed
return_tokens: bool: return a re-joined string or tokens
Returns:
str/list(str): processed document or list of processed documents
Example:
new_text = "It is important to by very pythonly while you are pythoning with python. \
All pythoners have pythoned poorly at least once."
print(getStemmedDocuments(new_text))
Reference: https://pythonprogramming.net/stemming-nltk-tutorial/
"""
en_stop = set(stopwords.words('english'))
p_stemmer = PorterStemmer()
if isinstance(docs, list):
output_docs = []
for item in docs:
output_docs.append(_stem(item, p_stemmer, en_stop, return_tokens))
return output_docs
else:
return _stem(docs, p_stemmer, en_stop, return_tokens)