-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
44 lines (33 loc) · 1.08 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
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from nltk.tokenize import word_tokenize
nltk.download('stopwords')
nltk.download('punkt')
def load(filename):
with open(filename, "r") as f:
return f.read()
def clean(document):
stop_words = stopwords.words('english')
porter = PorterStemmer()
words = word_tokenize(document)
words = [token.lower() for token in words if token.isalpha()]
words = [token for token in words if token not in stop_words]
words = [porter.stem(token) for token in words]
return words
def save(lines, filename):
data = '\n'.join(lines)
with open(filename, "w") as f:
f.write(data)
def generate_sequences(words, save_to):
length = 50 + 1
sequences = list()
for i in range(length, len(words)):
seq = words[i - length:i]
line = ' '.join(seq)
sequences.append(line)
save(sequences, save_to)
def preprocess(input_document, output_document):
doc = load(input_document)
tokens = clean(doc)
generate_sequences(tokens, output_document)