From b9be8f7335e54db6e152ffd17c51bdb0486abdb7 Mon Sep 17 00:00:00 2001 From: Lyubomir Ternavskiy <127299159+LyubomirT@users.noreply.github.com> Date: Mon, 27 Nov 2023 09:53:52 +0000 Subject: [PATCH] Add stack and merge_delete functions to autocorrect.py --- lesp/autocorrect.py | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lesp/autocorrect.py b/lesp/autocorrect.py index 38aef28..506d579 100644 --- a/lesp/autocorrect.py +++ b/lesp/autocorrect.py @@ -136,3 +136,55 @@ def remove_from_wordlist(word): raise ValueError(f"\"{word}\" not in wordlist!") wordlist.remove(word) + +def stack(source, destination): + try: + with open(source, "r") as f: + source_words = f.read().split("\n") + with open(destination, "r") as f: + destination_words = f.read().split("\n") + + if any(len(word.split()) > 1 for word in source_words): + raise ValueError("Invalid source file format. Each word must be on a separate line.") + if any(len(word.split()) > 1 for word in destination_words): + raise ValueError("Invalid destination file format. Each word must be on a separate line.") + + if not all(word.isalpha() for word in source_words): + raise ValueError("Invalid source file format. Words must contain only alphabetic characters.") + if not all(word.isalpha() for word in destination_words): + raise ValueError("Invalid destination file format. Words must contain only alphabetic characters.") + + destination_words.extend(source_words) + + with open(destination, "w") as f: + f.write("\n".join(destination_words)) + except FileNotFoundError as e: + raise FileNotFoundError(f"File not found: {str(e)}") + except Exception as e: + raise ValueError(f"Error during stacking: {str(e)}") + +def merge_delete(source, destination): + try: + with open(source, "r") as f: + source_words = f.read().split("\n") + with open(destination, "r") as f: + destination_words = f.read().split("\n") + + if any(len(word.split()) > 1 for word in source_words): + raise ValueError("Invalid source file format. Each word must be on a separate line.") + if any(len(word.split()) > 1 for word in destination_words): + raise ValueError("Invalid destination file format. Each word must be on a separate line.") + + if not all(word.isalpha() for word in source_words): + raise ValueError("Invalid source file format. Words must contain only alphabetic characters.") + if not all(word.isalpha() for word in destination_words): + raise ValueError("Invalid destination file format. Words must contain only alphabetic characters.") + + destination_words = list(set(destination_words) - set(source_words)) + + with open(destination, "w") as f: + f.write("\n".join(destination_words)) + except FileNotFoundError as e: + raise FileNotFoundError(f"File not found: {str(e)}") + except Exception as e: + raise ValueError(f"Error during merge delete: {str(e)}")