Spaces:
Sleeping
Sleeping
Create ginger.py
Browse files
ginger.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
def correct_sentence(text):
|
4 |
+
"""
|
5 |
+
Corrects the given text using the Ginger grammar correction API.
|
6 |
+
|
7 |
+
Parameters:
|
8 |
+
text (str): The text to be corrected.
|
9 |
+
|
10 |
+
Returns:
|
11 |
+
str: The corrected text with grammar suggestions applied.
|
12 |
+
"""
|
13 |
+
|
14 |
+
# Ginger grammar correction API URL
|
15 |
+
ginger_api_url = 'https://services.gingersoftware.com/Ginger/correct/jsonSecured/GingerTheTextFull'
|
16 |
+
|
17 |
+
# Ginger API parameters
|
18 |
+
params = {
|
19 |
+
'lang': 'US', # Language set to English (US)
|
20 |
+
'clientVersion': '2.0', # Version of the Ginger API
|
21 |
+
'apiKey': '6ae0c3a0-afdc-4532-830c-53f0f02e2f0c', # Public API key
|
22 |
+
'text': text # Text that needs grammar correction
|
23 |
+
}
|
24 |
+
|
25 |
+
try:
|
26 |
+
# Send a GET request to the Ginger API
|
27 |
+
response = requests.get(ginger_api_url, params=params)
|
28 |
+
|
29 |
+
# Check if the request was successful
|
30 |
+
if response.status_code == 200:
|
31 |
+
# Parse the JSON response
|
32 |
+
data = response.json()
|
33 |
+
|
34 |
+
# If no corrections are found, return the original text
|
35 |
+
if not data['LightGingerTheTextResult']:
|
36 |
+
return text
|
37 |
+
|
38 |
+
# Correct the text based on the suggestions
|
39 |
+
corrected_text = apply_corrections(text, data['LightGingerTheTextResult'])
|
40 |
+
return corrected_text
|
41 |
+
|
42 |
+
else:
|
43 |
+
# If the request failed, return the original text
|
44 |
+
return text
|
45 |
+
|
46 |
+
except requests.exceptions.RequestException as e:
|
47 |
+
# Handle any exceptions (like network errors)
|
48 |
+
print(f"An error occurred: {e}")
|
49 |
+
return text
|
50 |
+
|
51 |
+
def apply_corrections(text, corrections):
|
52 |
+
"""
|
53 |
+
Applies the corrections suggested by the Ginger API.
|
54 |
+
|
55 |
+
Parameters:
|
56 |
+
text (str): The original text.
|
57 |
+
corrections (list): A list of correction suggestions from Ginger.
|
58 |
+
|
59 |
+
Returns:
|
60 |
+
str: The corrected text with suggestions applied.
|
61 |
+
"""
|
62 |
+
|
63 |
+
corrected_text = list(text)
|
64 |
+
|
65 |
+
# Offset helps adjust the position as we modify the string
|
66 |
+
offset = 0
|
67 |
+
|
68 |
+
for correction in corrections:
|
69 |
+
start = correction['From'] + offset
|
70 |
+
end = correction['To'] + 1 + offset
|
71 |
+
|
72 |
+
# Replace the incorrect part with the suggestion
|
73 |
+
suggestion = correction['Suggestions'][0]['Text']
|
74 |
+
|
75 |
+
# Replace the text within the correct range
|
76 |
+
corrected_text[start:end] = suggestion
|
77 |
+
|
78 |
+
# Adjust offset based on length difference between suggestion and original
|
79 |
+
offset += len(suggestion) - (end - start)
|
80 |
+
|
81 |
+
return ''.join(corrected_text)
|
82 |
+
|
83 |
+
def main():
|
84 |
+
"""
|
85 |
+
Main function for grammar correction.
|
86 |
+
Prompts the user to enter a sentence and corrects the grammar.
|
87 |
+
"""
|
88 |
+
|
89 |
+
# Input sentence from the user
|
90 |
+
sentence = input("Enter a sentence to correct: ")
|
91 |
+
|
92 |
+
# Correct the sentence using Ginger
|
93 |
+
corrected_sentence = correct_sentence(sentence)
|
94 |
+
|
95 |
+
# Display the corrected sentence
|
96 |
+
print("Corrected Sentence: ", corrected_sentence)
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
main()
|