mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Refactoring the syntax using list comprehension (#7749)
* Refactoring the syntax using list comprehension * Update detecting_english_programmatically.py * Update detecting_english_programmatically.py Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
61eedc16c3
commit
de3271ec80
@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
|
from string import ascii_letters
|
||||||
|
|
||||||
UPPERLETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
LETTERS_AND_SPACE = ascii_letters + " \t\n"
|
||||||
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + " \t\n"
|
|
||||||
|
|
||||||
|
|
||||||
def load_dictionary() -> dict[str, None]:
|
def load_dictionary() -> dict[str, None]:
|
||||||
@ -20,24 +20,12 @@ def get_english_count(message: str) -> float:
|
|||||||
message = message.upper()
|
message = message.upper()
|
||||||
message = remove_non_letters(message)
|
message = remove_non_letters(message)
|
||||||
possible_words = message.split()
|
possible_words = message.split()
|
||||||
|
matches = len([word for word in possible_words if word in ENGLISH_WORDS])
|
||||||
if possible_words == []:
|
|
||||||
return 0.0
|
|
||||||
|
|
||||||
matches = 0
|
|
||||||
for word in possible_words:
|
|
||||||
if word in ENGLISH_WORDS:
|
|
||||||
matches += 1
|
|
||||||
|
|
||||||
return float(matches) / len(possible_words)
|
return float(matches) / len(possible_words)
|
||||||
|
|
||||||
|
|
||||||
def remove_non_letters(message: str) -> str:
|
def remove_non_letters(message: str) -> str:
|
||||||
letters_only = []
|
return "".join(symbol for symbol in message if symbol in LETTERS_AND_SPACE)
|
||||||
for symbol in message:
|
|
||||||
if symbol in LETTERS_AND_SPACE:
|
|
||||||
letters_only.append(symbol)
|
|
||||||
return "".join(letters_only)
|
|
||||||
|
|
||||||
|
|
||||||
def is_english(
|
def is_english(
|
||||||
|
Loading…
Reference in New Issue
Block a user