Added test cases

This commit is contained in:
Harshil Darji
2016-08-02 23:16:55 +05:30
parent 525b945446
commit f68690f296
5 changed files with 127 additions and 47 deletions

View File

@@ -1,8 +1,11 @@
import os
UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
def loadDictionary():
dictionaryFile = open('Dictionary.txt')
path = os.path.split(os.path.realpath(__file__))
dictionaryFile = open(path[0] + '\Dictionary.txt')
englishWords = {}
for word in dictionaryFile.read().split('\n'):
englishWords[word] = None
@@ -34,8 +37,19 @@ def removeNonLetters(message):
return ''.join(lettersOnly)
def isEnglish(message, wordPercentage = 20, letterPercentage = 85):
"""
>>> isEnglish('Hello World')
True
>>> isEnglish('llold HorWd')
False
"""
wordsMatch = getEnglishCount(message) * 100 >= wordPercentage
numLetters = len(removeNonLetters(message))
messageLettersPercentage = (float(numLetters) / len(message)) * 100
lettersMatch = messageLettersPercentage >= letterPercentage
return wordsMatch and lettersMatch
import doctest
doctest.testmod()