1. 要求:

给定一篇纯英文的文本,统计其中回文单词的比列,并输出其中的回文单词,文本数据如下:

This is Everyday Grammar. I am Madam Lucija
And I am Kaveh. Why the title, Lucija?
Well, it is a special word. Madam?
Yeah, maybe I should spell it for you forward or backward?
I am lost. The word Madam is a Palindrome.
I just learned about them the other day and I am having a lot of fun!
Palindrome, huh? Let me try!
But first, we need to explain what a Palindrome is.
That is easy! Palindromes are words, phrases or numbers that read the same back and forward, like DAD.
So, Palindromes can be serious or just silly.
Yup, like, A nut for a jar of tuna.
Or, Borrow or Rob. Probably borrow!
And if you are hungry, you can always have a Taco cat?
That is gross. What about this one?
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal panama!
That is a real tongue twister. But I prefer Italy. Amore Roma!
So how do we make palindromes?
One, read words backwards and see if they make sense.
Two, try to make palindromes where even the spacing between words is consistent. Like, NotATon.
And three, you can always check the internet for great palindromes!
And that is Everyday Grammar.

注意:

  • 区分单词的大小写,即同一个单词的大写和小写视为不同的单词;

2. 分析

本次任务的思路很简单,基本步骤如下:

  • 第一步:读入文本数据,然后去掉文本中的换行符;
  • 第二步:去掉第一步处理后的文本中的标点符号,这里使用正则表达式将文本中的单词保留,从而达到去标点符号的目的。之后使用一个列表存入每一行去掉标点之后的文本。
  • 第三步:根据预处理之后的文本统计词频,因为一篇文本里面可能有很多重复的单词,那么只须判断文本构成的子典中的单词是否是回文单词即可。
  • 第四步:遍历字典中的键,并判断是否是回文单词,具体实现方法见代码;
  • 第五步:根据找到的回文单词计算文本中回文单词的比例;

3. 代码

import re
from collections import Counter # 文本预处理,返回值为['This', 'is', 'Everyday']这种形式
def process(path):
token = []
with open(path, 'r') as f:
text = f.readlines()
for row_text in text:
row_text_prod = row_text.rstrip('\n')
row_text_prod = re.findall(r'\b\w+\b', row_text_prod)
token = token + row_text_prod
return token # 统计回文单词
def palindrome(processed_text):
c = Counter(processed_text) # 词频字典
palindrome_word = [] # 回文单词列表
not_palindrome_word = [] # 非回文单词列表
# 遍历词频字典
for word in c.keys():
flag = True
i, j = 0, len(word)-1
# 判断是否是回文单词
while i < j:
if word[i] != word[j]:
not_palindrome_word.append(word) # 不是回文单词
flag = False
break
i += 1
j -= 1
if flag:
palindrome_word.append(word) # 是回文单词
print("回文单词:")
print(palindrome_word)
print("非回文单词:")
print(not_palindrome_word)
# 统计回文单词的比率
total_palindrome_word = 0
for word in palindrome_word:
total_palindrome_word += c[word]
print("回文单词的比例为:{:.3f}".format(total_palindrome_word / len(processed_text))) def main():
text_path = 'test.txt'
processed_text = process(text_path)
palindrome(processed_text) if __name__ == '__main__':
main()

reference:

python3小技巧之:妙用string.punctuation

回文字符串(Palindromic_String)

python统计英文文本中的回文单词数的更多相关文章

  1. python统计一个文本中重复行数的方法

    python统计一个文本中重复行数的方法 这篇文章主要介绍了python统计一个文本中重复行数的方法,涉及针对Python中dict对象的使用及相关本文的操作,具有一定的借鉴价值,需要的朋友可以参考下 ...

  2. C#统计英文文本中的单词数并排序

    思路如下:1.使用的Hashtable(高效)集合,记录每个单词出现的次数2.采用ArrayList对Hashtable中的Keys按字母序排列3.排序使用插入排序(稳定) public void S ...

  3. 翻译器DIY它———算在英文文本中的单词数,字符和行数

    咳咳.这部分应该是序列化编译器DIY的,然而,在这样做DIY第一次使用前flex 为了练练手,对于后者的理解是有帮助. 在word 我经常看到一个字计数功能,因此,它是如何实现,当然,首先想到的是要经 ...

  4. 统计英文文章中各单词的频率,打印频率最高的十个单词(C语言实现)

     一.程序思路及相关代码 首先打开文件,代码如下 FILE *fp; char fname[10]; printf("请输入要分析的文件名:\n"); scanf("%s ...

  5. Python:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0

    下面是我写的python的一个小脚本,作用是:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0.用的是MySQL数据库. 要注意的是:strip函数的使用,该函数的作用是去除字符串两端多 ...

  6. 【python实例】判断是否是回文数

    """ 输入一个数,判断一个这个数是否是回文数.例如:121,这个数反过来还是121,所以这个是回文数: 再如:134,这个数反过来是431,所以这不是一个回文数: 12 ...

  7. python 统计工作簿中每个人名出现的次数

    工作簿 需求:统计人名出现的次数 代码: # coding=gbk import pandas as pd import re def extract_chinese(txt): pattern = ...

  8. WordCount C语言实现求文本的字符数,单词数,行数

    1.码云地址: https://gitee.com/miaomiaobobo/WordCount 2.psp表格 PSP2.1表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) P ...

  9. Python统计excel表格中文本的词频,生成词云图片

    import xlrd import jieba import pymysql import matplotlib.pylab as plt from wordcloud import WordClo ...

随机推荐

  1. I. Same String

    有两个只由小写字母组成的长度为n的字符串s1,s2和m组字母对应关系,每一组关系由两个字母c1和c2组成,代表c1可以直接变成c2,你需要判断s1是否可以通过这m组关系转换为s2. 输入格式 第一行输 ...

  2. 2016NOIP普及组T2回文日期

    回文日期 分类:枚举,函数 [题目描述] 日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月份,最后2位代表日期 ...

  3. JavaScript_Array

    Array 概念特点 值的有序集合: 每一个值叫一个元素: 每个元素在数组中有一个位置,以数字表示,称为索引(下标): 元素可以是任何类型 索引从0开始,最大为2的32次方 数组的创建 数组直接量 v ...

  4. wechall前十题

    今天开始打一打wechall 累了打wechall,不累的时候开始打buu 第一题:Get Sourced 查看源代码即可,拉到底部 第二题:Stegano 属于misc的范畴,直接下载下来,然后no ...

  5. 来个干货——使用VS2019发布.NET Core程序并部署到IIS的最新教程

    使用VS2019发布.NET Core程序并部署到IIS,不管你是使用.NET Core开发的是Web API还是网站类的程序,如果你是部署到IIS,那么下面的内容都适合于你,不会将.NET Core ...

  6. 使用hexo和coding建立静态博客站点

    背景 由于工作性质的原因,做技术的总想记录和分享一下自己的学习和成长历程,向这世界证明我来过.写文章,发博客,一开始使用51cto,广告太多,看起来让人很痛苦:接着试用了博客园,广告少一些,但感觉还是 ...

  7. Docker 搭建 ELK 集群步骤

    前言 本篇文章主要介绍在两台机器上使用 Docker 搭建 ELK. 正文 环境 CentOS 7.7 系统 Docker version 19.03.8 docker-compose version ...

  8. PE文件学习(1)DOS和NT

    大致结构 DOS头和NT头之间通常还有个DOS Stub DOS头 DOS头的作用是兼容MS-DOS操作系统中的可执行文件 一般没啥用 记录着PE头的位置 DOS头定义部分 typedef struc ...

  9. bootstrap4中使用fontawesome5.6.3

    先下载fontawesome5.6.3,选择free for web,下载完解压,丢在资源目录下 <form action=""> <div class=&quo ...

  10. 异常:NoSuchFieldError: BEST_MATCHING_HANDLER_ATTRIBUTE

    出现的原因 pom 依赖之间不匹配导致 当前的 pom 调整后访问资源成功后面的 pom