Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggestion choice method #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mdevolde
Copy link
Contributor

Sometimes LanguageTool gives several suggestions. In the case below, for this sentence in which I made a typo in the word ‘book’, I end up with a ton of suggestions. The correct suggestion is the third one.

import language_tool_python

def patch_text(text):
    with language_tool_python.LanguageTool('en-US') as tool:
        errors = tool.check(text)
    patched_text = language_tool_python.utils.correct(text, errors)
    return patched_text


if __name__ == '__main__':
    text = """
There is a bok on the table.
    """
    print(patch_text(text))
[Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['BOK', 'OK', 'book', 'box', ...], 'offsetInContext': 12, 'context': ' There is a bok on the table.     ', 'offset': 12, 'errorLength': 3, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'There is a bok on the table.'})]

By default, correct applies the first suggestion on the list. So I end up with this sentence:
There is a BOK on the table.

With my update, you can now choose a suggestion for each Match, and then change the Match list to correct func:

import language_tool_python

def patch_text(text):
    with language_tool_python.LanguageTool('en-US') as tool:
        matches = tool.check(text)
    for match in matches:
        if not match.replacements:
            continue
        print(match)
        index = input("Enter the index of the suggestion you want to apply: ")
        match.select_replacement(int(index)) # new method
    patched_text = language_tool_python.utils.correct(text, matches)
    return patched_text


if __name__ == '__main__':
    text = """
There is a bok on the table.
    """
    print(patch_text(text))

So I can end up with the sentence I want:
There is a book on the table.

This is a simple example, but in my case, when I'm doing interactive correction of large texts with this library, I end up having to modify the Matches myself, rather than using a method implemented in the library.

@mdevolde
Copy link
Contributor Author

I can add this to the usage examples in the readme file, if you like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant