python统计英文文本中的回文单词数
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统计英文文本中的回文单词数的更多相关文章
- python统计一个文本中重复行数的方法
python统计一个文本中重复行数的方法 这篇文章主要介绍了python统计一个文本中重复行数的方法,涉及针对Python中dict对象的使用及相关本文的操作,具有一定的借鉴价值,需要的朋友可以参考下 ...
- C#统计英文文本中的单词数并排序
思路如下:1.使用的Hashtable(高效)集合,记录每个单词出现的次数2.采用ArrayList对Hashtable中的Keys按字母序排列3.排序使用插入排序(稳定) public void S ...
- 翻译器DIY它———算在英文文本中的单词数,字符和行数
咳咳.这部分应该是序列化编译器DIY的,然而,在这样做DIY第一次使用前flex 为了练练手,对于后者的理解是有帮助. 在word 我经常看到一个字计数功能,因此,它是如何实现,当然,首先想到的是要经 ...
- 统计英文文章中各单词的频率,打印频率最高的十个单词(C语言实现)
一.程序思路及相关代码 首先打开文件,代码如下 FILE *fp; char fname[10]; printf("请输入要分析的文件名:\n"); scanf("%s ...
- Python:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0
下面是我写的python的一个小脚本,作用是:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0.用的是MySQL数据库. 要注意的是:strip函数的使用,该函数的作用是去除字符串两端多 ...
- 【python实例】判断是否是回文数
""" 输入一个数,判断一个这个数是否是回文数.例如:121,这个数反过来还是121,所以这个是回文数: 再如:134,这个数反过来是431,所以这不是一个回文数: 12 ...
- python 统计工作簿中每个人名出现的次数
工作簿 需求:统计人名出现的次数 代码: # coding=gbk import pandas as pd import re def extract_chinese(txt): pattern = ...
- WordCount C语言实现求文本的字符数,单词数,行数
1.码云地址: https://gitee.com/miaomiaobobo/WordCount 2.psp表格 PSP2.1表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) P ...
- Python统计excel表格中文本的词频,生成词云图片
import xlrd import jieba import pymysql import matplotlib.pylab as plt from wordcloud import WordClo ...
随机推荐
- A - Number Sequence 哈希算法(例题)
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...
- nginx配置虚拟主机、反向代理和负载均衡
为了实现这个功能,需要修改nginx的配置文件,将nginx.conf清理一下,使结构更清晰. worker_processes ; events { worker_connections ; } h ...
- [YII2] 去除自带头部以及底部右下角debug调试功能
YII2 去除自带头部以及底部右下角debug调试功能
- .NET 4 实践 - 使用dynamic和MEF实现轻量级的AOP组件 (4)
转摘 https://www.cnblogs.com/niceWk/archive/2010/07/23/1783394.html 借花献佛 前面我们介绍了构成DynamicAspect绝大部分的类, ...
- MVC-基础01
MVC体系结构将应用程序分成三个主要组件:模型(Model).视图(View).和控制器(Controller).在ASP.NET MVC应用程序中,数据操控的逻辑包含在Models文件夹下,数据的展 ...
- Neo4J 查找两节点之间的路径
# 两节点之间的所有路径MATCH p=(a)-[*]->(b)RETURN p # a->b 直接连接MATCH p=(a)-[]->(b)RETURN p # a-...> ...
- 百度paddlepaddle学习体会
一个偶然从微信公众号中刷到了<python小白逆袭A1大神>的文章,让我不经意的邂逅了飞桨(paddlepaddle),通过加入飞桨训练营一周的学习.实践,对飞桨有了很多的了解(飞桨官网: ...
- php表格--大数据处理
参考来源1:https://blog.csdn.net/tim_phper/article/details/77581071 参考来源2:https://blog.csdn.net/qq_376822 ...
- thinkphp5.0 模型的应用
<?php namespace app\admin\controller; use app\common\controller\BaseController; use think\Db;//数据 ...
- urlencode()和rawurlencode()区别
urlencode和rawurlencode两个方法在处理字母数字,特殊符号,中文的时候结果都是一样的 ,唯一的不同是对空格的处理, urlencode处理成“+”, rawurlencod ...