forked from transreductionist/mjwise-rkr-gst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory_2_canned_strings.py
45 lines (35 loc) · 1.2 KB
/
factory_2_canned_strings.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
from test_2_strings import str1
from test_2_strings import str2
from rkr_gst_helpers.rkr_gst import rkr_gst
from rkr_gst_helpers.manage_tokens import ManageTokens
import math
import pickle
def factory():
article_1 = [str.strip() for str in str1.split(' ') if str != '']
article_2 = [str.strip() for str in str2.split(' ') if str != '']
mininum_match_length = 3 # default: 3
initial_search_size = 8 # default: 20
t_tokens = article_1
p_tokens = article_2
mantok_t = ManageTokens(t_tokens)
mantok_p = ManageTokens(p_tokens)
rkr_gst(mantok_t, mantok_p, mininum_match_length, initial_search_size)
tiles = {}
i = 0
for token in mantok_t.is_marked:
if token:
i += 1
elif not token and i > 0:
if i in tiles:
tiles[i] += 1
else:
tiles[i] = 1
i = 0
normalization = (mantok_t.length_of_tokens + mantok_p.length_of_tokens)/2.0
similarity = 0
for tile_length, total in tiles.items():
similarity += total*tile_length*math.log(tile_length + 1)
normalized_similarity = similarity/normalization
print normalized_similarity
if __name__ == '__main__':
factory()