堕落天使369 2021-12-16 23:29 采纳率: 100%
浏览 35
已结题

HMM词性标注提示HMM没有定义

问题遇到的现象和发生背景

HMM怎么会没有定义

问题相关代码,请勿粘贴截图

class HMM(object):
def init(self):
pass

def try_load_model(self,trained):
    pass

def train(self,path):
    pass

def viterbi(self,text,states,start_p,trans_p,emit_p):
    pass

def cut(self,text):
    pass

def __int__(self):
    import os

    self.model_file = './data/hmm_model.pkl'

    self.state_list = ['B','M','E','S']

    self.load_para = False

def try_load_model(self,trained):
    if trained:
        import pickle
        with open(self.model_file,'rb') as f:
            self.A_dic = pickle.load(f)
            self.B_dic = pickle.load(f)
            self.Pi_dic = pickle.load(f)
            self.load_para = True
    else:
        self.A_dic = {}
        self.B_dic = {}
        self.Pi_dic = {}
        self.load_para = False

def train(self,path):

    self.try_load_model(False)

    Count_dic = {}

    def init_parameters():
        for state in self.state_list:
            self.A_dic[state] = {s:0.0 for s in self.state_list}
            self.Pi_dic[state] = 0.0
            self.B_dic[state] = {}

            Count_dic[state] = 0

    def makeLabel(text):
        out_text = []
        if len(text) == 1:
            out_text.append('S')
        else:
            out_text += ['B'] + ['M'] * (len(text) -2) + ['E']
        return out_text

    init_parameters()
    line_num = -1

    words = set()
    with open(path,encoding='utf8') as f:
        for line in f:
            line_num += 1

            line = line.strip()
            if not line:
                continue

            word_list = [i for i in line if i != '']
            words |= set(word_list)

            linelist = line.spilt()

            line_state = []
            for w in linelist:
                line_state.extend(makeLabel(w))

            assert  len(word_list) == len(line_state)

            for k, v in enumerate(line_state):
                Count_dic[v] += 1
                if k == 0:
                    self.Pi_dic[v] += 1
                else:
                    self.A_dic[line_state[k - 1]][v] += 1
                    self.B_dic[line_state[k]][word_list[k]] = \
                    self.B_dic[line_state[k]].get(word_list[k],0) + 1.0
        self.Pi_dic = {k: v * 1.0 / line_num for k, v in self.Pi_dic.itmes()}
        self.A_dic = {k: {k1: v1 / Count_dic[k] for k1, v1 in v.items()}for k,v in self.A_dic.item()}
        self.B_dic = {k: {k1: (v1 + 1) / Count_dic[k] for k1, v1 in v.items()} for k, v in self.B_dic.item()}

        import pickle
        with open(self.model_file,'wb') as f:
            pickle.dump(self.A_dic, f)
            pickle.dump(self.B_dic, f)
            pickle.dump(self.Pi_dic, f)
        return self

def viterbi(self, text, states, start_p, trans_p, emit_p):
    V = [{}]
    path = {}
    for y in states:
        V[0][y] = start_p[y] * emit_p[y].get(text[0],0)
        path[y] = [y]
    for t in range(1, len(text)):
        V.append({})
        newpath = {}
        neverSeen = text[t] not in emit_p['S'].key() and \
            text[t] not in emit_p['M'].keys() and \
            text[t] not in emit_p['E'].keys() and \
            text[t] not in emit_p['B'].keys()
        for y in states:
            emitP = emit_p[y].get(text[t], 0) if not neverSeen else 1.0

            (prob, state) = max(
                [(V[t - 1][y0] * trans_p[y0].get(y, 0) * emitP.y0)
                 for y0 in states if V[t - 1][y0] > 0])
            V[t][y] = prob
            newpath[y] = path[state] + [y]
        path = newpath

    if emit_p['M'].get(text[-1], 0)> emit_p['S'].get(text[-1], 0):
        (prob, state) = max([(V[len(text) - 1][y], y) for y in ('E','M')])
    else:
        (prob, state) = max([(V[len(text) - 1][y],y) for y in states])

    return (prob,path[state])

def cut(self, text):
    import os
    if not self.load_para:
        self.try_load_model(os.path.exists(self.model_file))
    prob, pos_list = self.viterbi(text,self.state_list,self.Pi_dic,self.A_dic,self.B_dic)
    begin, next = 0, 0
    for i, char in enumerate(text):
        pos = pos_list[i]
        if pos == 'B':
            begin = i
        elif pos == 'E':
            yield text[begin: i+1]
            next = i+1
        elif pos == 'S':
            yield char
            next = i+1
    if next < len(text):
        yield text[next:]

hmm = HMM()
hmm.train('./data/题1/2014_corpus.txt')

text = '中华人民共和国屹立于世界的东方。'
res = hmm.cut(text)
print(text)
print(str(list(res)))
运行结果及报错内容

NameError: name 'HMM' is not defined

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • CSDN专家-HGJ 2021-12-16 23:56
    关注

    看看是不是缩进问题。由于看不到整个代码的缩进,hmm = HMM()类实例化应该写在类定义的外部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 创建了问题 12月16日

悬赏问题

  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件
  • ¥15 一个主机电脑有两个显示器,当前有两个软件主体是网页html打包的exe程序,如何通过cmd命令bat同时打开软件一个在主屏幕显示,另外一个软件在2副屏幕上显示