-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfeatures.py
53 lines (44 loc) · 1.29 KB
/
features.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
from khmernltk.utils.data import seg_kcc
def word_to_features(sent, i):
word = sent[i]
# split word into Khmer character clusters
kccs = seg_kcc(word)
features = {
"bias": 1.0,
"len(kccs)": len(kccs),
"word.isdigit()": word.isdigit(),
"word": word,
}
for k in range(len(kccs)):
features.update({f"kccs[{k}]": kccs[k]})
if i > 0:
word1 = sent[i - 1]
kccs1 = seg_kcc(word1)
features.update(
{
"-1:word.isdigit()": word1.isdigit(),
"-1:word": word1,
"-1len(kccs)": len(kccs1),
}
)
for k in range(len(kccs1)):
features.update({f"-1kccs1[{k}]": kccs1[k]})
else:
features["BOS"] = True
if i < len(sent) - 1:
word1 = sent[i + 1]
kccs1 = seg_kcc(word1)
features.update(
{
"+1:word.isdigit()": word1.isdigit(),
"+1:word": word1,
"+1len(kccs)": len(kccs1),
}
)
for k in range(len(kccs1)):
features.update({f"+1kccs1[{k}]": kccs1[k]})
else:
features["EOS"] = True
return features
def create_word_features(sent):
return [word_to_features(sent, i) for i in range(len(sent))]