Python 将单数转换为复数
在本文中,我们将学习一个Python程序,将单数转换为复数。
假设给定一个单数词,我们必须使用各种Python方法将其转换为复数。
使用的方法
以下是完成此任务的各种方法:
- 使用正则表达式模块
-
使用NLTK和Pattern-en包
-
使用Textblob模块
-
使用inflect模块
方法1:使用正则表达式模块
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应用程序,并提供对语料库和词汇资源的简单接口。
Pattern模块
Pattern模块是一个开源的Python模块,用于执行各种自然语言处理操作。该模块可用于各种任务,包括文本处理和数据挖掘。
使用以下命令安装pattern模块
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模块。这是用于处理自然语言处理问题的最简单的模块之一。
以下命令可用于下载该模块。除了安装textblob模块外,您还需要安装textblob模块的corpora函数。
使用以下命令安装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模块
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下载数据。