-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConceptItem.py
80 lines (65 loc) · 1.8 KB
/
ConceptItem.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from CSVFile import CSVFile
import nltk
# import Embedding as emb
import numpy as np
#Part-of-speech tagger
def tagPOS(sentence):
text = nltk.word_tokenize(sentence)
# print text
print (nltk.pos_tag(text))
tagWeight = {'NN':2, 'NNP':2, 'VBG':1}
class ConceptItem(object):
"""docstring for ConceptItem"""
def __init__(self, arg):
super(ConceptItem, self).__init__()
self.concept = arg[0]
self.description = arg[1]
self.category = arg[2]
self.found = True
self.lowemb = []
def conceptName(self):
return self.concept
def getCategory(self):
return self.category
def tagBag(self, sentence):
sentence = sentence.replace("/"," ")
tagBag = tagWeight = {'NN':[], 'NNP':[], 'VBG':[]}
text = nltk.word_tokenize(sentence)
posList = nltk.pos_tag(text)
for pos in posList:
try:
tagBag[pos[1]].append(pos[0])
except Exception as e:
pass
return tagBag
def conceptBag(self):
return self.tagBag(self.concept)
# def itemVector(self):
# itemVec = np.zeros(128)
# weightSum = 0.0
# bag = self.tagBag(self.concept)
# for tag in bag:
# for word in bag[tag]:
# try:
# itemVec = np.add(itemVec,emb.wordVec(word.lower())) * tagWeight[tag]
# weightSum = weightSum + tagWeight[tag]
# except Exception as e:
# print (word + ' not found')
# if weightSum==0:
# self.found = False
# return np.zeros(128)
# return (itemVec/weightSum)
def isFound(self):
return self.found
def setLowEmb(self,lowemb):
self.lowemb = lowemb
def lowEmb(self):
return self.lowemb
def fullConcept(self):
return (self.concept.replace('-',' ') + " " +self.description.replace('-',' ')).split()
if __name__ == '__main__':
file = CSVFile()
conceptlist = file.getContent()[0:5]
for item in conceptlist:
conceptItem = ConceptItem(item)
print (conceptItem.fullConcept())