Python 切片字符串以满足if条件
我有一系列的条件使单词多元化。如果在文件中找到它们的条件,以及如果在文件中找不到它们的条件。我已经(我想)确定了我的问题,但不确定如何解决它。我确信这与我如何分割字符串以匹配条件有关 我相信你可以在Python 切片字符串以满足if条件,python,string,dictionary,debugging,Python,String,Dictionary,Debugging,我有一系列的条件使单词多元化。如果在文件中找到它们的条件,以及如果在文件中找不到它们的条件。我已经(我想)确定了我的问题,但不确定如何解决它。我确信这与我如何分割字符串以匹配条件有关 我相信你可以在之前阅读所有内容:所以我会跳到前面。 以下所有else的目的是: **若单词不是复数,也不是专有名词,那个么:若单词以元音结尾,添加“s”。否则 -如果以“y”结尾,前面有一个辅音,请删除最后一个字母并添加“ies” -如果以“f”结尾,请删除最后一个字母并添加“ves” -如果以“sh”/“ch”/
之前阅读所有内容:
所以我会跳到前面。
以下所有else
的目的是:
**若单词不是复数,也不是专有名词,那个么:若单词以元音结尾,添加“s”。否则
-如果以“y”结尾,前面有一个辅音,请删除最后一个字母并添加“ies”
-如果以“f”结尾,请删除最后一个字母并添加“ves”
-如果以“sh”/“ch”/“z”结尾,则添加“es”
-如果以上都不适用,只需添加“s”**
逻辑必须遵循上述顺序,不能偏离
def pluralize(word):
proper_nouns= [line.strip() for line in open (filepath)] ### opens file when function is called, removes white spaces, and returns a list of the content within the file.
word_in_plural = '' ### placeholders for string values.
x = ''
vowels = ('aeiou') ### variables to apply conditional statements
consonants = ('bcdfghjklmnpqrstvwxyz')
dictionary = {'plural' : word_in_plural, 'status' : x} ### dictionary definition
if word == '':
dictionary ['plural'] = ''; dictionary ['status'] = 'empty_string'
elif word [-1] == 's':
dictionary ['plural'] = word; dictionary ['status'] = 'already_in_plural'
elif word.lower() in proper_nouns:
dictionary ['plural'] = word; dictionary ['status'] = 'proper_noun'
else:
if word [-1].lower() in vowels: #works
word = word + 's'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word [-2].lower() in consonants and word[-1] == 'y': #works
word = word [:-1] + 'ies'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word [-1].lower() == 'f': #works
word = word [:-1] + 'ves'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word [-1:-2].lower() == 's' + 'h' or 'c' + 'h': #does not
word = word + 'es'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word [:-1].lower() == 'z': #works
word = word + 'es'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word not in proper_nouns.lower(): #not sure
word = word + 's'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
输出:
failure --> {'plural': 'failures', 'status': 'success'}
----
food --> {'plural': 'foodes', 'status': 'success'}
----
Zulma --> {'plural': 'Zulma', 'status': 'proper_noun'}
----
injury --> {'plural': 'injuries', 'status': 'success'}
----
elf --> {'plural': 'elves', 'status': 'success'}
----
buzz --> {'plural': 'buzzes', 'status': 'success'}
----
computers --> {'plural': 'computers', 'status': 'already_in_plural'}
----
PCs --> {'plural': 'PCs', 'status': 'already_in_plural'}
----
--> {'plural': '', 'status': 'empty_string'}
----
highway --> {'plural': 'highwayes', 'status': 'success'}
----
presentation --> {'plural': 'presentationes', 'status': 'success'}
----
pouch --> {'plural': 'pouches', 'status': 'success'}
----
COVID-19 --> {'plural': 'COVID-19es', 'status': 'success'}
----
adam --> {'plural': 'adam', 'status': 'proper_noun'}
食品、公路、演讲和新冠病毒19都是错误的。它们不在文件中,因此应以“s”结尾使用此选项
elif (word [-1:-2].lower() == 's' + 'h') or (word [-1:-2].lower() == 'c' + 'h'):
这回答了你的问题吗?特别是关于您的
es
案例不具体,但我感谢您的帮助,但这会造成另一个问题:buzz-->{pollular':'buzzs','status':'success'}
邮袋-->{pollular':'backs','status':'success'}
现在没有达到预期效果