卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章20361本站已运行347

将以下内容翻译为中文:Python程序将单数转换为复数

<?xml encoding="utf-8" ?>

将以下内容翻译为中文:Python程序将单数转换为复数

在本文中,我们将学习一个将单数转换为复数的 Python 程序。

假设给你一个单数单词,我们必须使用各种 python 方法将其转换为复数。

使用的方法

以下是完成此任务的各种方法 -

  • 使用正则表达式模块

  • 使用 NLTK 和 Pattern-en 包

  • 使用 Textblob 模块

  • 使用inflect模块

方法一:使用正则表达式模块

Python中的正则表达式模块根据指定的模式搜索一个字符串或一组字符串。如果您的 Python 中尚不存在该模块,则必须安装它,该模块通常随 Python 一起预安装。

示例

以下程序使用正则表达式模块通过指定适当的正则表达式模式返回给定单词的复数 -

# importing re(regex) module
from re import *
# input word to be pluralized
inputWord = "plant"
print("The plural of the word {", inputWord, "} is:")
# checking whether the input word is ending with s,x,z or is
# ending with ah, eh, ih, oh, uh, dh, gh, kh, ph, rh, th
# with the regex pattern
if search('[sxz]$', inputWord) or search('[^aeioudgkprt]h$', inputWord):
   # If it is true, then get the pluraof the inputWord by adding "es" in end
   print(sub('$', 'es', inputWord))
# checking whether the input word is ending with ay,ey,iy,oy,uy
# with the other regex pattern
elif search('[aeiou]y$', inputWord):
   # If it is true, then get the plural
   # of the word by removing 'y' from the end and adding ies to end
      print(sub('y$', 'ies', inputWord))
# Else add it just "s" to the word at the end to make it plural
else:
   print(inputWord + 's')

输出

执行时,上述程序将生成以下输出 -

The plural of the word { plant } is:
plants

但是,使用此正则表达式方法获取复数的效率不是很高,因为它可以正确地对某些单词进行复数化,而在其他情况下却无法做到这一点。因此,我们将寻找更有效的技术来确定单词的复数。

方法2:使用NLTK和Pattern-en包

NLTK模块

NLTK 模块创建 Python 应用程序来处理人类语言数据,并提供与语料库和词汇资源的简单接口。

模式模块

模式模块是一个开源Python模块,用于执行各种自然语言处理操作。该模块可用于各种任务,包括文本处理和数据挖掘。

使用以下命令安装模式模块

pip install pattern

使用以下代码下载所需的 nltk 数据 -

# importing nltk module
import nltk
nltk.download('omw-1.4')
# downloading the NLTK data to run the en function of the package module 
nltk.download('popular')

示例

以下程序使用 NLTK 和 Pattern-en 包返回给定单词的复数形式 -

# importing pluralize from pattern-en module
from pattern.en import pluralize
# passing the word to be pluralized as an argument to the 
# pluralize() function to make the word plural
print(pluralize('lion'))

输出

执行时,上述程序将生成以下输出 -

lions

方法3:使用Textblob模块

Textblob Python 模块用于处理文本数据,简称 Textblob 模块。这是涉及自然语言处理问题的最简单的模块之一。

以下命令可用于下载该模块。除了texblob模块之外,还需要安装textblob模块的语料库功能。

使用以下命令安装textblob模块 -

pip install textblob 

示例

以下程序使用 Textblob 模块返回给定单词的复数形式 -

# importing TextBlob function from textblob module 
from textblob import TextBlob
# passing the word to be pluralized as an argument to the TextBlob() function 
blobWord = TextBlob('flower')
 
# printing the plural word of the given blob word using the pluralize() function
print(blobWord.words.pluralize())

输出

执行时,上述程序将生成以下输出 -

['flowers']

方法4:使用inflect模块

变形模块

Python 中的 Inflect 模块用于创建复数名词、单数名词、序数词和不定冠词,以及将数字转换为单词。可以使用以下命令来安装该模块。

安装 -

pip install inflect

算法(步骤)

以下是执行所需任务所需遵循的算法/步骤。 -

  • 使用 import 关键字导入 inflect 模块。

  • 使用engine()函数设置inflect模块的引擎/源

  • 使用 pluralize() 函数通过将输入单词作为参数传递给它来获取给定单词的复数形式,并打印结果单词。

  • 单数名词通过plural()函数转换为复数名词,其功能非常合适。

示例

以下程序使用 inflect 模块返回给定单词的复数形式 -

# importing inflect module
import inflect
m = inflect.engine()
# input word to be pluralized 
word = 'dog'
# Pass the word to be pluralized as an argument to the plural() 
# function to make the word plural.
print("The plural form of 'dog' is: ", m.plural(word))

输出

执行时,上述程序将生成以下输出 -

The plural form of 'dog' is:  dogs

结论

在本文中,我们介绍了将给定单词从单数更改为复数的四种不同方法。我们学习了如何使用 pip 命令来安装模块。此外,我们还学习了如何从 nltk 下载数据。

卓越飞翔博客
上一篇: 在C语言中,kbhit指的是检测键盘是否有输入的函数
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏