python统计英文单词个数_python统计英文文本中的回文单词数

1. 要求:

给定一篇纯英文的文本,统计其中回文单词的比列,并输出其中的回文单词,文本数据如下:PW3爱图古源码汇

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.

注意:PW3爱图古源码汇

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

2. 分析

本次任务的思路很简单,基本步骤如下:PW3爱图古源码汇

第一步:读入文本数据,然后去掉文本中的换行符;

第二步:去掉第一步处理后的文本中的标点符号,这里使用正则表达式将文本中的单词保留,从而达到去标点符号的目的。之后使用一个列表存入每一行去掉标点之后的文本。

第三步:根据预处理之后的文本统计词频,因为一篇文本里面可能有很多重复的单词,那么只须判断文本构成的子典中的单词是否是回文单词即可。

第四步:遍历字典中的键,并判断是否是回文单词,具体实现方法见代码;

第五步:根据找到的回文单词计算文本中回文单词的比例;

3. 代码

import re

from collections import Counter

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

total_palindrome_word += c[word]

print( 回文单词的比例为:{:.3f} .format(total_palindrome_word / len(processed_text)))

免责申明:注册会员下载资源前请仔细阅读《本站条款》

本站所有资源都来自于网络,版权归原创者所有!本站网络资源不提供任何售后和服务,也不承担任何法律责任,如有侵权或危害了你的利益,请出示相应的资质证明并联系站长,我们将马上予以删除。

来源:爱图古源码网(站长邮箱:a13879801652@foxmail.com 转载请保留出处!)

本文地址:https://www.aitgoo.com/wangluo/29891.html

相关关键词:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值