本文整理汇总了Python中enchant.checker.SpellChecker类的典型用法代码示例。如果您正苦于以下问题:Python SpellChecker类的具体用法?Python SpellChecker怎么用?Python SpellChecker使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SpellChecker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filter_ngrams
def filter_ngrams(terms, spelling=False, singletons=True, contains_numeric=False, contains_alpha=False, contains_non_alphanumeric=False):
"""
Filter n-grams by a variety of features
"""
chkr = SpellChecker("en_US")
print(len(terms), "n-grams before filter")
if spelling:
for k in list(terms.keys()):
chkr.set_text(k)
errors = set()
for err in chkr:
errors.add(err.word)
if len(errors) > 0:
del terms[k]
if singletons:
for k,v in list(terms.items()):
if len(v) == 1:
del terms[k]
if contains_numeric:
for k in list(terms.keys()):
if re.search("[^0-9]",k):
del terms[k]
if contains_alpha:
for k in list(terms.keys()):
if re.search("[^a-z]",k):
del terms[k]
if contains_non_alphanumeric:
for k in list(terms.keys()):
if re.search("[^[:alnum:]]",k):
del terms[k]
print(len(terms), "n-grams after filter")
return terms
示例2: CorrectBot
def CorrectBot(interface,command,args,messagetype):
"!correct [n=previous] [words=all of them] - Try to correct the words in the nth message before this one."
words=''
chkr = SpellChecker("en_UK",filters=[EmailFilter,URLFilter])
try:
a=args.split()
n=int(args.split()[0])
if len(a)>1:
words=args.partition(" ")[2]
else:
words=interface.LastMessages[n].Body
except:
n=0
words=interface.LastMessages[0].Body
if not interface.LastMessages[n].IsEditable:
SpellBot(interface,'spell',words,messagetype,onlyerror=True)
return
text=interface.LastMessages[n].Body
origtext = text
chkr.set_text(words)
for err in chkr:
w = err.suggest()#Spell(word[0])
if w:
if w!=err.word:
text = text.replace(err.word,w[0])
if origtext!=text:
interface.LastMessages[n].Body=text
示例3: spelling
def spelling(file_name, contents, language="en_US"):
"""
You give it a file_name and the contents of that file and it tells you
if it's spelled correctly. The reason you give it contents is that you
will typically run a template through the render process, so spelling
can't just load a file and check it.
It assumes you have PyEnchant installed correctly and configured
in your config/testing.py file. Use "salmon spell" to make sure it
works right.
"""
try:
from enchant.checker import SpellChecker
from enchant.tokenize import EmailFilter, URLFilter
except ImportError:
print "Failed to load PyEnchant. Make sure it's installed and salmon spell works."
return True
failures = 0
chkr = SpellChecker(language, filters=[EmailFilter, URLFilter])
chkr.set_text(contents)
for err in chkr:
print "%s: %s \t %r" % (file_name, err.word, contents[err.wordpos - 20:err.wordpos + 20])
failures += 1
if failures:
print "You have %d spelling errors in %s. Run salmon spell.." % (failures, file_name)
return False
else:
return True
示例4: spellcheck_text
def spellcheck_text(self):
"""Spellcheckes text and saves spellchecked text in
self.text.
"""
# Variable declaration
errors = list() # spelling errors
chkr = SpellChecker('de_DE') # spellchecker for whole text
dic = enchant.Dict('de_DE') # enchant dict
# Run spellchecker over whole text
chkr.set_text(self.text)
# Loop over every error
for err in chkr:
# Save error in errors list
errors.append(err.word)
# There are errors
if len(errors) > 0:
# Replace errors with proper word
for error in errors:
# Check if there is a substitute
try:
self.text = self.text.replace(error, dic.suggest(error)[0])
except IndexError:
pass
示例5: matchlocations
def matchlocations(line):
from enchant.checker import SpellChecker
chkr = SpellChecker("en_US")
global sdx
global sdx4
global sdx3
global places
i=0
chkr.set_text(line)
for err in chkr:
#print "error",err.word
toprint = err.word
word = err.word.upper()
mind = 4
replace = []
flag = False
soundFlag = False
noFlag = False
if word in places:
line = line.replace(toprint,"<loc>"+word.lower()+"</loc>")
else:
for place in places:
dist = minEditDist(word,place)
if dist<mind:
replace=[]
replace.append(place)
mind = dist
flag = True
else:
if dist == mind:
replace.append(place)
flag == True
if flag == True and len(word) > mind:
if mind ==1 and len(replace)==1:
line = line.replace(toprint,"<loc>"+replace[0].lower()+"</loc>")
else:
if(soundex2(word,4) in sdx4 and len(word)>3):
line = line.replace(toprint,"<loc>"+sdx4[soundex2(word,4)].lower()+"</loc>")
elif(soundex2(word,3) in sdx3 and len(word)>3):
line = line.replace(toprint,"<loc>"+sdx3[soundex2(word,3)].lower()+"</loc>")
elif(dm(word)[0] in sdx and len(word)>3):
line = line.replace(toprint,"<loc>"+sdx[dm(word)[0]].lower()+"</loc>")
else:
if len(replace) == 1:
line = line.replace(toprint,"<loc>"+replace[0].lower()+"</loc>")
else:
#print replace
for ele in replace:
if(dm(ele)[0] == dm(toprint)[0]):
line = line.replace(toprint,"<loc>"+ele.lower()+"</loc>")
soundFlag = True
break
if soundFlag == False:
line = line.replace(toprint,"<loc>"+replace[0].lower()+"</loc>")
else:
if (dm(word)[0] in sdx and len(word)>3):
line = line.replace(toprint,"<loc>"+sdx[dm(word)[0]].lower()+"</loc>")
line = line.replace('\r','')
print line
示例6: __init__
def __init__(self, text, dictionary):
super(spellChecker, self).__init__()
log.debug("Creating the SpellChecker object. Dictionary: %s" % (dictionary,))
self.active = True
try:
if config.app["app-settings"]["language"] == "system":
log.debug("Using the system language")
self.checker = SpellChecker(filters=[twitterFilter.TwitterFilter, tokenize.EmailFilter, tokenize.URLFilter])
else:
log.debug("Using language: %s" % (languageHandler.getLanguage(),))
self.checker = SpellChecker(languageHandler.getLanguage(), filters=[twitterFilter.TwitterFilter, tokenize.EmailFilter, tokenize.URLFilter])
self.checker.set_text(text)
except DictNotFoundError:
log.exception("Dictionary for language %s not found." % (dictionary,))
wx_ui.dict_not_found_error()
self.active = False
if self.active == True:
log.debug("Creating dialog...")
self.dialog = wx_ui.spellCheckerDialog()
widgetUtils.connect_event(self.dialog.ignore, widgetUtils.BUTTON_PRESSED, self.ignore)
widgetUtils.connect_event(self.dialog.ignoreAll, widgetUtils.BUTTON_PRESSED, self.ignoreAll)
widgetUtils.connect_event(self.dialog.replace, widgetUtils.BUTTON_PRESSED, self.replace)
widgetUtils.connect_event(self.dialog.replaceAll, widgetUtils.BUTTON_PRESSED, self.replaceAll)
self.check()
self.dialog.get_response()
self.fixed_text = self.checker.get_text()
示例7: decipher
def decipher(ciphertext):
checker = SpellChecker("en_US")
best_key = -1
least_num_errors = len(ciphertext)
for i in range(0, 26):
plaintext = ''
for j in range(0, len(ciphertext)):
if ciphertext[j].isalpha():
pos = ord(ciphertext[j])
offset = 97
if ciphertext[j].isupper():
offset = 65
new_pos = (pos - offset + 26 - i) % 26 + offset
plaintext += unichr(new_pos)
else:
plaintext += ciphertext[j]
checker.set_text(plaintext)
num_errors = 0
for err in checker:
num_errors = num_errors + 1
if num_errors < least_num_errors:
least_num_errors = num_errors
best_key = i
words = plaintext.split()
en_words = len(words) - num_errors
print("%i: %s English words: %i" % (i, plaintext, en_words))
return "%s %i" % ("The key is most likely: ", best_key)
示例8: spell
def spell(text):
"""spell <word/sentence> -- Check spelling of a word or sentence."""
if len(text.split(" ")) > 1:
# input is a sentence
checker = SpellChecker(en_dict, filters=[EmailFilter, URLFilter])
checker.set_text(text)
offset = 0
for err in checker:
# find the location of the incorrect word
start = err.wordpos + offset
finish = start + len(err.word)
# get some suggestions for it
suggestions = err.suggest()
s_string = '/'.join(suggestions[:3])
s_string = "\x02{}\x02".format(s_string)
# calculate the offset for the next word
offset = (offset + len(s_string)) - len(err.word)
# replace the word with the suggestions
text = text[:start] + s_string + text[finish:]
return text
else:
# input is a word
is_correct = en_dict.check(text)
suggestions = en_dict.suggest(text)
s_string = ', '.join(suggestions[:10])
if is_correct:
return '"{}" appears to be \x02valid\x02! ' \
'(suggestions: {})'.format(text, s_string)
else:
return '"{}" appears to be \x02invalid\x02! ' \
'(suggestions: {})'.format(text, s_string)
示例9: testAccuracy
def testAccuracy(inputString):
errors = 0
chkr = SpellChecker("en_US")
chkr.set_text(inputString)
for err in chkr:
errors += 1
return errors
示例10: correct
def correct(text):
chkr = SpellChecker("en_US")
chkr.set_text(text)
for err in chkr:
sug = err.suggest()
if sug:
err.replace(sug[0])
return chkr.get_text()
示例11: spellcheck
def spellcheck(sentence):
checker = SpellChecker("en_US")
checker.set_text(sentence)
for error in checker:
for suggestion in error.suggest():
if error.word.replace(' ','') == suggestion.replace(' ',''):
error.replace(suggestion)
break
return checker.get_text()
示例12: Errores
def Errores(self):
terrores = 0
texto = open(self.archivo, 'r')
chkr = SpellChecker("es_ES")
chkr.set_text(texto.readline())
for err in chkr:
terrores+=1
terrores=str(terrores)
self.ui.errores.setText(terrores)
示例13: spellcheck
def spellcheck(self):
errs = 0
words = []
chkr = SpellChecker("en_US")
chkr.set_text(self.text)
for err in chkr:
errs += 1
words.append(err.word)
return [errs, words]
示例14: user_stats
def user_stats(messages, usermap):
"""Keys: (posts, likes_recieved, likes_given, wordcount, images, misspellings, kicked)"""
stats = {}
checker = SpellChecker('en_US')
for user_id in usermap:
stats[usermap[user_id]] = {
'posts': [],
'likes_recieved': 0,
'likes_given': 0,
'wordcount': 0,
'images': 0,
'misspellings': [],
'kicked': 0,
'been_kicked': 0 }
current_names = {} # map user id to alias at the time of each message
for m in reversed(messages):
current_names[m['sender_id']] = m['name']
if m['user_id'] == 'system':
if m['text'] is not None:
if ' changed name to ' in m['text']:
s = m['text'].split(' changed name to ')
for uid in current_names:
if current_names[uid] == s[0]:
current_names[uid] = s[1]
elif ' removed ' in m['text']:
s = m['text'][:-16].split(' removed ')
remover = 0
removed = 0
for uid in current_names:
if current_names[uid] == s[0]: remover = uid
if current_names[uid] == s[1]: removed = uid
if remover != 0 and removed != 0:
stats[usermap[remover]]['kicked'] += 1
stats[usermap[removed]]['been_kicked'] += 1
name = usermap[m['sender_id']]
stats[name]['posts'].append(m)
stats[name]['likes_recieved'] += len(m['favorited_by'])
for liker in m['favorited_by']:
try:
likername = usermap[liker]
stats[likername]['likes_given'] += 1
except KeyError:
pass
stats[name]['images'] += 1 if len(m['attachments']) > 0 else 0
if m['text'] is not None:
stats[name]['wordcount'] += len(m['text'].split(' '))
checker.set_text(m['text'])
stats[name]['misspellings'] += [error.word for error in list(checker)]
del stats['GroupMe']
del stats['GroupMe Calendar']
del stats['Annie Hughey']
del stats['Nicole Vergara']
return stats
示例15: has_error
def has_error(file_path):
"""return boolean indicating whether the file specified by the
file_path contains spelling mistakes
"""
with open(file_path, "r") as file_to_check:
data = file_to_check.read()
checker = SpellChecker("en_US")
checker.set_text(data)
for err in checker:
return True
return False