python判断英文
生活随笔
收集整理的這篇文章主要介紹了
python判断英文
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#!/usr/bin/python
#-*-coding:utf-8-*-
__author__ = 'lxw'
#dictionary.txt的下載地址為:http://pan.baidu.com/s/1i4JL4RN
import string
def loadEnglish():#這個函數(shù)加載字典文件dicFile = open('dictionary.txt')english_word= {}for word in dicFile.read().split('\n'):english_word[word]=Nonereturn english_wordENGLISH_WORDS = loadEnglish()def removenonLetters(message):#移除非字母字符,包括標(biāo)點符號和數(shù)字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):#得到一段字符串中單詞的個數(shù)message = message.upper()message = removenonLetters(message)match = 0words = message.split()if len(words)==0: return 0.0for word in words:if word in ENGLISH_WORDS.keys():match+=1return float(match)/len(words)def isEnglish(message,wordPercentage=20,letterPercentage=85):
#調(diào)用這個函數(shù)判斷一段字符串是否問一段英文.如果是則返回TruewordMatch = getEnglishCount(message)*100 >= wordPercentageremovedMsg = removenonLetters(message)letterMatch = (len(removedMsg)/float(len(message)))*100>=letterPercentagereturn wordMatch and letterMatch
總結(jié)
以上是生活随笔為你收集整理的python判断英文的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python操作xlsx文档
- 下一篇: 用angr解二进制题目1