-
Notifications
You must be signed in to change notification settings - Fork 53
/
word_split.py
132 lines (98 loc) · 3.55 KB
/
word_split.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
import dict_creat
import suffix_tree
split_mark = "#"
friendly_split_mark = ","
word_split_mark = "/"
input_file = open("preprocessing.txt", "r")
dict_file = open("dict.csv", "r")
long_word_punishment = 1000
def load():
return input_file.read()
def construct_tree(string):
tree = suffix_tree.SuffixTree(string)
tree.update_counter()
return tree
def load_dict():
dictionary = dict()
lines = dict_file.read().split("\n")
count_sum = 0
for line in lines:
if line:
cols = line.split(",")
dictionary[cols[0]] = int(cols[1])*float(cols[-1])
count_sum += int(cols[1])
return {key: value/count_sum for key, value in dictionary.items()}
def get_prob(tree, dictionary, sentence_length_count, string):
"""
Get the probability that string is a word.
:return: probability
"""
try:
current_word_prob = dictionary[string]
except KeyError:
current_word_count = tree.query(string).counter
current_word_prob = current_word_count / dict_creat.count_all_possibilities(sentence_length_count, len(string))
if current_word_count == 1:
for i in range(len(string)-1):
current_word_prob /= long_word_punishment
return current_word_prob
def split(tree, dictionary, sentence_length_count, string):
"""
Split string to words.
:return: words
"""
prob = [1] # Probability for the best split
last_word_index = [0] # Position for best split of last word
# Calculate probability
for i in range(1, len(string)+1):
max_prob = -1
max_prob_candidate = None
for candidate in range(max(0, i-4), i):
current_word = string[candidate: i]
current_prob = prob[candidate]*get_prob(tree, dictionary, sentence_length_count, current_word)
if current_prob > max_prob:
max_prob = current_prob
max_prob_candidate = candidate
# print("[%d:%d]%s/%s:%.10f" % (candidate, i, last_word, current_word, current_prob))
prob.append(max_prob)
last_word_index.append(max_prob_candidate)
# Get result
result = []
cursor = len(string)
while cursor != 0:
prev = last_word_index[cursor]
result.append(string[prev: cursor])
cursor = prev
return list(reversed(result))
def split_all(tree, dictionary, sentence_length_count, string, out_file, show_progress=True):
all_list = string.split(split_mark)
progress_update_interval = len(all_list)//20
for i, s in enumerate(all_list):
out_file.write(word_split_mark.join(split(tree, dictionary, sentence_length_count, s)))
out_file.write(friendly_split_mark)
if i % progress_update_interval == 1 and show_progress:
print("|", end="", flush=True)
if show_progress:
print()
out_file.write("\n\n")
def main():
print("Loading dictionary")
dictionary = load_dict()
print("Building tree")
string = load()
tree = construct_tree(string)
sentence_length_count = dict_creat.count_sentence_length(string)
print("Processing")
output_file = open("word_split.txt", "w")
split_all(tree, dictionary, sentence_length_count, string, output_file)
# def test_cursor():
# tree = construct_tree("banana$")
# tree.root.visualize()
# cursor = tree.query_cursor("an")
# cursor.node.visualize()
# cursor.current_node.visualize()
# cursor.move_front_forward()
# cursor.node.visualize()
# cursor.current_node.visualize()
if __name__ == "__main__":
main()