Advertisement
Thoughtcoder411

Grammer and typo lister and editor

Apr 28th, 2023
1,375
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | Software | 0 0
  1. import openai
  2. import os
  3. import tkinter as tk
  4. from tkinter import filedialog, scrolledtext
  5. from tkinter.ttk import Progressbar
  6. from typing import Tuple
  7.  
  8. openai.api_key = "your_api_key_here"
  9.  
  10.  
  11. def check_text(text: str) -> Tuple[str, str]:
  12.     response = openai.Completion.create(
  13.         engine="text-davinci-002",
  14.         prompt=f"Please correct the following text for grammar, punctuation, and typos:\n\n{text}",
  15.         max_tokens=100,
  16.         n=1,
  17.         stop=None,
  18.         temperature=0.7,
  19.     )
  20.  
  21.     corrected_text = response.choices[0].text.strip()
  22.     changes = "Changes:\n"
  23.  
  24.     if corrected_text == text:
  25.         changes += "No changes were made.\n"
  26.     else:
  27.         changes += f"Original: {text}\nCorrected: {corrected_text}\n"
  28.  
  29.     return corrected_text, changes
  30.  
  31.  
  32. def process_file(file_path: str, progress_bar, status_label, changes_textbox):
  33.     with open(file_path, "r") as file:
  34.         text = file.read()
  35.  
  36.     output_file_path = os.path.splitext(file_path)[0] + "_corrected.txt"
  37.     changes_file_path = os.path.splitext(file_path)[0] + "_changes.txt"
  38.  
  39.     corrected_sections = []
  40.     changes_list = []
  41.  
  42.     total_sections = (len(text) - 1) // 500 + 1
  43.     progress_bar["maximum"] = total_sections
  44.  
  45.     for i in range(0, len(text), 500):
  46.         section = text[i : i + 500]
  47.         corrected_section, changes = check_text(section)
  48.         corrected_sections.append(corrected_section)
  49.         changes_list.append(changes)
  50.  
  51.         progress_bar["value"] += 1
  52.         progress_bar.update()
  53.  
  54.         changes_textbox.insert(tk.END, changes)
  55.         changes_textbox.yview(tk.END)
  56.  
  57.     with open(output_file_path, "w") as output_file:
  58.         output_file.write("".join(corrected_sections))
  59.  
  60.     with open(changes_file_path, "w") as changes_file:
  61.         changes_file.write("\n".join(changes_list))
  62.  
  63.     status_label.config(text="Done!")
  64.  
  65.  
  66. def browse_file(progress_bar, status_label, changes_textbox):
  67.     file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
  68.     if file_path:
  69.         status_label.config(text="Changes loading...")
  70.         progress_bar["value"] = 0
  71.         progress_bar.update()
  72.         process_file(file_path, progress_bar, status_label, changes_textbox)
  73.  
  74.  
  75. def main():
  76.     root = tk.Tk()
  77.     root.title("Grammar and Typo Checker")
  78.  
  79.     load_button = tk.Button(root, text="Load Text File", command=lambda: browse_file(progress_bar, status_label, changes_textbox))
  80.     load_button.pack(pady=10)
  81.  
  82.     status_label = tk.Label(root, text="")
  83.     status_label.pack(pady=5)
  84.  
  85.     progress_bar = Progressbar(root, orient="horizontal", mode="determinate")
  86.     progress_bar.pack(pady=5, padx=10, fill=tk.X)
  87.  
  88.     changes_label = tk.Label(root, text="Changes:")
  89.     changes_label.pack(pady=5)
  90.  
  91.     changes_textbox = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=80, height=20)
  92.     changes_textbox.pack(padx=10, pady=5, fill=tk.BOTH, expand=True)
  93.  
  94.     root.mainloop()
  95.  
  96.  
  97. if __name__ == "__main__":
  98.     main()
  99.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement