本文整理汇总了Python中enchant.checker.SpellChecker.set_text方法的典型用法代码示例。如果您正苦于以下问题:Python SpellChecker.set_text方法的具体用法?Python SpellChecker.set_text怎么用?Python SpellChecker.set_text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类enchant.checker.SpellChecker
的用法示例。
在下文中一共展示了SpellChecker.set_text方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spelling
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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
示例2: testAccuracy
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
def testAccuracy(inputString):
errors = 0
chkr = SpellChecker("en_US")
chkr.set_text(inputString)
for err in chkr:
errors += 1
return errors
示例3: spellcheck_text
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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
示例4: filter_ngrams
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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
示例5: BeardBotModule
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
class BeardBotModule(ModuleBase):
"""Checks the spelling of all words in the channel.
Add a word to the dictionary (addressed):
* [word] is a word!
Add a word in reply to the bot's taunts (addressed):
* Yes I [expletive] do
"""
def __init__(self, *args, **kwargs):
ModuleBase.__init__(self, *args, **kwargs)
self.spell_checker = SpellChecker("en_UK")
self.last_word = None
def on_channel_message(self, source_name, source_host, message):
self.spell_checker.set_text(message)
for error in self.spell_checker:
self.bot.say("%s? You call that a word?" % error.word)
self.last_word = error.word
def on_addressed_message(self, source_name, source_host, message):
is_a_word_match = is_a_word.search(message)
yes_i_do_match = yes_i_do.search(message)
if is_a_word_match:
word = match.group(1)
self.bot.say("You're right, %s is a word :(" % word)
self.spell_checker.add(word)
elif yes_i_do_match and self.last_word:
self.spell_checker.add(self.last_word)
self.bot.say("Yes Master...")
示例6: spell
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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)
示例7: spellChecker
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
class spellChecker(object):
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(languageHandler.curLang, filters=[tokenize.EmailFilter, tokenize.URLFilter])
else:
log.debug("Using language: %s" % (languageHandler.getLanguage(),))
self.checker = SpellChecker(languageHandler.curLang, filters=[tokenize.EmailFilter, tokenize.URLFilter])
self.checker.set_text(text)
except DictNotFoundError:
print "no dict"
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()
def check(self):
try:
self.checker.next()
textToSay = _(u"Misspelled word: %s") % (self.checker.word,)
context = u"... %s %s %s" % (self.checker.leading_context(10), self.checker.word, self.checker.trailing_context(10))
self.dialog.set_title(textToSay)
output.speak(textToSay)
self.dialog.set_word_and_suggestions(word=self.checker.word, context=context, suggestions=self.checker.suggest())
except StopIteration:
log.debug("Process finished.")
wx_ui.finished()
self.dialog.Destroy()
def ignore(self, ev):
self.check()
def ignoreAll(self, ev):
self.checker.ignore_always(word=self.checker.word)
self.check()
def replace(self, ev):
self.checker.replace(self.dialog.get_selected_suggestion())
self.check()
def replaceAll(self, ev):
self.checker.replace_always(self.dialog.get_selected_suggestion())
self.check()
def clean(self):
if hasattr(self, "dialog"):
self.dialog.Destroy()
示例8: matchlocations
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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
示例9: CorrectBot
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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
示例10: decipher
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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)
示例11: correct
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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()
示例12: Errores
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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()
示例14: spellcheck
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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]
示例15: has_error
# 需要导入模块: from enchant.checker import SpellChecker [as 别名]
# 或者: from enchant.checker.SpellChecker import set_text [as 别名]
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