-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorization.py
More file actions
47 lines (36 loc) · 979 Bytes
/
Vectorization.py
File metadata and controls
47 lines (36 loc) · 979 Bytes
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
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
def MakeCorpus(*args):
corp = set()
for sent in args:
for tok in word_tokenize(sent):
corp.add(tok)
return list(corp)
def PresenceAbsenceVectorization(*args):
corp = MakeCorpus(*args)
vecs = []
print(corp)
for sent in args:
vec = [0]*len(corp)
for tok in word_tokenize(sent):
if tok in corp:
vec[corp.index(tok)] = 1
vecs.append(vec)
return vecs
def CountVectorization(*args):
corpus = []
for sent in args:
corpus.append(sent)
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
return X.toarray()
def TFIDFVectorization(*args):
corpus = []
for sent in args:
corpus.append(sent)
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
return X.toarray()