-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpreprocess.py
More file actions
70 lines (53 loc) · 1.81 KB
/
Copy pathpreprocess.py
File metadata and controls
70 lines (53 loc) · 1.81 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
69
70
import re
import os
import nltk.data
from nltk.corpus import stopwords
from nltk.stem import SnowballStemmer
def readData(filename,lang='spanish'):
'''
Arguments: Name of file, language (default: Spanish)
Returns: List of List of sentence words, unfiltered sentence list and
length of doc
Returns the [ s1[w1,w2,w3..], s2[w1,w2,w3..] ...], [s1,s2,...]
and length of doc.
'''
sentWordList = []
sentList = []
lenDoc = 0
with open(filename,'r') as fp:
for line in fp:
line = line.strip()
sentWordList, sentList, lenDoc = cleanLine(line,sentWordList,sentList,lenDoc,lang)
return sentWordList,sentList,lenDoc
def cleanLine(line,finalList,sentList,lenDoc,lang):
'''
Arguments: each line, existing sentence word list, existing unfiltered sentence
list, length of the document and language.
Returns: appended sentence word list, appended unfiltered sentence list, and
length of the document seen so far.
Performs preprocessing task. Removes stopwords, punctuations and numeric values
'''
try:
tokenizer = nltk.data.load('tokenizers/punkt/'+ lang + '.pickle')
except:
print "Language not Supported"
return None
tempList = tokenizer.tokenize(line.decode('utf-8'))
for sent in tempList:
sentList.append(sent)
line = line.decode('utf-8').lower()
notNum = re.compile(r'[0-9]+')
decimal = re.compile(r'\d+.\d+')
stemmer = SnowballStemmer('spanish')
line = decimal.sub('',line)
line = notNum.sub('',line)
words = line.split(' ')
impWords = filter(lambda x: x not in stopwords.words(lang), words)
stemmedWords = [stemmer.stem(word) for word in impWords]
line = ' '.join(stemmedWords)
sentWordList = tokenizer.tokenize(line)
for sent in sentWordList:
words = re.findall(r'\w+', sent,flags = re.UNICODE | re.LOCALE)
finalList.append(words)
lenDoc += len(words)
return finalList, sentList, lenDoc