-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_hmm.py
executable file
·64 lines (55 loc) · 1.68 KB
/
make_hmm.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
#!/usr/bin/env python3
import sys
import traceback
import os
import pickle
import json
__dirname = os.path.dirname(__file__)
projectPath = os.path.normpath(os.path.join(__dirname))
PROB_START_P = "prob_start.p"
PROB_TRANS_P = "prob_trans.p"
PROB_EMIT_P = "prob_emit.p"
def getProb(filename):
probStartPath = os.path.join(
projectPath, "./jieba-tw/jieba/finalseg", filename)
with (open(probStartPath, "rb")) as openfile:
return pickle.load(openfile, encoding="utf-8")
def main(argv):
MIN_PROB = "-3.14e+100"
startProb = getProb(PROB_START_P)
transProb = getProb(PROB_TRANS_P)
emitProb = getProb(PROB_EMIT_P)
print("#prob_start")
print(
"%s %s %s %s"
% (startProb["B"], startProb["E"], startProb["M"], startProb["S"])
)
print("#prob_trans 4x4 matrix")
print(
"%s %s %s %s"
% (MIN_PROB, transProb["B"]["E"], transProb["B"]["M"], MIN_PROB)
)
print(
"%s %s %s %s"
% (transProb["E"]["B"], MIN_PROB, MIN_PROB, transProb["E"]["S"])
)
print(
"%s %s %s %s"
% (MIN_PROB, transProb["M"]["E"], transProb["M"]["M"], MIN_PROB)
)
print(
"%s %s %s %s"
% (transProb["S"]["B"], MIN_PROB, MIN_PROB, transProb["S"]["S"])
)
print("#prob_emit 4 lines")
def emitItem(kv):
return "%s:%f" % (kv[0], kv[1])
print(",".join(map(emitItem, emitProb["B"].items())))
print(",".join(map(emitItem, emitProb["E"].items())))
print(",".join(map(emitItem, emitProb["M"].items())))
print(",".join(map(emitItem, emitProb["S"].items())))
# print(emitProb)
# print(transProb)
# print(emitProb)
if __name__ == '__main__':
main(sys.argv[1:])