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
|
# File 'lib/bimyou_segmenter/aozora_model.rb', line 13
def self.segment(text, options = {})
if (text.nil? || text.size == 0)
return []
end
white_space = options[:white_space].nil? ? false : options[:white_space]
symbol = options[:symbol].nil? ? true : options[:symbol]
wakachi = []
text_chars = text.chars.to_a
chars = [BOFS, text_chars, EOFS].flatten
chars_bigram = 0.upto(chars.size - 2).map do |i|
chars[i] + chars[i+1]
end
chars_trigram = 0.upto(chars.size - 3).map do |i|
chars[i] + chars[i + 1] + chars[i + 2]
end
char_types = [BOCS, char_types(text_chars), EOCS].flatten
char_types_bigram = 0.upto(char_types.size - 2).map do |i|
char_types[i] + char_types[i + 1]
end
char_types_trigram = 0.upto(char_types.size - 3).map do |i|
char_types[i] + char_types[i + 1] + char_types[i + 2]
end
last_seg = 1
word = chars[LEFT_TOKENS]
(LEFT_TOKENS + 1).upto(chars.size - (RIGHT_TOKENS + 1)).each do |i|
k0 = i - LEFT_TOKENS
k1 = k0 + 1
k2 = k0 + 2
k3 = k0 + 3
if (char_types[k1] == 'C' || char_types[k2] == 'C')
dot = 1.0 else
dot = BIAS
state = last_seg.to_s
if (w = C10_W[state + char_types[k0]])
dot += w
end
if (w = C11_W[state + char_types[k1]])
dot += w
end
if (w = C12_W[state + char_types[k2]])
dot += w
end
if (w = C13_W[state + char_types[k3]])
dot += w
end
if (w = C20_W[state + char_types_bigram[k0]])
dot += w
end
if (w = C21_W[state + char_types_bigram[k1]])
dot += w
end
if (w = C22_W[state + char_types_bigram[k2]])
dot += w
end
if (w = C30_W[state + char_types_trigram[k0]])
dot += w
end
if (w = C31_W[state + char_types_trigram[k1]])
dot += w
end
if (w = C40_W[state + char_types_trigram[k0] + char_types[k3]])
dot += w
end
state = last_seg.to_s + ":"
if (w = F10_W[chars[k0]])
dot += w
end
if (w = F10_W[state + chars[k0]])
dot += w
end
if (w = F11_W[chars[k1]])
dot += w
end
if (w = F11_W[state + chars[k1]])
dot += w
end
if (w = F12_W[chars[k2]])
dot += w
end
if (w = F12_W[state + chars[k2]])
dot += w
end
if (w = F13_W[chars[k3]])
dot += w
end
if (w = F13_W[state + chars[k3]])
dot += w
end
if (w = F20_W[chars_bigram[k0]])
dot += w
end
if (w = F20_W[state + chars_bigram[k0]])
dot += w
end
if (w = F21_W[chars_bigram[k1]])
dot += w
end
if (w = F21_W[state + chars_bigram[k1]])
dot += w
end
if (w = F22_W[chars_bigram[k2]])
dot += w
end
if (w = F22_W[state + chars_bigram[k2]])
dot += w
end
if (w = F30_W[chars_trigram[k0]])
dot += w
end
if (w = F30_W[state + chars_trigram[k0]])
dot += w
end
if (w = F31_W[chars_trigram[k1]])
dot += w
end
if (w = F31_W[state + chars_trigram[k1]])
dot += w
end
fourgram = chars_trigram[k0] + chars[k3]
if (w = F40_W[fourgram])
dot += w
end
if (w = F40_W[state + fourgram])
dot += w
end
end
if (dot > 0.0)
wakachi << word
word = chars[i]
last_seg = 1
else
word += chars[i]
if (last_seg < MAX_HIST)
last_seg += 1
end
end
end
wakachi << word
unless (white_space)
wakachi = wakachi.reject{|v| v.match(WHITE_SPACE) }
end
unless (symbol)
wakachi = wakachi.reject{|v| v.match(SYMBOL) }
end
wakachi
end
|