Skip to content

Instantly share code, notes, and snippets.

@aminechraibi
Last active April 20, 2024 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminechraibi/b32b6d277c648dd31f68dc85a08ac22f to your computer and use it in GitHub Desktop.
Save aminechraibi/b32b6d277c648dd31f68dc85a08ac22f to your computer and use it in GitHub Desktop.
This Python script checks the grammar, spelling, and other issues in LaTeX files (.tex) within a specified directory using pydetex to extract plain text and language_tool_python for grammar checking. It saves the results of each file's grammar check in a separate report file in a folder named grammar_check.

LaTeX Grammar Checker Script

Description

This Python script checks the grammar, spelling, and other issues in LaTeX files (.tex) within a specified directory using pydetex to extract plain text and language_tool_python for grammar checking. It saves the results of each file's grammar check in a separate report file in a folder named grammar_check.

Installation

You need to install the required libraries via pip:

pip install pydetex language_tool_python

Usage

  1. Replace '/path/to/latex/project' with the actual path to your LaTeX project directory.
  2. Run the script.
  3. Check the grammar_check folder for individual grammar check reports for each LaTeX file.

Script

import os
import pydetex.pipelines as pip
import language_tool_python

# Initialize LanguageTool
tool = language_tool_python.LanguageTool('en-US')

# Directory where your LaTeX project is located
latex_project_directory = '.'

# Directory where you want to save the grammar check reports
output_directory = 'grammar_check'
os.makedirs(output_directory, exist_ok=True)

# List all .tex files and check each one
for root, dirs, files in os.walk(latex_project_directory):
    for file in files:
        if file.endswith('.tex'):
            path_to_file = os.path.join(root, file)
            print("Checking ", path_to_file)
            with open(path_to_file, 'r', encoding='utf-8') as f:
                tex_content = f.read()

            # Convert LaTeX to plain text
            plain_text = pip.simple(tex_content)

            # Check grammar
            matches = tool.check(plain_text)

            # Output filename
            output_file_path = os.path.join(output_directory, f'grammar_check_{file}.txt')

            # Write the matches to the output file
            with open(output_file_path, 'w', encoding='utf-8') as out_f:
                for match in matches:
                    out_f.write(f"{match}\n\n")

            print(f"Grammar check complete for {file}, results saved to {output_file_path}")

print("Grammar checking for all .tex files is complete.")

tool.close()

License

This script is provided under the MIT License.

Replace '/path/to/latex/project' with the actual path to your LaTeX project directory in the script section. Once you've filled in the details, you can create a gist on GitHub and paste this Markdown content there with a suitable name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment