Skip to content

Commit

Permalink
Merge branch 'nrnrk-feature/load_dictionary_support_file_object' into…
Browse files Browse the repository at this point in the history
… dev
  • Loading branch information
mammothb committed Oct 18, 2024
2 parents f08e79a + d996c01 commit cc4977a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 9 additions & 3 deletions symspellpy/symspellpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,19 +314,20 @@ def load_bigram_dictionary(

def load_dictionary(
self,
corpus: Union[Path, str],
corpus: Union[Path, str, IO[str]],
term_index: int,
count_index: int,
separator: str = " ",
encoding: Optional[str] = None,
):
) -> bool:
"""Loads multiple dictionary entries from a file of word/frequency count
pairs.
**NOTE**: Merges with any dictionary data already loaded.
Args:
corpus: The path+filename of the file.
corpus: The path+filename of the file or a file object of the
dictionary.
term_index: The column position of the word.
count_index: The column position of the frequency count.
separator: Separator characters between term(s) and count.
Expand All @@ -335,6 +336,11 @@ def load_dictionary(
Returns:
``True`` if file loaded, or ``False`` if file not found.
"""
if not isinstance(corpus, (Path, str)):
return self._load_dictionary_stream(
corpus, term_index, count_index, separator
)

corpus = Path(corpus)
if not corpus.exists():
logger.error(f"Dictionary file not found at {corpus}.")
Expand Down
13 changes: 13 additions & 0 deletions tests/test_symspellpy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from io import StringIO
from pathlib import Path
from unittest import TestCase

Expand Down Expand Up @@ -239,6 +240,18 @@ def test_load_dictionary_encoding(self, symspell_default):
assert 1 == len(result)
assert "АБИ" == result[0].term

def test_load_dictionary_from_string_io(self, symspell_default, dictionary_path):
with open(dictionary_path, "r") as f:
symspell_default.load_dictionary(StringIO(f.read()), 0, 1)
assert 82834 == symspell_default.word_count
assert 676094 == symspell_default.entry_count

def test_load_dictionary_from_text_io_wrapper(self, symspell_default, dictionary_path):
with open(dictionary_path, "r") as f:
symspell_default.load_dictionary(f, 0, 1)
assert 82834 == symspell_default.word_count
assert 676094 == symspell_default.entry_count

def test_create_dictionary_invalid_path(self, symspell_default):
with TestCase.assertLogs("symspellpy.symspellpy.logger", level="ERROR") as cm:
assert not symspell_default.create_dictionary(INVALID_PATH)
Expand Down

0 comments on commit cc4977a

Please sign in to comment.