Request For error correction in source code

43 views
Skip to first unread message

abhishek tiwari

unread,
Jun 6, 2017, 9:31:27 AM6/6/17
to Python GCU Forum


For those who cant open file

UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'

def loadDictionary():
    dictionaryFile = open('dictionary.txt')
    englishWords = {}
    for word in dictionaryFile.read().split('\n'):
        englishWords[word] = None
    dictionaryFile.close()
    return englishWords

ENGLISH_WORDS = loadDictionary()


def getEnglishCount(message):
    message = message.upper()
    message = removeNonLetters(message)
    possibleWords = message.split()

    if possibleWords ==[]:
         return 0.0

    matches=0
     
    for word in possibleWords:
        if word in ENGLISH_WORDS:
            matches += 1
    return float(matches) / len(possibleWords)
    


def removeNonLetters(message):
    lettersOnly = []
    for symbol in message:
        if symbol in LETTERS_AND_SPACE:
            lettersOnly.append(symbol)
    return ''.join(lettersOnly)


def isEnglish(message, wordPercentage=30, letterPercentage=90):
 # By default, 20% of the words must exist in the dictionary file, and
 # 85% of all the characters in the message must be letters or spaces
 # (not punctuation or numbers).
    wordsMatch = getEnglishCount(message) * 100
    numLetters = len(removeNonLetters(message))
    messageLettersPercentage = float(numLetters) / len(message) * 100
    lettersMatch = messageLettersPercentage
    if lettersMatch >= letterPercentage and wordsMatch >= wordPercentage:
        return True
    else:
        return False
    
    

detectenglish.py
Reply all
Reply to author
Forward
0 new messages