3

How to perform spell check in spacy. Need to find number of worng words and suggestions if possible. I have tried this page

print('spell check doc_new')
print('-----------------')
print('contextual_spellCheck')
print(doc_new._.contextual_spellCheck)
print('performed_spellCheck')
print(doc_new._.performed_spellCheck)
print('score_spellCheck')
print(doc_new._.score_spellCheck)
print('outcome_spellCheck')
print(doc_new._.outcome_spellCheck)
print(nlp.pipe_names)
-----------------------
Output
contextual_spellCheck
True
performed_spellCheck
True
score_spellCheck
{bok: [('home', 0.25162), ('life', 0.10225), ('job', 0.0533), ('friend', 0.02805), ('place', 0.01896), ('world', 0.01788), ('apartment', 0.01757), ('family', 0.01643), ('house', 0.01583), ('boss', 0.01192)], universty: [('full', 0.24508), ('last', 0.14188), ('first', 0.11419), ('middle', 0.09706), ('real', 0.07817), ('given', 0.04026), ('birth', 0.03326), ('code', 0.0086), ('stage', 0.00846), ('maiden', 0.00798)], acc: [('David', 0.0059), ('Sam', 0.0052), ('Alex', 0.00496), ('James', 0.0047), ('I', 0.00424), ('Paul', 0.00419), ('Jack', 0.00384), ('John', 0.00381), ('Peter', 0.00347), ('Mark', 0.00344)]}
func
{bok: 'job', universty: 'first', acc: 'Jack'}
outcome_spellCheck
This is my new job. My first name is Jack.
['tok2vec', 'tagger', 'parser', 'ner', 'attribute_ruler', 'lemmatizer', 'contextual spellchecker'

Behaving differently

Any other alternative.

1

2 Answers 2

2

You can use doc._.suggestions_spellCheck:

import spacy
import contextualSpellCheck

nlp = spacy.load('en_core_web_sm')
contextualSpellCheck.add_to_pipe(nlp)
doc = nlp('This is my neww job. My firsrt neme is Jack.')

print(len(doc._.suggestions_spellCheck)) # => Number of errors: 3
print(doc._.suggestions_spellCheck)      # => {neww: 'new', firsrt: 'best', neme: 'name'}
print(doc._.outcome_spellCheck)          # => This is my new job. My best name is Jack.
2
  • 4
    It corrects "firsrt" to "best", which sounds very strange. Jun 17, 2022 at 8:41
  • 1
    @AlessandroDeSimone The package is based on BERT, so it could be that it does this because the two can be semantically synonymous depending on the context.
    – MattSt
    Dec 28, 2022 at 9:54
0
import spacy
import enchant

nlp = spacy.load("en_core_web_sm")
spellchecker = enchant.Dict("en_US")

text = "Ths sentnce hs sme splling msitkes."

doc = nlp(text)

corrected_text = []
for token in doc:
    if not spellchecker.check(token.text):
        corrected_text.append(spellchecker.suggest(token.text)[0])
    else:
        corrected_text.append(token.text)

print(" ".join(corrected_text))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.