切片字符串以满足 if 条件

时间:2021-01-21 22:01:02

标签: python string dictionary debugging

我有一系列条件使单词复数。如果在文件中找到它们的条件,以及如果在文件中找不到它们的条件。我已经(我认为)确定了我的问题,但不确定如何解决它。我确定它与我如何切片字符串以匹配条件有关。

我相信您可以在 else: 之前阅读所有内容,所以我会跳到前面。 以下所有 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'}

食物、高速公路、演示文稿和 covid-19 是错误的。它们不在文件中,因此应以“s”结尾

1 个答案:

答案 0 :(得分:1)

使用这个

elif (word [-1:-2].lower() == 's' + 'h') or (word [-1:-2].lower() == 'c' + 'h'):