Skip to content

Commit

Permalink
Merge pull request #2 from Marley-Mulvin-Broome/json
Browse files Browse the repository at this point in the history
Bug fixes and better testing
  • Loading branch information
Marley-Mulvin-Broome committed Aug 30, 2023
2 parents d33ec6f + bc4cc87 commit 7bb651e
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/jpfreq/exporters/iexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

class IExporter(metaclass=abc.ABCMeta):
@abc.abstractmethod
def export(self, frequency_list: JapaneseFrequencyList, limit: int=100, combine: bool=True) -> str:
def export(self, frequency_list: JapaneseFrequencyList, limit: int = 100, combine: bool = True) -> str:
raise NotImplementedError

@abc.abstractmethod
def export_lazy(self, frequency_list: JapaneseFrequencyList, limit: int=100, combine: bool=True) -> Generator[str, None, None]:
def export_lazy(self, frequency_list: JapaneseFrequencyList, limit: int = 100, combine: bool = True) -> Generator[str, None, None]:
raise NotImplementedError
15 changes: 11 additions & 4 deletions src/jpfreq/exporters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,31 @@ def _create_export_dictionary(frequency_list: JapaneseFrequencyList, limit: int

return dictionary

def export(self, frequency_list: JapaneseFrequencyList, limit: int = 100, combine: bool = False) -> str:
def export(self, frequency_list: JapaneseFrequencyList, limit: int = 100, combine: bool = False, as_dict: bool = False) -> [str|dict]:
"""
Exports the frequency list to JSON.
Parameters
----------
frequency_list : JapaneseFrequencyList
The frequency list to export
limit
limit : int
The MAX number of words to export
combine
combine : bool
Whether to combine the word slots or not
as_dict : bool
Whether to return the dictionary or the JSON string
Returns
-------
str
The JSON string
"""
return dumps(self._create_export_dictionary(frequency_list, limit=limit), ensure_ascii=False, indent=4)
result = self._create_export_dictionary(frequency_list, limit=limit, combine=combine)

if as_dict:
return result

return dumps(result, ensure_ascii=False, indent=4)

def export_lazy(self, frequency_list: JapaneseFrequencyList, limit: int = 100, combine: bool = False) -> Generator[str, None, None]:
return super().export_lazy(frequency_list, limit)
18 changes: 16 additions & 2 deletions src/jpfreq/word_slot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@
from .word import Word


@dataclass
@dataclass(init=False)
class WordSlot:
words: list[Word]

def __init__(self, words: Iterable[Word]):
"""
Creates a WordSlot from a list of words.
Parameters
----------
words : Iterable[Word]
The words to create the word slot from.
"""
self.words = []

[self.add_word(word) for word in words]

def __contains__(self, item: [Word | str]):
"""
Checks if the word slot contains the item.
Expand All @@ -34,6 +46,8 @@ def __contains__(self, item: [Word | str]):
if isinstance(item, str):
return item in [word.surface for word in self.words]

return False

def __len__(self):
"""
Returns the frequency of the word slot.
Expand Down Expand Up @@ -104,7 +118,7 @@ def add_word(self, word: Word) -> None:
"""
for old_word in self.words:
if old_word.surface == word.surface:
old_word.frequency += 1
old_word.frequency += word.frequency
return

self.words.append(word)
Expand Down
9 changes: 7 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ open_report=false
generate_report=false
report=""
save_report=false
exit_first=""

while getopts "r:c:hogs" opt; do
while getopts "r:c:hogsx" opt; do
case $opt in
h)
echo "Usage: [-ogs] test.sh [-r <required coverage>] [-c <coverage report type>]"
echo "-o opens the coverage report in a browser after running tests. Requires -g"
echo "-g generates a coverage report"
echo "-s saves test coverage report to 'pytest-coverage.txt and pytest.xml"
echo "-x exits on first failure"
exit 0
;;
r)
Expand All @@ -34,6 +36,9 @@ while getopts "r:c:hogs" opt; do
s)
save_report=true
;;
x)
exit_first="-x"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
Expand All @@ -55,7 +60,7 @@ exit_code=0

# runs coverage with a html report
# fails if cov < required_coverage
result=$(pytest -n auto $report tests/)
result=$(pytest -n auto "$exit_first" $report tests/)
exit_code=$?

if [ "$save_report" = true ] ; then
Expand Down
117 changes: 99 additions & 18 deletions tests/exporters/test_json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from jpfreq.exporters.json import JsonExporter
from jpfreq.jp_frequency_list import JapaneseFrequencyList
from jpfreq.word import WordType
from json import dumps

import pytest

Expand All @@ -15,20 +15,101 @@ def freq_list():
return JapaneseFrequencyList()


# test_to_dict_data = [
# ("", {}),
# ("あ", {"words": [{"surface": "あ", "types": [WordType.VERB], "frequency": 1}]}),
# ]
#
#
# @pytest.mark.parametrize("text, expected", test_to_dict_data)
# def test_to_dict(exporter, freq_list: JapaneseFrequencyList, text, expected):
# freq_list.process_text(text)
# assert exporter._create_export_dictionary(freq_list) == expected
#

#
# @pytest.mark.parametrize("text, expected", test_export_data)
# def test_export(exporter, freq_list: JapaneseFrequencyList, text, expected):
# freq_list.process_text(text)
# assert exporter.export(freq_list).strip() == expected.strip()
test_to_dict_keys_data = [
("", ["text_info", "word_slots"], False, 100),
("あ", ["text_info", "word_slots"], False, 100),
("あ", ["text_info", "word_slots"], False, 1),
("あ", ["text_info", "word_slots"], False, -1),
("", ["text_info", "word_slots"], False, 100),
("あ", ["text_info", "word_slots"], True, 100),
("あ", ["text_info", "word_slots"], True, 1),
("あ", ["text_info", "word_slots"], True, -1),
]


@pytest.mark.parametrize("text, expected_keys, combine, limit", test_to_dict_keys_data)
def test_to_dict_keys(
exporter, freq_list: JapaneseFrequencyList, text, expected_keys, combine, limit
):
freq_list.process_text(text)
result = exporter._create_export_dictionary(freq_list, combine=combine, limit=limit)

for key in expected_keys:
assert key in result.keys()


test_to_dict_word_slots_count_data = [
("", 0, False, 100),
("あ", 1, False, 100),
("あ", 1, False, 1),
("あ", 1, False, -1),
("猫が好き", 2, False, 100),
("猫が好き", 1, False, 1),
("猫が好き", 2, False, -1),
("猫が猫", 1, False, 100),
("猫が猫", 1, False, 1),
("猫が猫", 1, False, -1),
("あ", 1, True, 100),
("あ", 1, True, 1),
("あ", 1, True, -1),
("猫が好き", 2, True, 100),
("猫が好き", 1, True, 1),
("猫が好き", 2, True, -1),
("猫が猫", 1, True, 100),
("猫が猫", 1, True, 1),
("猫が猫", 1, True, -1),
]


@pytest.mark.parametrize(
"text, expected_count, combine, limit", test_to_dict_word_slots_count_data
)
def test_to_dict_word_slots_count(
exporter, freq_list: JapaneseFrequencyList, text, expected_count, combine, limit
):
freq_list.process_text(text)
result = exporter._create_export_dictionary(freq_list, combine=combine, limit=limit)

assert len(result["word_slots"]) == expected_count


test_export_data = [
("", False, 100),
("あ", False, 100),
("あ", False, 1),
("あ", False, -1),
("猫が好き", False, 100),
("猫が好き", False, 1),
("猫が好き", False, -1),
("", True, 100),
("あ", True, 100),
("あ", True, 1),
("あ", True, -1),
("猫が好き", True, 100),
("猫が好き", True, 1),
("猫が好き", True, -1),
]


@pytest.mark.parametrize("text, combine, limit", test_export_data)
def test_export_string(exporter, freq_list, text, combine, limit):
freq_list.process_text(text)
result = exporter.export(freq_list, combine=combine, limit=limit)

assert isinstance(result, str)
assert result == dumps(
exporter._create_export_dictionary(freq_list, combine=combine, limit=limit),
ensure_ascii=False,
indent=4,
)


@pytest.mark.parametrize("text, combine, limit", test_export_data)
def test_export_dict(exporter, freq_list, text, combine, limit):
freq_list.process_text(text)
result = exporter.export(freq_list, combine=combine, limit=limit, as_dict=True)

assert isinstance(result, dict)
assert result == exporter._create_export_dictionary(
freq_list, combine=combine, limit=limit
)
Empty file removed tests/test_json.py
Empty file.
28 changes: 28 additions & 0 deletions tests/test_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from jpfreq.word import Word, WordType

import pytest

to_dict_data = [
(
Word("test", "test", [], 1),
{"representation": "test", "surface": "test", "types": [], "frequency": 1},
),
(
Word("test", "test", [WordType.NOUN, WordType.GENERAL], 1),
{
"representation": "test",
"surface": "test",
"types": ["名詞", "一般"],
"frequency": 1,
},
),
(
Word("test", "test", [WordType.VERB], 2),
{"representation": "test", "surface": "test", "types": ["動詞"], "frequency": 2},
),
]


@pytest.mark.parametrize("word, expected_dict", to_dict_data)
def test_to_dict(word, expected_dict):
assert word.to_dict() == expected_dict
Loading

0 comments on commit 7bb651e

Please sign in to comment.