-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathduplicate_lines.py
More file actions
59 lines (51 loc) · 1.73 KB
/
duplicate_lines.py
File metadata and controls
59 lines (51 loc) · 1.73 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
import sys
import Levenshtein
import networkx as nx
def get_dupes(words):
"""
input: words json
output: array of arrays of arrays (jk, it has the sentences, too)
(duplicate lines -> start/stop word indices of each sentence)
"""
sentence_start = 0
tmp_sentence = ""
sentences = []
sentence_idx = []
for i, word in enumerate(words):
if any(word["word"].endswith(x) for x in ['.', '?', '!', '"', ':']):
tmp_sentence += word["word"]
sentences.append(tmp_sentence)
tmp_sentence = ''
sentence_idx.append((sentence_start, i))
sentence_start = i + 1
elif word["alignedWord"] == "sp" or word["alignedWord"] == "{BR}":
if i == sentence_start:
sentence_start += 1
continue
else:
tmp_sentence += word["word"] + ' '
# create a graph to track similar sentences
sim = nx.Graph()
for i, s in enumerate(sentences):
sim.add_node((i, s, sentence_idx[i]))
for i, s in enumerate(sentences):
for j, t in enumerate(sentences):
dist = Levenshtein.distance(s, t)
if i != j and dist < max(len(s), len(t)) / 2.0:
sim.add_edge((i, s, sentence_idx[i]), (j, t, sentence_idx[j]))
out = []
ccs = nx.connected_components(sim)
for i in range(len(ccs)):
ccs[i] = sorted(ccs[i], key=lambda x: x[0])
ccs = sorted(ccs, key=lambda x: x[0][0])
for cc in ccs:
out.append([[elt[2], elt[1]] for elt in cc])
return out
if __name__ == '__main__':
try:
import ujson as json
except:
import json
with open(sys.argv[1], 'r') as f:
txt = json.load(f)["words"]
print get_dupes(txt)