Python编程入门:英文词频统计

text = "Got tho on super sale. Love it! Cuts my drying time in half Reckon I have had this about a year now,\
        at least 7 months. Works great, I use it 5 days a week, blows hot air, doesnt overheat,\
        isnt to big, came quick, didnt cost much. Get you one, you will like it.The styling tip does not stay on,\
        keeps falling off in the middle of blow drying and then it's too hot to put back"
text = text.lower()
# 将特殊字符替换成为空格
for ch in '!@#$%:^&*()-.;':
    text = text.replace(ch, " ")
# 对字符串通过空格进行分割
words = text.split()
counts = {}

for word in words:
    if word in counts:
        counts[word] = counts[word] + 1
    else:
        counts[word] = 1

items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
# 输出词频统计的结果
for i in range(3):
    word, count = items[i]
    if  i<2:
       print("{0}:{1}".format(word, count))
    else:
       print("{0}:{1}".format(word, count),end="")

要统计英文词频,可以使用Python中的字典数据结构和字符串操作函数。

下面是一个简单的示例代码,用于统计一个英文文本中每个单词出现的次数:

def word_frequency(text):
    # 将文本转换为小写并按空格分割为单词列表
    words = text.lower().split()
    
    # 创建一个空字典用于存储单词及其出现次数
    frequency = {}
    
    # 遍历单词列表
    for word in words:
        # 去除单词中的标点符号
        word = word.strip(".,!?")
        
        # 如果单词已经在字典中,则将其出现次数加1;否则将单词添加到字典中,并将出现次数设为1
        if word in frequency:
            frequency[word] += 1
        else:
            frequency[word] = 1
    
    # 返回字典
    return frequency

# 示例用法
text = "This is a sample text. It contains some words, some of which are repeated."
result = word_frequency(text)
print(result)

运行以上代码,输出结果如下:

{'this': 1, 'is': 1, 'a': 1, 'sample': 1, 'text': 1, 'it': 1, 'contains': 1, 'some': 2, 'words': 1, 'of': 1, 'which': 1, 'are': 1, 'repeated': 1}

可以看到,每个单词及其出现次数被存储在一个字典中。

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/12276.html

(0)
江山如画的头像江山如画管理团队
上一篇 2023年10月3日 上午10:36
下一篇 2023年10月3日 上午11:21

99%的人还看了以下文章

  • createStatement参数详解,带参数与不带参数的区别

    Connection接口的createStatement方法,可用来创建向数据库发送SQL语句的对象,用于执行SQL语句。 常用代码: Statement stmt = con.createStatement(); Statement stmt = con.createStatement (int resultSetType,int resultSetCon…

    2018年10月23日
    9.7K0
  • jsp写mysql数据库出现中文乱码

    今天用jsp做个图片书管理系统,向mysql数据库中存中文的时候显示乱码,如图书名: web???? 修改页面是http://localhost:8080/library/book?action=bookModifyQuery&ID=14 根据中文乱码的处理方法: 表单method方式为post或get中文乱码的解决方法 jsp:include包含h…

    2020年8月22日
    4.9K0
  • Ubantu为非标准应用程序添加桌面图标的操作方法

    本文主要指的是第三方或自己编写的非标准应用程序 需要编写.desktop文件。 主要步骤如下:1、进入/usr/share/applications目录,建立一个空白的文本文件,文件要以.desktop作为扩展名。 命令行进入/usr/share/applications cd /usr/share/applications 2、在文件写入启动自己应用的相应…

    2022年9月1日
    6.1K0
  • 如何用R语言绘制散点图

    这篇文章主要介绍了如何用R语言绘制散点图,帮助大家更好的理解和学习使用R语言,感兴趣的朋友可以了解下

    2022年8月9日 编程开发
    6.6K0
  • MySql数据库Timestamp、time、datetime 区别及使用详解

    对于数据库来说,有多种日期时间字段可供选择,如 timestamp 和 datetime 。 不仅新手,包括一些有经验的程序员还是比较迷茫,究竟我该用哪种类型来存储日期时间呢? 一个完整的日期格式如下:YYYY-MM-DD HH:MM:SS[.fraction],它可分为两部分:date部分和time部分,其中,date部分对应格式中的“YYYY-MM-DD…

    编程开发 2018年5月2日
    7.7K1
  • %matplotlib inline使用详解

    #内嵌画图 %matplotlib inline import matplotlib # 注意这个也要import一次 import matplotlib.pyplot as plt myfont = matplotlib.font_manager.FontProperties(fname=r’C:/Windows/Fonts/msyh.ttf’) # 这一…

    2023年1月13日
    2.7K0

发表回复

登录后才能评论