-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add folder loading support for bulk JSON imports #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e0fa505
5d21840
a7e3ce2
9bf150d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -118,6 +118,16 @@ contractions.expand("myterm is great") | |||||||
| # "my expansion is great" | ||||||||
| ``` | ||||||||
|
|
||||||||
| Load all JSON files from a folder: | ||||||||
|
|
||||||||
| ```python | ||||||||
| # Load all *.json files from a directory (ignores non-JSON files) | ||||||||
| contractions.load_folder("./my_contractions/") | ||||||||
|
|
||||||||
| contractions.expand("myterm is great") | ||||||||
| # "my expansion is great" | ||||||||
| ``` | ||||||||
|
|
||||||||
| ### Preview Contractions Before Fixing | ||||||||
|
|
||||||||
| The `preview()` function lets you see all contractions in a text before expanding them: | ||||||||
|
|
@@ -176,6 +186,18 @@ Loads custom contractions from a JSON file. | |||||||
| - `FileNotFoundError`: If the file doesn't exist | ||||||||
| - `json.JSONDecodeError`: If the file contains invalid JSON | ||||||||
|
|
||||||||
| ### `load_folder(folderpath)` | ||||||||
|
|
||||||||
| Loads custom contractions from all JSON files in a directory. Non-JSON files are automatically ignored. | ||||||||
|
|
||||||||
| **Parameters:** | ||||||||
| - `folderpath` (str): Path to directory containing JSON files | ||||||||
|
|
||||||||
| **Raises:** | ||||||||
| - `FileNotFoundError`: If the folder doesn't exist | ||||||||
| - `NotADirectoryError`: If the path is a file, not a directory | ||||||||
| - `ValueError`: If no JSON files are found in the folder | ||||||||
|
||||||||
| - `ValueError`: If no JSON files are found in the folder | |
| - `ValueError`: If no JSON files are found in the folder | |
| - `json.JSONDecodeError`: If any JSON file contains invalid JSON |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,6 +169,66 @@ def test_load_file_non_dict() -> None: | |
| os.unlink(temp_path) | ||
|
|
||
|
|
||
| def test_load_folder() -> None: | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| file1 = os.path.join(temp_dir, "dict1.json") | ||
| file2 = os.path.join(temp_dir, "dict2.json") | ||
|
|
||
| with open(file1, "w", encoding="utf-8") as f: | ||
| json.dump({"foldertest1": "folder test one", "foldertest2": "folder test two"}, f) | ||
|
|
||
| with open(file2, "w", encoding="utf-8") as f: | ||
| json.dump({"foldertest3": "folder test three"}, f) | ||
|
|
||
| contractions.load_folder(temp_dir) | ||
|
|
||
| assert contractions.expand("foldertest1") == "folder test one" | ||
| assert contractions.expand("foldertest2") == "folder test two" | ||
| assert contractions.expand("foldertest3") == "folder test three" | ||
|
|
||
|
|
||
| def test_load_folder_ignores_non_json() -> None: | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| json_file = os.path.join(temp_dir, "valid.json") | ||
| txt_file = os.path.join(temp_dir, "ignored.txt") | ||
|
|
||
| with open(json_file, "w", encoding="utf-8") as f: | ||
| json.dump({"validkey": "valid value"}, f) | ||
|
|
||
| with open(txt_file, "w", encoding="utf-8") as f: | ||
| f.write("this should be ignored") | ||
|
|
||
| contractions.load_folder(temp_dir) | ||
| assert contractions.expand("validkey") == "valid value" | ||
|
|
||
|
|
||
| def test_load_folder_not_found() -> None: | ||
| with pytest.raises(FileNotFoundError, match="Folder not found"): | ||
| contractions.load_folder("/nonexistent/folder/path") | ||
|
|
||
|
|
||
| def test_load_folder_not_directory() -> None: | ||
| with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False, encoding="utf-8") as f: | ||
| json.dump({"test": "data"}, f) | ||
| temp_path = f.name | ||
|
|
||
| try: | ||
| with pytest.raises(NotADirectoryError, match="not a directory"): | ||
| contractions.load_folder(temp_path) | ||
| finally: | ||
| os.unlink(temp_path) | ||
|
|
||
|
|
||
| def test_load_folder_no_json_files() -> None: | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| txt_file = os.path.join(temp_dir, "file.txt") | ||
| with open(txt_file, "w", encoding="utf-8") as f: | ||
| f.write("no json here") | ||
|
|
||
| with pytest.raises(ValueError, match="No JSON files found"): | ||
| contractions.load_folder(temp_dir) | ||
|
|
||
|
Comment on lines
+172
to
+230
|
||
|
|
||
| def test_expand_leftovers_only() -> None: | ||
| text = "I'm happy you're here" | ||
| result = contractions.expand(text, leftovers=True, slang=False) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation should clarify the behavior when multiple JSON files contain the same keys. Currently, later files (in sorted order) will overwrite values from earlier files. This merge behavior should be documented so users understand how conflicts are resolved.