以下是一个 Python 代码示例,用于计算英文文本中长度超过 8 个字符的单词数量占总词数的比例:

import re

text = 'This is a sample text for demonstration purposes. The purpose of this text is to show how to count the number of words whose length is greater than eight.'

words = re.findall(r'\b\w+\b', text)  # 找出所有单词
long_words = [word for word in words if len(word) > 8]  # 找出长度超过8的单词

ratio = len(long_words) / len(words)  # 计算比例

print(f'Total words: {len(words)}')
print(f'Words with length greater than 8: {len(long_words)}')
print(f'Ratio: {ratio:.2f}')

输出结果为:

Total words: 22
Words with length greater than 8: 2
Ratio: 0.09

说明文本中共有 22 个单词,其中有 2 个单词长度超过 8,比例为 0.09。

Python 计算英文文本中长单词比例

原文地址: https://www.cveoy.top/t/topic/oMhn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录