-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
61 lines (45 loc) · 1.75 KB
/
Copy pathpreprocess.py
File metadata and controls
61 lines (45 loc) · 1.75 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
from nltk.corpus import stopwords
from nltk.tokenize import TweetTokenizer
from nltk.stem import WordNetLemmatizer
from pattern.en import lemma
import numpy as np
from collections import Counter
from gensim import corpora
import glob
def read_data(file):
with open(file, 'r') as f:
content = f.read()
return content
def pre_process(documents):
# lower case
documents = [doc.lower() for doc in documents]
# remove stop words
stop_words = set(stopwords.words('english'))
symbols = {'-', '(', ')', ';', "'", '.', '_', '/', ':', ',', ');', '‘', '’', '"', '“', '”', '—'}
stop_words = stop_words | symbols
tokenizer = TweetTokenizer()
# lemmatization
wnl = WordNetLemmatizer()
filter_documents = [' '.join([wnl.lemmatize(w) for w in tokenizer.tokenize(doc) if w not in stop_words])
for doc in documents]
# filter_documents = [' '.join([lemma(w) for w in tokenizer.tokenize(doc) if w not in stop_words])
# for doc in documents]
texts = [[word for word in d.split(' ')] for d in filter_documents]
d = corpora.Dictionary(texts)
words = [w for w in d.token2id.keys()]
return filter_documents, words
def word_doc_matrix(vocabulary, documents):
d = dict(zip(vocabulary, range(len(vocabulary))))
X = np.zeros([len(vocabulary), len(documents)])
for j, doc in enumerate(documents):
items_count = Counter(doc.split(' '))
for k, v in items_count.items():
X[d[k], j] = v
return X
if __name__ == '__main__':
files = glob.glob('./text/*.txt')
documents = []
for f in files:
documents.append(read_data(f))
documents, words = pre_process(documents)
X = word_doc_matrix(words, documents)