停用词表的修改

停用词表在“pyhanlp\static\data\dictionary”路径下的“stopwords.txt”文件中,CoreStopWordDictionary.apply方法支持去除停用词。如果需要修改停用词表,则直接编辑文件“stopwords.txt”,之后删除路径下的“stopwords.txt.bin”,运行CoreStopWordDictionary.apply后即可自动生效。有关验证的方法见“验证是否生效”小节。

自定义词语过滤方法

用户可以通过编写“pyhanlp\static”路径下的“MyFilter.java”文件设置自己的词语过滤方法。应当注意这里处理的语言单位是词语,而不是字。编辑完毕后需要编译该文件并生成字节码文件,之后运行CoreStopWordDictionary.apply方法时就会自动调用用户自己的词语过滤方法了。这里给出一个自定义过滤方法的编写示例代码。

import os

from pyhanlp.static import STATIC_ROOT, HANLP_JAR_PATH

java_code_path = os.path.join(STATIC_ROOT, 'MyFilter.java')

with open(java_code_path, 'w') as out:

java_code = """

import com.hankcs.hanlp.dictionary.stopword.CoreStopWordDictionary;

import com.hankcs.hanlp.dictionary.stopword.Filter;

import com.hankcs.hanlp.seg.common.Term;

public class MyFilter implements Filter

{

public boolean shouldInclude(Term term)

{

if (term.nature.startsWith('m')) return false; // 数词过滤

if (term.nature.startsWith('q')) return false; // 量词过滤

if (term.nature.startsWith('t')) return false; // 时间词过滤

if (term.nature.startsWith("w")) return false; // 过滤标点符号

return !CoreStopWordDictionary.contains(term.word); // 停用词过滤

}

}

"""

out.write(java_code)

os.system('javac -cp {} {} -d {}'.format(HANLP_JAR_PATH, java_code_path, STATIC_ROOT))

验证是否生效

本节给出停用词表修改后以及使用了自定义词语过滤方法的示例代码。

from pyhanlp import *

# 加载停用词类

CoreStopWordDictionary = JClass("com.hankcs.hanlp.dictionary.stopword.CoreStopWordDictionary")

# 加载自定义词语过滤逻辑

MyFilter = JClass('MyFilter')

CoreStopWordDictionary.FILTER = MyFilter()

term_list = HanLP.segment(text)

CoreStopWordDictionary.apply(term_list)

最新文章

  1. SE(homework3)_敏捷模型
  2. BundleConfig.cs
  3. gulp 插件
  4. 结对项目——高级四则运算检验器记录(168 & 187)
  5. 【JavaScript】JQuery中$.fn、$.extend、$.fn.extend
  6. HDU1250 高精度斐波那契数列
  7. PyChram使用技巧总结
  8. 【重走Android之路】【开篇】序
  9. sass 使用入门教程
  10. Android Integer.decode()和Intger.valueof()
  11. WKWebView-b
  12. 【转】AAC ADTS格式分析
  13. HDU 1851 A Simple Game
  14. 用git上传项目到github
  15. 和S5933比较起来,开发PLX9054比较不幸,可能是第一次开发PCI的缘故吧。因为,很多PCI的例子都是对S5933,就连微软出版的《Programming the Microsoft Windows Driver Model》都提供了一个完整的S5933的例子。 在这篇有关DDK的开发论文里。
  16. UVA No Tipping
  17. Asp.Net Identity 深度解析 之 注册登录的扩展
  18. (转)java二维数组的深度学习(静态与动态)
  19. unity3d 脚本学习系列
  20. 基于node写了个工具,可以在线制作“sorry,为所欲为”的 GIF(开源)

热门文章

  1. Python身份运算符
  2. BZOJ 3667: Rabin-Miller算法 (Pollard-Rho 模板)
  3. cogs服务点设置(不凶,超乖) x
  4. Java集合框架中底层文档的List与Set
  5. 「CSA Round #41」BFS-DFS
  6. flask 第六篇 flask内置的session
  7. 黑马vue---1-7、vue杂记
  8. koa 基础(二十三)封装 DB 库 --- 应用
  9. LC 425. Word Squares 【lock,hard】
  10. 利用CountDownTimer倒计时的简单使用实现