Created
July 22, 2017 19:44
-
-
Save siennathesane/1c419245018ab50dbb99dd736947e16f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| attempts = 0 | |
| def ask_lt(s): | |
| s = s.strip() # Believe this removes all whitespace from left and right | |
| # Need to trim both left and right for elipses | |
| while s[:3] == '...': | |
| s = s[3:] | |
| while s[-3:] == '...': | |
| s = s[:-3] | |
| params = {"text": s, "language": "en-US"} | |
| resp = requests.get("http://127.0.0.1:8081/v2/check", params=params).json() | |
| try: | |
| # basically, if this is empty, it will throw an exception we can catch, and then break the loop. | |
| mistake = resp["matches"][0] | |
| _ = mistake["message"] # throws KeyError if no match | |
| except (KeyError, IndexError): | |
| return s | |
| else: | |
| local_context = mistake["context"] | |
| original_string = s | |
| print(mistake) | |
| fixed_string = ''.join([ | |
| original_string[:local_context["offset"]], | |
| validate_replacement(mistake["replacements"], original_string[local_context["offset"]:local_context["length"] + local_context["offset"]]), | |
| original_string[local_context["length"] + local_context["offset"]:], | |
| ]) | |
| while attempts > 25: | |
| return ask_lt(fixed_string) | |
| else: | |
| return s | |
| def validate_replacement(x, s): | |
| try: | |
| y = x[0]["value"] | |
| return y | |
| except IndexError: | |
| return s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment