-
Notifications
You must be signed in to change notification settings - Fork 24
/
stem.py
27 lines (24 loc) · 844 Bytes
/
stem.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
import csv
import json
import re
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
for i in range(46):
i += 1
print (i)
# get file and loop through each line
lines = open('data/songs/' + str(i) + '.txt', 'r')
data = []
for line in lines:
line = line.replace('\n', '')
stemmedLine = []
# for each line, loop through all words and pass it through stemmer
for word in re.sub('[^a-zA-Z]+', ' ', line).split():
stemmed = stemmer.stem(word)
stemmedLine.append(stemmed)
# then push it back into lines
stemmedLine = " ".join(stemmedLine)
data.append([line, stemmedLine])
# after everything is done, write it to file
with open('processed/' + str(i) + '.json', 'w') as outfile:
json.dump(data, outfile)