forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetecting_english_programmatically.py
62 lines (48 loc) · 1.68 KB
/
detecting_english_programmatically.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
importos
fromstringimportascii_letters
LETTERS_AND_SPACE=ascii_letters+" \t\n"
defload_dictionary() ->dict[str, None]:
path=os.path.split(os.path.realpath(__file__))
english_words: dict[str, None] = {}
withopen(path[0] +"/dictionary.txt") asdictionary_file:
forwordindictionary_file.read().split("\n"):
english_words[word] =None
returnenglish_words
ENGLISH_WORDS=load_dictionary()
defget_english_count(message: str) ->float:
message=message.upper()
message=remove_non_letters(message)
possible_words=message.split()
matches=len([wordforwordinpossible_wordsifwordinENGLISH_WORDS])
returnfloat(matches) /len(possible_words)
defremove_non_letters(message: str) ->str:
"""
>>> remove_non_letters("Hi! how are you?")
'Hi how are you'
>>> remove_non_letters("P^y%t)h@o*n")
'Python'
>>> remove_non_letters("1+1=2")
''
>>> remove_non_letters("www.google.com/")
'wwwgooglecom'
>>> remove_non_letters("")
''
"""
return"".join(symbolforsymbolinmessageifsymbolinLETTERS_AND_SPACE)
defis_english(
message: str, word_percentage: int=20, letter_percentage: int=85
) ->bool:
"""
>>> is_english('Hello World')
True
>>> is_english('llold HorWd')
False
"""
words_match=get_english_count(message) *100>=word_percentage
num_letters=len(remove_non_letters(message))
message_letters_percentage= (float(num_letters) /len(message)) *100
letters_match=message_letters_percentage>=letter_percentage
returnwords_matchandletters_match
if__name__=="__main__":
importdoctest
doctest.testmod()