#!/usr/bin/env python3
A = {}
# A = [[0]*4 for i in range(4)]
# A_B = 0
# A_M = 1
# A_E = 2
# A_S = 3
A_B = 'B'
A_M = 'M'
A_E = 'E'
A_S = 'S'

Pi={'B':0,'E':0,'S':0,'M':0}
B = {'B':{},'E':{},'S':{},'M':{}}
B_B = {}
B_E = {}
B_M = {}
B_S = {}

SUM_A = 0
SUM_Pi = 0
SUM_B = {'B':0,'E':0,'S':0,'M':0}

#输入"词/t"
#输出('词','t')
def ParseWord(word):
    
    return word.split('/')

def addtodict(d,w):
    if w not in d:
        d[w] = 1
    else:
        d[w] += 1
#判断词语片是否为句子分隔符如[，/w ]
def validWord(s):
    if s == '\n':
        return False
    return True
    if s[-2:] != '/w':
        return True
    else:
        return False



f = open('1998-01-2003版-带音.txt',encoding='utf-8')

line = f.readline()

while line:
    lastStatus = ''
    words = line.split('  ')
    #去掉第一个日期内容
    for i in range(1,len(words)):

        w = ParseWord(words[i].strip(' '))
        if not validWord(w[0]):
            continue

        if len(w[0]) == 1:
            #单字词S
            addtodict(Pi,'S')
            SUM_Pi += 1
            addtodict(B['S'],w[0])
            SUM_B['S'] += 1
            if lastStatus != '':
                #A[lastStatus][A_S] += 1
                addtodict(A,lastStatus + 'S' )
                SUM_A += 1
            lastStatus = A_S
        else:
            #多字词开头是B
            addtodict(Pi,'B')
            SUM_Pi += 1
            addtodict(B['B'],w[0][0])
            SUM_B['B'] += 1
            if lastStatus != '':
                #A[lastStatus][A_B] += 1
                addtodict(A,lastStatus + 'B' )
                SUM_A += 1
            lastStatus = A_B

            #中间是M
            for ch in w[0][1:-1]:
                addtodict(B['M'],ch)
                SUM_B['M'] += 1
                #A[lastStatus][A_M] += 1
                addtodict(A,lastStatus + 'M' )
                SUM_A += 1
                lastStatus = A_M
            #结尾是E
            addtodict(B['E'],w[0][-1])
            SUM_B['E'] += 1
            #A[lastStatus][A_E] += 1
            addtodict(A,lastStatus + 'E' )
            SUM_A += 1
            lastStatus = A_E


    line = f.readline()

f.close()

out = open('HMM_train_result.py','w',encoding='utf-8')
out.write('#!/usr/bin/env python3\nA = ')
print(A,file=out)
out.write('B = ')
print(B,file=out)
out.write('Pi = ')
print(Pi,file=out)

out.write('SUM_A = ')
print(SUM_A,file=out)
out.write('SUM_B = ')
print(SUM_B,file=out)
out.write('SUM_Pi = ')
print(SUM_Pi,file=out)