-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemuSTS.py
190 lines (170 loc) · 7.01 KB
/
TemuSTS.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
185
186
187
188
189
190
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 27 10:40:44 2020
@author: crodri
Version 2.0
"""
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
import spacy
modelo="es_core_news_md"
import wmd
nlp = spacy.load(modelo)#
import os,sys,pickle,numpy, time
from rapidfuzz import process
from optparse import OptionParser
def generateSentences(dirin):
if dirin[-1] != os.path.sep:
dirin = dirin+os.path.sep
filesin = os.listdir(dirin)
parrafos = {}
all_sents = 0
all_toks = 0
doc_number = len(filesin)
from collections import OrderedDict
for x in filesin:
print(dirin+x)
txt = open(dirin+x).read().strip()
txt = txt.replace("\n\n", "\n")
txt = txt.replace("\n\n", "\n")
txt = txt.replace("\n\n", "\n")
txt = txt.replace("\n\n", "\n")
y = nlp(txt)
number_sentences = len([x for x in y.sents])
number_toks = len([x for x in y])
all_sents = all_sents + number_sentences
all_toks = all_toks + number_toks
statsdoc = (doc_number,all_sents,all_toks)
w = 0
for s in y.sents:
parrafos[x+"_"+str(number_sentences)+"-"+str(w)] = s.text#s.lower_#" ".join([z.lower_ for z in s])
w += 1
return parrafos, statsdoc
def generateSourceSentences(dirin):
if dirin[-1] != os.path.sep:
dirin = dirin+os.path.sep
filesin = os.listdir(dirin)
parrafos = {}
all_sents = 0
all_toks = 0
doc_number = len(filesin)
#from collections import OrderedDict
for x in filesin:
print(dirin+x)
txt = open(dirin+x).read().strip()
txt = txt.replace("\n\n", "\n")
txt = txt.replace("\n\n", "\n")
txt = txt.replace("\n\n", "\n")
txt = txt.replace("\n\n", "\n")
y = nlp(txt)
number_sentences = len([x for x in y.sents])
number_toks = len([x for x in y])
all_sents = all_sents + number_sentences
all_toks = all_toks + number_toks
statsdoc = (doc_number,all_sents,all_toks)
w = 0
for s in y.sents:
parrafos[s.text] = x+"_"+str(number_sentences)+"-"+str(w)#s.lower_#" ".join([z.lower_ for z in s])
w += 1
return parrafos, statsdoc
#Jaccard similarity
def get_jaccard_sim(str1, str2):
a = set(str1.lower().split())
b = set(str2.lower().split())
c = a.intersection(b)
return float(len(c)) / (len(a) + len(b) - len(c))
def fuzzy(allterms,sentence,cutoff=93):
highest = process.extractOne(sentence,allterms,processor=None, score_cutoff=cutoff)
if highest:
return highest
else:
return None
def similitud(parrafos,sentence):
#from operator import itemgetter
sim = {}
for k in parrafos.keys():
source = parrafos[k]
s = get_jaccard_sim(source,sentence)
if s == 0.0:
pass
else:
sim[k] = s
sortedic = sorted(sim.items(), key=lambda x: x[1],reverse=True)
try:
return sortedic[0]
except IndexError:
return None
def compareAndWrite(parrafos1,parrafos2,umbral,method,redact=None):
similares = []
allterms = [x for x in parrafos1.keys()]
for eachone in parrafos2.keys():
sentence = parrafos2[eachone]
if method == 'fuzzy':
top = fuzzy(allterms,sentence,93)
elif method == 'jaccard':
top = similitud(parrafos1,sentence)
if top:
if top[-1] > umbral:
if redact:
similares.append([top[0],parrafos1[top[0]],top[-1],"--XXXXXXX--",eachone])
#similares.append([eachone,sentence,top[-1],"--XXXXXXX--",top[0]])
else:
similares.append([top[0],parrafos1[top[0]],top[-1],sentence,eachone])
return similares
def writeOut(targetdir,similares,statstarget,method,fileout):
import csv
fo= open(fileout,"w")
w = csv.writer(fo, dialect="excel", delimiter="\t")
w.writerow(["Target directory:"+targetdir,' # of Documents'+ str(statstarget[0]),"<=====>", '# of sentences '+str(statstarget[1]), '# of tokens '+str(statstarget[-1]),'Method:'+method])
w.writerow(['source_id', 'source_sentence', "score", 'target_sentence', 'target_id','method'])
w.writerows(similares)
fo.close()
def main(argv=None):
parser = OptionParser()
parser.add_option("-s", "--source", dest="source",
help="source directory")
parser.add_option("-o", "--fileout", dest="fileout", help="output file, tab-separated values extension (.tsv)",default="compared.tsv")
parser.add_option("-m", "--method", dest="method", help="Comparison method (jaccard, fuzzy [default]) Fuzzy is way faster",default="fuzzy")
parser.add_option("-t", "--target", dest="target", help="target directory")
parser.add_option("-u", "--umbral", dest="umbral", help="similarity threshold (default 93, for jaccard use 0.3 or higher)",type="float", default=93)
parser.add_option("-r", "--redact", dest="redact", help="do not write target sentences", default=None)
(options, args) = parser.parse_args(argv)
import time
t1 = time.time()
#print("*",type(options.cluster_num))
if options.source:
if options.method == 'fuzzy':
parrafos1, statssource = generateSourceSentences(options.source)
else:
parrafos1, statssource = generateSentences(options.source)
parrafos2, statstarget = generateSentences(options.target)
print("Initiating Comparison ...")
if options.redact:
similares = compareAndWrite(parrafos1,parrafos2,options.umbral,options.method,options.redact)
else:
similares = compareAndWrite(parrafos1,parrafos2,options.umbral,options.method)
if options.fileout:
writeOut(options.target,similares,statstarget,options.method,options.fileout)
else:
writeOut(options.target,similares,statstarget,options.method,"compared.tsv")
else:
print("Initiating Comparison ...")
if not options.target:
print("Need arguments. use python similituds_cc.py --help")
else:
print("No Source Directory ... Comparing against a reference corpus")
parrafos1 = pickle.load(open("corpus.bin","rb"))
parrafos2,statstarget = generateSentences(options.target)
if options.redact:
similares = compareAndWrite(parrafos1,parrafos2,options.umbral,options.method,options.redact)
else:
similares = compareAndWrite(parrafos1,parrafos2,options.umbral,options.method)
if options.fileout:
writeOut(options.target,similares,statstarget,options.method,options.fileout)
else:
writeOut(options.target,similares,statstarget,options.method,"compared.tsv")
t2 = time.time()
print("processed in "+str((t2-t1))+" seconds "+str((t2-t1)/60)+" minutes or "+str(((t2-t1)/60)/60)+" hours using "+options.method)
if __name__ == "__main__":
sys.exit(main())