-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorize.py
66 lines (41 loc) · 1.48 KB
/
vectorize.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
64
65
66
import os, string, pickle, re, sys, time
import numpy as np
from collections import Counter
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.stem.porter import *
from nltk.corpus import stopwords
import textprocessor as tp
import pickler
import json
def main():
path = 'index.json'
with open(path, encoding="utf8") as json_file:
res = json.load(json_file)
docs = res['response']['docs']
url_list = []
doc_contents = []
for doc in docs:
if 'url' and 'content' in doc:
url_list.append(doc['url'])
doc_contents.append(doc['content'])
pickler.pickle_item(url_list, 'url_list')
pickler.pickle_item(doc_contents, 'doc_contents')
print('Text Processing . . .')
tokens = tp.tokenize_docs(doc_contents)
filtered_tokens = tp.remove_stopwords(tokens)
print('Done.')
print(len(filtered_tokens))
# stems = tp.tem_tokens(filtered_tokens)
print('Apply vectorizer . . ')
apply_vectorization(filtered_tokens)
print('Done.')
def apply_vectorization(tokens):
processed_docs = []
for doc in tokens:
processed_docs.append(' '.join(doc))
vectorizer = TfidfVectorizer(use_idf=True).fit(processed_docs)
doc_vectors = vectorizer.transform(processed_docs)
pickler.pickle_item(doc_vectors, 'docvec')
pickler.pickle_item(vectorizer, 'vectorizer')
if __name__ == "__main__":
main()