This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_ex3.py
184 lines (132 loc) · 5.55 KB
/
project_ex3.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import itertools
import pprint
import sys
import time
from math import log
import nltk
import string
import numpy
from sklearn.feature_extraction.text import TfidfVectorizer, _document_frequency
from project_ex2 import getDataFromDir, calcMetrics, merge, mergeDict
numpy.set_printoptions(threshold=sys.maxsize)
def findBiggestGram(candidates):
max_gram = 0
for doc in candidates:
for cand in doc:
gram = len(cand.split())
max_gram = gram if gram > max_gram else max_gram
return max_gram
def getAllChunks(tagged_sents, grammar=r'KT: {(<JJ>* <NN.*>+ <IN>)? <JJ>* <NN.*>+}', deliver_as='list'):
punct = set(string.punctuation)
stop_words = set(nltk.corpus.stopwords.words('english'))
chunker = nltk.chunk.regexp.RegexpParser(grammar)
all_chunks = [nltk.chunk.tree2conlltags(chunker.parse(tagged_sent))
for tagged_sent in tagged_sents]
if deliver_as == 'sentences':
candidates = []
for sentence in all_chunks:
candidates.append(set([' '.join(word for word, pos, chunk in group).lower()
for key, group in itertools.groupby(sentence, lambda tpl: tpl[2] != 'O') if
key]))
return [[cand for cand in sent if cand not in stop_words
and len(cand.split()) <= 3
and not any([all(char in punct for char in w) for w in cand.split()])
and not all(char in punct for char in cand)]
for sent in candidates]
# ]
else:
candidates = set()
for sentence in all_chunks:
candidates = candidates | set([' '.join(word for word, pos, chunk in group).lower()
for key, group in itertools.groupby(sentence, lambda tpl: tpl[2] != 'O') if
key])
return [cand for cand in candidates
if cand not in stop_words and not all(char in punct for char in cand)]
# all_chunks is a list of lists. the inner lists are chunks for each sentence (so we don't have multi-sentence candidates)
def getTFIDFScore(dataset, mergetype='list'):
# extract candidates from each text in texts, either chunks or words
ds = list(itertools.chain.from_iterable(getAllCandidates(dataset, deliver_as='sentences')))
words = listOfTaggedToListOfWords(dataset)
vec = TfidfVectorizer(tokenizer=lambda i: i, lowercase=False)
vec.fit(ds)
vec.ngram_range = (1, findBiggestGram(ds))
X = vec.transform(words).toarray()
# print(X)
terms = vec.get_feature_names()
if mergetype == 'dict':
return mergeDict(dataset, terms, X)
else:
return merge(dataset, terms, X)
def getAllCandidates(dataset, deliver_as='list'):
return [getAllChunks(text, deliver_as=deliver_as) for text in dataset.values()]
def listOfTaggedToString(dataset):
documents = []
for d in dataset.values():
arr = []
for p in d:
arr.append([k[0].lower() for k in p])
documents.append(' '.join(itertools.chain.from_iterable(arr)))
return documents
def listOfTaggedToListOfWords(dataset):
documents = []
punct = set(string.punctuation)
for d in dataset.values():
doc_i = []
for ph in d:
for w_tag in ph:
word = w_tag[0].lower()
if not all(char in punct for char in word):
doc_i.append(word)
documents.append(doc_i)
return documents
def getBM25Score(dataset, k1=1.2, b=0.75, mergetype='list', min_df=2, cands=None):
if not cands:
cands = getAllCandidates(dataset, deliver_as='sentences')
ds = [list(itertools.chain.from_iterable(doc)) for doc in cands]
else:
ds = cands
words = listOfTaggedToListOfWords(dataset)
# documents = listOfTaggedToString(dataset)
# stopW = set(nltk.corpus.stopwords.words('english'))
vec_tf = TfidfVectorizer(tokenizer=lambda e: e, lowercase=False, use_idf=False)
vec_tf.fit(ds)
#
vec_tf.ngram_range = (1, findBiggestGram(ds))
# vec_tf.tokenizer = None
# vec_tf.stop_words = stopW
# vec_tf.min_df = 2
terms = vec_tf.get_feature_names()
X = vec_tf.transform(words)
tf_arr = X.toarray()
N = len(dataset)
avgDL = getAvgDL(ds)
DF_all = _document_frequency(X) # .sum()
score = []
for i, doc in enumerate(dataset.values()):
temp = []
dl = len(list(itertools.chain.from_iterable(doc)))
for j in range(len(terms)):
DF = DF_all[j]
tf = tf_arr[i][j]
bm25_idf = log((N - DF + 0.5) / (DF + 0.5), 10)
bm25_tf = (tf * (k1 + 1)) / (tf + k1 * (1 - b + (b * (dl / avgDL))))
bm25 = bm25_tf * (bm25_idf + 1.)
if DF >= min_df:
temp.append(bm25 * (len(terms[j]) / len(terms[j].split())))
else:
temp.append(0.)
score.append(temp)
if mergetype == 'dict':
return mergeDict(dataset, terms, score)
else:
return merge(dataset, terms, score)
def getAvgDL(all_d):
return numpy.average([len(d) for d in all_d])
def main():
train = getDataFromDir('ake-datasets-master/datasets/500N-KPCrowd/train', mode='list')
results = getBM25Score(train)
results_ = getTFIDFScore(train)
calcMetrics(results, 'ake-datasets-master/datasets/500N-KPCrowd/references/train.reader.stem.json')
calcMetrics(results_, 'ake-datasets-master/datasets/500N-KPCrowd/references/train.reader.stem.json')
if __name__ == '__main__':
main()