当前位置:   article > 正文

jieba库:Tokenizer()类详解:(六)cut,cut_for_search_jieba cutall和cutforsearch

jieba cutall和cutforsearch

2021SC@SDUSC


cut

  1. def cut(sentence,cut_all=False,HMM=True):
  2. #整个分词的主函数,
  3. #输入sentence是需要分词的句子;
  4. #cut_all是分词模式,默认是精准模式;
  5. #HMM是隐马尔可夫链,默认开启。
  6. '''The main function that segments an entire sentence that contains
  7. Chinese characters into seperated words.
  8. Parameter:
  9. - sentence: The String to be segmented
  10. - cut_all: Model. True means full pattern, false means accurate pattern.
  11. - HMM: Whether use Hidden Markov Model.
  12. '''
  13. if not isinstance(sentence, unicode): #如果输入的句子不是统一编码形式的unicode,那么使用Python中的decode,将其解码称为unicode
  14. #注:unicode、gbk等都是中文编码的方式
  15. try:
  16. sentence = sentence.decode('utf-8')
  17. except UnicodeDecodeError: #如果在decode的过程中出现了错误,那么就解码成gbk形式的。
  18. sentence = sentence.decode('gbk','ignore')
  19. '''
  20. \u4E00-\u9FA5a-zA-Z0-9+#&\._ : All non-space characters. Will be handled with re_han
  21. \r\n|\s : whitespace characters. Will not be Handled.
  22. '''
  23. #下面使用到了Python中正则表达式模块re,正则表达式是用于处理字符串的强大工具,
  24. #拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十分强大。
  25. #其最擅长的、最被大家使用的就是字符串匹配功能
  26. #正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功;
  27. #一旦有匹配不成功的字符则匹配失败。如果表达式中有量词或边界,这个过程会稍微有一些不同
  28. #re.compile 可以把正则表达式编译成一个正则表达式对象。
  29. #可以把那些经常使用的正则表达式编译成正则表达式对象,这样可以提高一定的效率
  30. #关于正则表达式指南:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
  31. #关于正则表达式常用的处理函数:http://www.cnblogs.com/sevenyuan/archive/2010/12/06/1898075.html
  32. re_han, re_skip = re.compile(ur"([\u4E00-\u9FA5a-zA-Z0-9+#&\._]+)", re.U), re.compile(ur"(\r\n|\s)", re.U)
  33. #将以下正则表达式编译成re_han对象:
  34. #\u4E00-\u9FA5是所有汉字的编码,a-zA-Z是所有大小写字母的编码,0-9是所有阿拉伯数字的编码,+#&\._其余字符
  35. #将以下正则表达式编译成re_skip对象:
  36. #\r是回车,\n是换行,|或, \s是空白字符(\r\n\t\f\v)
  37. #re.U是使得compile的结果取决于unicode定义的字符属性
  38. if cut_all:
  39. re_han, re_skip = re.compile(ur"([\u4E00-\u9FA5]+)", re.U), re.compile(ur"[^a-zA-Z0-9+#\n]", re.U)
  40. #全模式下,只handle中文字符部分[\u4E00-\u9FA5],其余的字母、数字等等都要跳过。
  41. blocks = re_han.split(sentence)
  42. #这个split的作用和Python自带的split作用差不多,按照能够匹配的子串,即刚才compile进去的哪些字符,将sentence分割
  43. #而且是把sentence分成几个blocks,然后针对每一块进行分词
  44. '''
  45. 例子:
  46. p = re.compile(r'\d+')
  47. print p.split('one1two2three3four4')
  48. ### output ###
  49. # ['one', 'two', 'three', 'four', ''] #按照compile的表达式,把一个string分成几个blocks
  50. '''
  51. if HMM: #默认情况使用__cut_DAG作为分词函数,为每个block分词
  52. cut_block = __cut_DAG
  53. else:
  54. cut_block = __cut_DAG_NO_HMM
  55. if cut_all:
  56. cut_block = __cut_all
  57. for blk in blocks: #针对每一块进行分词
  58. if len(blk)==0: #当blk为空时,继续下一个blk的分词
  59. continue
  60. if re_han.match(blk): #如果re_han.match(blk)为true,说明当前blk是re_han范围内的文本
  61. for word in cut_block(blk): #__cut_DAG作为分词函数为每个block分词,返回分词后word列表
  62. yield word #yield作用也是返回,但是其不会消除local variables。
  63. #yield statement is what makes a function a generator function.
  64. #具体的yield和return在Python中的区别:http://blog.csdn.net/jiyanfeng1/article/details/8148561
  65. else:
  66. tmp = re_skip.split(blk) #按照re_skip中的正则表达式,把blk划分成几块
  67. for x in tmp:
  68. if re_skip.match(x):
  69. yield x #返回匹配划分的每一部分
  70. elif not cut_all: #如果是精准模式,则返回x中的每一项,即试图将句子最精确地切开
  71. for xx in x:
  72. yield xx
  73. else: #如果是全模式,把句子中所有的可以成词的词语都扫描出来
  74. yield x

cut_for_search()

  1. # 搜索引擎模式,首先调用cut()方法,得到切分后的结果,
  2. # 然后对length大于二和大于三的子结果进行遍历,检测是否有二字成词(对于长度大于二的结果)和三字成词(对于长度大于三的结果),
  3. # 如果有,先于结果加入迭代器返回。
  4. def cut_for_search(self, sentence, HMM=True):
  5. """
  6. Finer segmentation for search engines.
  7. """
  8. words = self.cut(sentence, HMM=HMM)
  9. for w in words:
  10. if len(w) > 2:
  11. for i in xrange(len(w) - 1):
  12. gram2 = w[i:i + 2]
  13. if self.FREQ.get(gram2):
  14. yield gram2
  15. if len(w) > 3:
  16. for i in xrange(len(w) - 2):
  17. gram3 = w[i:i + 3]
  18. if self.FREQ.get(gram3):
  19. yield gram3
  20. yield w

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/348412
推荐阅读
相关标签
  

闽ICP备14008679号