FMM和BMM的python代码实现

  1. FMM和BMM的编程实现,其实两个算法思路都挺简单,一个是从前取最大词长度的小分句,查找字典是否有该词,若无则分句去掉最后面一个字,再次查找,直至分句变成单词或者在字典中找到,并将其去除,然后重复上述步骤。BMM则是从后取分句,字典中不存在则分句最前去掉一个字,也是重复类似的步骤。

  2. readCorpus.py

    import sys
    output = {}
    with open('语料库.txt', mode='r', encoding='UTF-8') as f:
    for line in f.readlines():
    if line is not None:
    # 去除每行的换行符
    t_line = line.strip('\n')
    # 按空格分开每个词
    words = t_line.split(' ')
    for word in words:
    # 按/分开标记和词
    t_word = word.split('/')
    # 左方括号去除
    tf_word = t_word[0].split('[')
    if len(tf_word) == 2:
    f_word = tf_word[1]
    else:
    f_word = t_word[0]
    # 若在输出字典中,则value+1
    if f_word in output.keys():
    output[f_word] = output[f_word]+1
    # 不在输出字典中则新建
    else:
    output[f_word] = 1
    big_word1 = t_line.split('[')
    for i in range(1, len(big_word1)):
    big_word2 = big_word1[i].split(']')[0]
    words = big_word2.split(' ')
    big_word = ""
    for word in words:
    # 按/分开标记和词
    t_word = word.split('/')
    big_word = big_word + t_word[0]
    # 若在输出字典中,则value+1
    if big_word in output.keys():
    output[big_word] = output[big_word]+1
    # 不在输出字典中则新建
    else:
    output[big_word] = 1 f.close() with open('output.txt', mode='w', encoding='UTF-8') as f:
    while output:
    minNum = sys.maxsize
    minName = ""
    for key, values in output.items():
    if values < minNum:
    minNum = values
    minName = key
    f.write(minName+": "+str(minNum)+"\n")
    del output[minName]
    f.close()
  3. BMM.py

    MAX_WORD = 19
    word_list = []
    ans_word = []
    with open('output.txt', mode='r', encoding='UTF-8')as f:
    for line in f.readlines():
    if line is not None:
    word = line.split(':')
    word_list.append(word[0])
    f.close()
    #num = input("输入句子个数:")
    #for i in range(int(num)):
    while True:
    ans_word = []
    try:
    origin_sentence = input("输入:\n")
    while len(origin_sentence) != 0:
    len_word = MAX_WORD
    while len_word > 0:
    # 从后读取最大词长度的数据,若该数据在字典中,则存入数组,并将其去除
    if origin_sentence[-len_word:] in word_list:
    ans_word.append(origin_sentence[-len_word:])
    len_sentence = len(origin_sentence)
    origin_sentence = origin_sentence[0:len_sentence-len_word]
    break
    # 不在词典中,则从后取词长度-1
    else:
    len_word = len_word - 1
    # 单词直接存入数组
    if len_word == 0:
    if origin_sentence[-1:] != ' ':
    ans_word.append(origin_sentence[-1:])
    len_sentence = len(origin_sentence)
    origin_sentence = origin_sentence[0:len_sentence - 1]
    for j in range(len(ans_word)-1, -1, -1):
    print(ans_word[j] + '/', end='')
    print('\n')
    except (KeyboardInterrupt, EOFError):
    break
  4. FMM.py

    MAX_WORD = 19
    word_list = []
    with open('output.txt', mode='r', encoding='UTF-8')as f:
    for line in f.readlines():
    if line is not None:
    word = line.split(':')
    word_list.append(word[0])
    f.close()
    #num = input("输入句子个数:")
    #for i in range(int(num)):
    while True:
    try:
    origin_sentence = input("输入:\n")
    while len(origin_sentence) != 0:
    len_word = MAX_WORD
    while len_word > 0:
    # 读取前最大词长度数据,在数组中则输出,并将其去除
    if origin_sentence[0:len_word] in word_list:
    print(origin_sentence[0:len_word]+'/', end='')
    origin_sentence = origin_sentence[len_word:]
    break
    # 不在字典中,则读取长度-1
    else:
    len_word = len_word - 1
    # 为0则表示为单词,输出
    if len_word == 0:
    if origin_sentence[0] != ' ':
    print(origin_sentence[0]+'/', end='')
    origin_sentence = origin_sentence[1:]
    print('\n')
    except (KeyboardInterrupt, EOFError):
    break
  5. 效果图

  • BMM.py(不含大粒度分词)

  • BMM.py(含大粒度分词)

  • FMM.py(不含大粒度分词)

  • FMM.py(含大粒度分词)

我们可以观察到含大粒度分词的情况将香港科技大学,北京航空航天大学等表意能力强的词分在了一起而不是拆开,更符合分词要求。

FMM和BMM的python代码实现的更多相关文章

  1. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  2. if __name__== "__main__" 的意思(作用)python代码复用

    if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog  http://www.dabu.info/if-__-name__ ...

  3. Python 代码风格

    1 原则 在开始讨论Python社区所采用的具体标准或是由其他人推荐的建议之前,考虑一些总体原则非常重要. 请记住可读性标准的目标是提升可读性.这些规则存在的目的就是为了帮助人读写代码,而不是相反. ...

  4. 一行python代码实现树结构

    树结构是一种抽象数据类型,在计算机科学领域有着非常广泛的应用.一颗树可以简单的表示为根, 左子树, 右子树. 而左子树和右子树又可以有自己的子树.这似乎是一种比较复杂的数据结构,那么真的能像我们在标题 ...

  5. [Dynamic Language] 用Sphinx自动生成python代码注释文档

    用Sphinx自动生成python代码注释文档 pip install -U sphinx 安装好了之后,对Python代码的文档,一般使用sphinx-apidoc来自动生成:查看帮助mac-abe ...

  6. 上传自己的Python代码到PyPI

    一.需要准备的事情 1.当然是自己的Python代码包了: 2.注册PyPI的一个账号. 二.详细介绍 1.代码包的结构: application \application __init__.py m ...

  7. 如何在batch脚本中嵌入python代码

    老板叫我帮他测一个命令在windows下消耗的时间,因为没有装windows那个啥工具包,没有timeit那个命令,于是想自己写一个,原理很简单: REM timeit.bat echo %TIME% ...

  8. ROS系统python代码测试之rostest

    ROS系统中提供了测试框架,可以实现python/c++代码的单元测试,python和C++通过不同的方式实现, 之后的两篇文档分别详细介绍各自的实现步骤,以及测试结果和覆盖率的获取. ROS系统中p ...

  9. 让计算机崩溃的python代码,求共同分析

    在现在的异常机制处理的比较完善的编码系统里面,让计算机完全崩溃无法操作的代码还是不多的.今天就无意运行到这段python代码,运行完,计算机直接崩溃,任务管理器都无法调用,任何键都用不了,只能强行电源 ...

随机推荐

  1. ASIHTTPRequest框架使用总结系列之阿堂教程3(异步请求)

    在上一节中,阿堂和网友们分享了ASIHTTPRequest框架对于get,post的同步请求方式.很显然,如果网速比较慢,查询的时候会一直很黑屏,直到请求结束界面才出现结果,这样用户体验肯定很不好了. ...

  2. Makefile学习之显示命令与出错命令

    显示命令: 1.在makefile中 如果在命令行下添加“@”符号,则只执行,不显示命令: 2.在执行make时,make -n 表示只显示命令而不执行: make -s 表示只执行命令而不显示: 3 ...

  3. 15 Basic ‘ls’ Command Examples in Linux

    FROM: http://www.tecmint.com/15-basic-ls-command-examples-in-linux/ ls command is one of the most fr ...

  4. 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)

    [原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...

  5. 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 40怎么办

    出现这种错误的时候,我把一套测试完好的电机和驱动器,直接把跟电机连接的线拔掉换另一个电机,驱动器所有参数不变,这样由于是绝对值编码器的,所以驱动器已经记住了上一个电机的圈数,换了新的电机之后圈数不对了 ...

  6. C++ 11 可变模板参数的两种展开方式

    #include <iostream> #include <string> #include <stdint.h> template<typename T&g ...

  7. NinePatch

    将图片保存为扩展名为.9.png的格式直接放入Android Studio中的drawable文件夹,拖拉选择拉伸区域,如下图,即可制作出可拉伸背景

  8. python——iterator迭代器|iterator详解——20140918|

    -----------------------------------------------------------------------------前言--------------------- ...

  9. select、poll、epoll之间的区别总结[整理](转)

    select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作.但select ...

  10. 【Excle数据透视表】如何重复显示行字段的项目标签

    前提:该数据透视表以表格形式显示 解决办法: 通过报表布局设置"重复所有项目标签" 修改前样式 步骤 单击数据透视表中任意单元格→设计→报表布局→重复所有项目标签 修改后样式