|
import requests |
|
|
|
def correct_sentence(text): |
|
""" |
|
Corrects the given text using the Ginger grammar correction API. |
|
|
|
Parameters: |
|
text (str): The text to be corrected. |
|
|
|
Returns: |
|
str: The corrected text with grammar suggestions applied. |
|
""" |
|
|
|
|
|
ginger_api_url = 'https://services.gingersoftware.com/Ginger/correct/jsonSecured/GingerTheTextFull' |
|
|
|
|
|
params = { |
|
'lang': 'US', |
|
'clientVersion': '2.0', |
|
'apiKey': '6ae0c3a0-afdc-4532-830c-53f0f02e2f0c', |
|
'text': text |
|
} |
|
|
|
try: |
|
|
|
response = requests.get(ginger_api_url, params=params) |
|
|
|
|
|
if response.status_code == 200: |
|
|
|
data = response.json() |
|
|
|
|
|
if not data['LightGingerTheTextResult']: |
|
return text |
|
|
|
|
|
corrected_text = apply_corrections(text, data['LightGingerTheTextResult']) |
|
return corrected_text |
|
|
|
else: |
|
|
|
return text |
|
|
|
except requests.exceptions.RequestException as e: |
|
|
|
print(f"An error occurred: {e}") |
|
return text |
|
|
|
def apply_corrections(text, corrections): |
|
""" |
|
Applies the corrections suggested by the Ginger API. |
|
|
|
Parameters: |
|
text (str): The original text. |
|
corrections (list): A list of correction suggestions from Ginger. |
|
|
|
Returns: |
|
str: The corrected text with suggestions applied. |
|
""" |
|
|
|
corrected_text = list(text) |
|
|
|
|
|
offset = 0 |
|
|
|
for correction in corrections: |
|
start = correction['From'] + offset |
|
end = correction['To'] + 1 + offset |
|
|
|
|
|
suggestion = correction['Suggestions'][0]['Text'] |
|
|
|
|
|
corrected_text[start:end] = suggestion |
|
|
|
|
|
offset += len(suggestion) - (end - start) |
|
|
|
return ''.join(corrected_text) |
|
|
|
def main(): |
|
""" |
|
Main function for grammar correction. |
|
Prompts the user to enter a sentence and corrects the grammar. |
|
""" |
|
|
|
|
|
sentence = input("Enter a sentence to correct: ") |
|
|
|
|
|
corrected_sentence = correct_sentence(sentence) |
|
|
|
|
|
print("Corrected Sentence: ", corrected_sentence) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|