-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorization.py
More file actions
executable file
·60 lines (52 loc) · 2.02 KB
/
Copy pathvectorization.py
File metadata and controls
executable file
·60 lines (52 loc) · 2.02 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
'''
Implement function to vectorize the given document using VectorSpace Model
Name: Wei Xu
Uniquename: weixu '''
import preprocess
import math
import numpy as np
def processDoc(doc_string):
token_doc = preprocess.stemWords(preprocess.removeStopwords(preprocess.tokenizeText(doc_string)))
return token_doc
''' Function to count the occurance frequency of the tokens in each document
This function will be called by function tfIdfVectorize
Return a document-term matrix, in which each element indicates the occurence count of
the column token in the row document '''
def countTokenFreq(raw_documents):
# preprocess the doc content to get tokens
token_document = []
token_voc = []
for contentDoc in raw_documents:
token_Doc = processDoc(contentDoc)
token_document.append(token_Doc)
token_voc.extend(list(set(token_Doc)))
token_voc = list(set(token_voc))
doc_num = len(token_document)
token_num = len(token_voc)
doc_term_mat = np.zeros((doc_num, token_num))
for i in range(0, doc_num):
for j in range(0, token_num):
doc_term_mat[i][j] = token_document[i].count(token_voc[j])
#print doc_term_mat[i][j]
return token_voc, doc_term_mat
''' Function to compute the document_term matrix based on tf-idf weighting
Each row is one document
Each column is one token
Return the wieghting document-term matrix '''
def tfidfVectorize(raw_documents):
token_voc, doc_term_mat = countTokenFreq(raw_documents)
doc_num = doc_term_mat.shape[0]
token_num = doc_term_mat.shape[1]
idf_arr = np.zeros(token_num)
for i in range(0, token_num ):
df = 0
for j in range(0,doc_num):
if doc_term_mat[j,i] != 0:
df += 1
idf_arr[i] = math.log10(doc_num*1.0/df)
weight_doc_term = np.zeros((doc_num, token_num))
for m in range(0, token_num):
for n in range(0, doc_num):
tf = doc_term_mat[j, i]
weight_doc_term[j, i] = tf*idf_arr[m]
return weight_doc_term, token_voc, idf_arr