python判断英文

来源:互联网 发布:淘宝下架的宝贝在哪里找 编辑:程序博客网 时间:2024/06/14 06:35
#!/usr/bin/python#-*-coding:utf-8-*-__author__ = 'lxw'#dictionary.txt的下载地址为:http://pan.baidu.com/s/1i4JL4RNimport  stringdef     loadEnglish():#这个函数加载字典文件        dicFile = open('dictionary.txt')        english_word= {}        for word in dicFile.read().split('\n'):            english_word[word]=None        return english_wordENGLISH_WORDS = loadEnglish()def     removenonLetters(message):#移除非字母字符,包括标点符号和数字        letterOnly = []        allLetter = string.uppercase+string.lowercase+' \n\t'        for i in message:            if i in allLetter:                letterOnly.append(i)        return  "".join(letterOnly)def     getEnglishCount(message):#得到一段字符串中单词的个数        message = message.upper()        message = removenonLetters(message)        match = 0        words = message.split()        if len(words)==0: return 0.0        for word in words:            if word in ENGLISH_WORDS.keys():                match+=1        return float(match)/len(words)def     isEnglish(message,wordPercentage=20,letterPercentage=85):#调用这个函数判断一段字符串是否问一段英文.如果是则返回True        wordMatch = getEnglishCount(message)*100 >= wordPercentage        removedMsg = removenonLetters(message)        letterMatch = (len(removedMsg)/float(len(message)))*100>=letterPercentage        return  wordMatch and letterMatch