0

I want to judge if all words in a string are proper English words. I am using a function to check it:

is_word_english(word: str)

Returns a boolean value

The function can only process a word so I use split()to take out spaces between words. Here is an example:

a = ('Apple Banana')
b = a.split() 
b
['Apple', 'Banana']
b[1]
'Banana'
is_word_english(b[1])
False
is_word_english('banana')
True
jalazbe
  • 1,801
  • 3
  • 19
  • 40
Terry Yu
  • 3
  • 4
  • 2
    Try `is_word_english(b[1].lower())`. Your `is_word_english` function is likely case-sensitive. – blhsing Aug 21 '18 at 11:29
  • as @blhsing `is_word_english(b[1].lower())`this should work since what you do is make sure all words are on lowercase. It will, however, not work for some words that require capital letters (example English, Spanish, .. or UN .. ) – jalazbe Aug 21 '18 at 11:37
  • 1
    Remmember that python is case sensitive. Banana is not equal to banana. – jalazbe Aug 21 '18 at 11:53

1 Answers1

0

If the output for banana and Banana differs it means is_word_english is case-sensitive and you'll have to use str.lower first:

a = ('Apple Banana')
words = a.split()
lower_words = map(str.lower, words) # this part is missing in your code?
is_all_english = all(map(is_word_english, lower_words))

If is_word_english handles ONLY lowercase words (seems reasonable) you can wrap it for convenience:

def is_word_english_ci(word):
    return is_word_english(word.lower())
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88