python 文本单词提取和词频统计的实例

yipeiwu_com5年前Python基础

这些对文本的操作经常用到, 那我就总结一下。 陆续补充。。。

操作:

strip_html(cls, text) 去除html标签

separate_words(cls, text, min_lenth=3) 文本提取

get_words_frequency(cls, words_list) 获取词频

源码:

class DocProcess(object):

 @classmethod
 def strip_html(cls, text):
  """
   Delete html tags in text.
   text is String
  """
  new_text = " "
  is_html = False
  for character in text:
   if character == "<":
    is_html = True
   elif character == ">":
    is_html = False
    new_text += " "
   elif is_html is False:
    new_text += character
  return new_text

 @classmethod
 def separate_words(cls, text, min_lenth=3):
  """
   Separate text into words in list.
  """
  splitter = re.compile("\\W+")
  return [s.lower() for s in splitter.split(text) if len(s) > min_lenth]

 @classmethod
 def get_words_frequency(cls, words_list):
  """
   Get frequency of words in words_list.
   return a dict.
  """
  num_words = {}
  for word in words_list:
   num_words[word] = num_words.get(word, 0) + 1
  return num_words

以上这篇python 文本单词提取和词频统计的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

谈一谈基于python的面向对象编程基础

谈一谈基于python的面向对象编程基础

活在当下的程序员应该都听过“面向对象编程”一词,也经常有人问能不能用一句话解释下什么是“面向对象编程”,我们先来看看比较正式的说法。 把一组数据结构和处理它们的方法组成对象(object...

如何使用python把ppt转换成pdf

ppt要想完美的转pdf,图片,还是需要在windows下面来操作。 1,安装python3.5.1 下载地址 Windows x86-64 executable installer,w...

用Python编程实现语音控制电脑

电脑面前的你,是否也希望能让电脑听命于你?   当你累的时候,只需说一声“我累了”,电脑就会放着优雅的轻音乐来让你放松。 或许你希望你在百忙之中,能让电脑郎读最新的N...

Python实现全角半角转换的方法

本文实例讲解了Python实现全角半角转换的方法,相信对于大家的Python学习能够起到一定的参考借鉴价值。如下所示: 一、全角半角转换概述: 全角字符unicode编码从65281~6...

Python设计模式之策略模式实例详解

Python设计模式之策略模式实例详解

本文实例讲述了Python设计模式之策略模式。分享给大家供大家参考,具体如下: 策略模式(Strategy Pattern):它定义了算法家族,分别封装起来,让他们之间可以相互替换,此模...