Search code examples
pythonpython-3.xspell-checkingcapitalizationautocapitalize

Is there a python spelling correction library that corrects capitalisation?


I have strings that are spelled correctly but in all lower case (except for the first character), that I would like to correct for capitalisation (in English - so basically just names of things...). I tried pyspellcheck, autocorrect and symspellpy, which do not consider capitalisation afaik.

So for example the string 'And then we went to see frank from england to have a beer with him.' should be corrected to 'And then we went to see Frank from England to have a beer with him.'.

Do you know any library that can do that?


Solution

  • You can do it with spaCy:

    import spacy
    nlp=spacy.load('en_core_web_md')
    
    
    def capitalize_ent(text):
        title_text=text.title()
        print(text)
        doc=nlp(title_text)
        words=[]
        for x in doc:
            if nlp(x.text).ents:
                words.append(x.text)
        for word in words:
            text=text.replace(word.lower(),word)
        return text
                
    

    Don't forget to download the spaCy language model:

    python -m spacy download en_core_web_md