Python程式:將單數轉換為複數
在本文中,我們將學習一個Python程式,用於將單數轉換為複數。
假設您給定一個單數單詞,我們必須使用各種Python方法將其轉換為複數。
使用方法
以下是完成此任務的各種方法:
使用regex模組
使用NLTK和Pattern-en包
使用Textblob模組
使用inflect模組
方法1:使用regex模組
Python中的regex模組根據指定的模式搜尋字串或字串組。如果它不存在於您的Python中,您必須安裝它,此模組通常預裝在Python中。
示例
以下程式透過指定適當的正則表示式模式,使用regex模組返回給定單詞的複數:
# 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
但是,這種regex方法對於獲取複數效率不高,因為它在某些情況下可以正確地將一些單詞變為複數,而在其他情況下則會失敗。因此,我們將尋找更有效的方法來確定單詞的複數。
方法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模組。對於涉及自然語言處理的問題,這是最簡單的模組之一。
可以使用以下命令下載此模組。除了texblob模組外,您還需要安裝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下載資料。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP