import requests import cloudscraper URL = "https://services.gingersoftware.com/Ginger/correct/jsonSecured/GingerTheTextFull" # noqa API_KEY = "6ae0c3a0-afdc-4532-a810-82ded0054236" class GingerIt(object): def __init__(self): self.url = URL self.api_key = API_KEY self.api_version = "2.0" self.lang = "US" def parse(self, text, verify=True): session = cloudscraper.create_scraper() try: request = session.get( self.url, params={ "lang": self.lang, "apiKey": self.api_key, "clientVersion": self.api_version, "text": text, }, verify=verify, ) # Print the raw response text for debugging print("Response Text:", request.text) # Try parsing the response as JSON try: data = request.json() except ValueError: print("Failed to parse JSON response. Returning original text.") return {"text": text, "result": text, "corrections": []} # Process and return the corrected data return self._process_data(text, data) except requests.exceptions.RequestException as e: print(f"An error occurred during the API request: {e}") return {"text": text, "result": text, "corrections": []} @staticmethod def _change_char(original_text, from_position, to_position, change_with): return "{}{}{}".format( original_text[:from_position], change_with, original_text[to_position + 1 :] ) def _process_data(self, text, data): result = text corrections = [] if "Corrections" not in data: print("No corrections found in the API response.") return {"text": text, "result": text, "corrections": corrections} for suggestion in reversed(data["Corrections"]): start = suggestion["From"] end = suggestion["To"] if suggestion["Suggestions"]: suggest = suggestion["Suggestions"][0] result = self._change_char(result, start, end, suggest["Text"]) corrections.append( { "start": start, "text": text[start : end + 1], "correct": suggest.get("Text", None), "definition": suggest.get("Definition", None), } ) return {"text": text, "result": result, "corrections": corrections}