2012-08-30 23 views

回答

10

結帳inflect 0.2.4庫。

活用0.2.4

正確生成複數,單數名詞,序數,不定 物品;將數字轉換爲單詞

+1

這很酷。我不知道。 – David

+5

@Ashwini:inflect.py可以*生成*複數,但它可以*檢測*複數? – Hugo

2

如果您的問題在英語語句的上下文中是孤立的單詞或單詞,您不會說出問題。

例如「綿羊」可以是單個或複數。但是:

羊在外地

是單數和

羊在外地

是複數。

對於後者,你需要一個詞性標註器,它將識別名詞在句子中的角色。有許多自由和商業的和維基百科有an excellent list。 NLTK可能是Python的自然選擇。

如果你只有孤立的單詞,你可以做的最好的就是參考很多字典(如 Wordnet這將表明單數和複數形式的名詞)。

5

Ashwini提到有用的inflect庫,但沒有解釋如何檢查給定的單詞是複數還是單數形式。

如果你知道的話要麼是單數還是複數,你可以使用:

singular_noun(word) 

這將返回False如果字是不是一個複數,所以你的話在理論上應爲單數。

請注意我的例子中涉及到的經典複數,可以是單數或複數的形式,以及它將通常返回False爲無法識別的形式的缺點。

import inflect 
inflect = inflect.engine() 

english_words = ["hat", "hats", 
       "hero", "heroes", 
       "cherry", "cherries", 
       "dish", "dishes", 
       "stadium", "stadia", "stadiums", 
       "mitochondrion", "mitochondria", 
       "sheep", "a sheep", "the sheep", 
       "whjkjhkjh", "msipelling"] 

for en in english_words: 
    if inflect.singular_noun(en) is False: 
     print (en, "is singular") 
    else: 
     print (en, "is plural") 

>>> 
hat is singular 
hats is plural 
hero is singular 
heroes is plural 
cherry is singular 
cherries is plural 
dish is singular 
dishes is plural 
stadium is singular 
stadia is singular 
stadiums is plural 
mitochondrion is singular 
mitochondria is singular 
sheep is plural 
a sheep is plural 
the sheep is plural 
whjkjhkjh is singular 
+0

'some_boolean_value是False'是反模式,請使用'not some_boolean_value' –

相關問題