alessandro trinca tornidor
commited on
Commit
·
000cfc3
1
Parent(s):
125ee1c
feat: add backend support for multi-word stemming with test cases
Browse files- my_ghost_writer/constants.py +1 -0
- my_ghost_writer/text_parsers.py +78 -32
- tests/events/llm_generated_story_4.txt +3 -0
- tests/events/response_get_words_tokens_and_indexes_ngrams_text4_n5.json +0 -0
- tests/events/response_text_stemming_empty_rows.json +0 -0
- tests/events/response_text_stemming_from_llm_generated_story_3.json +0 -0
- tests/events/response_text_stemming_no_parents.json +0 -0
- tests/events/response_text_stemming_with_parents.json +0 -0
- tests/test_text_parsers.py +81 -29
my_ghost_writer/constants.py
CHANGED
|
@@ -17,5 +17,6 @@ LOG_JSON_FORMAT = bool(os.getenv("LOG_JSON_FORMAT"))
|
|
| 17 |
IS_TESTING = bool(os.getenv('IS_TESTING', ""))
|
| 18 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
|
| 19 |
API_MODE = bool(os.getenv("API_MODE", ""))
|
|
|
|
| 20 |
session_logger.setup_logging(json_logs=LOG_JSON_FORMAT, log_level=LOG_LEVEL)
|
| 21 |
app_logger = structlog.stdlib.get_logger(__name__)
|
|
|
|
| 17 |
IS_TESTING = bool(os.getenv('IS_TESTING', ""))
|
| 18 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
|
| 19 |
API_MODE = bool(os.getenv("API_MODE", ""))
|
| 20 |
+
N_WORDS_GRAM = int(os.getenv("N_WORDS_GRAM", 2))
|
| 21 |
session_logger.setup_logging(json_logs=LOG_JSON_FORMAT, log_level=LOG_LEVEL)
|
| 22 |
app_logger = structlog.stdlib.get_logger(__name__)
|
my_ghost_writer/text_parsers.py
CHANGED
|
@@ -2,14 +2,14 @@ from typing import Iterator
|
|
| 2 |
|
| 3 |
from nltk import PorterStemmer
|
| 4 |
|
| 5 |
-
from my_ghost_writer.constants import app_logger
|
| 6 |
from my_ghost_writer.type_hints import RequestTextRowsParentList, ResponseTextRowsDict
|
| 7 |
|
| 8 |
|
| 9 |
ps = PorterStemmer()
|
| 10 |
|
| 11 |
|
| 12 |
-
def text_stemming(text: str | RequestTextRowsParentList) -> ResponseTextRowsDict:
|
| 13 |
"""
|
| 14 |
Applies Porter Stemmer algorithm to reduce words in a given text to their base form;
|
| 15 |
then it uses WordPunctTokenizer() to produce a dict of words frequency with, for
|
|
@@ -17,6 +17,7 @@ def text_stemming(text: str | RequestTextRowsParentList) -> ResponseTextRowsDict
|
|
| 17 |
|
| 18 |
Args:
|
| 19 |
text (str): Input string containing the text to be stemmed.
|
|
|
|
| 20 |
|
| 21 |
Returns:
|
| 22 |
tuple[int, dict]: a tuple with the number of processed total rows within the initial text and the word frequency dict
|
|
@@ -43,9 +44,12 @@ def text_stemming(text: str | RequestTextRowsParentList) -> ResponseTextRowsDict
|
|
| 43 |
idx_rows = []
|
| 44 |
idx_rows_child = []
|
| 45 |
idx_rows_parent = []
|
|
|
|
| 46 |
for textrow in valid_textrows_with_num:
|
| 47 |
row = textrow["text"]
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
idx_rows_child.append(textrow["idxRowChild"])
|
| 51 |
idx_rows_parent.append(textrow["idxRowParent"])
|
|
@@ -54,39 +58,11 @@ def text_stemming(text: str | RequestTextRowsParentList) -> ResponseTextRowsDict
|
|
| 54 |
idx_rows_parent.append(None)
|
| 55 |
row_words_tokens.append(wordpunct_tokenize(row))
|
| 56 |
row_offsets_tokens.append(WordPunctTokenizer().span_tokenize(row))
|
| 57 |
-
words_stems_dict =
|
| 58 |
n_total_rows = len(valid_textrows_with_num)
|
| 59 |
return n_total_rows, words_stems_dict
|
| 60 |
|
| 61 |
|
| 62 |
-
def get_words_tokens_and_indexes(
|
| 63 |
-
words_tokens_list: list[str], offsets_tokens_list: list | Iterator, idx_rows_list: list[int], idx_rows_child: list[int], idx_rows_parent: list[int]
|
| 64 |
-
) -> dict:
|
| 65 |
-
"""
|
| 66 |
-
Get the word tokens and their indexes in the text.
|
| 67 |
-
|
| 68 |
-
Args:
|
| 69 |
-
words_tokens_list (list): List of words tokens.
|
| 70 |
-
offsets_tokens_list (list): List of offsets for each token.
|
| 71 |
-
idx_rows_list (list[int]): List of row indices corresponding to the tokens.
|
| 72 |
-
idx_rows_child (list[int]): List of child row indices corresponding to the tokens.
|
| 73 |
-
idx_rows_parent (list[int]): List of parent row indices corresponding to the tokens.
|
| 74 |
-
|
| 75 |
-
Returns:
|
| 76 |
-
dict: Dictionary with stemmed words as keys and a list of dictionaries
|
| 77 |
-
containing the original word and its offsets as values.
|
| 78 |
-
"""
|
| 79 |
-
words_stems_dict = {}
|
| 80 |
-
for (n_row, n_row_child, n_row_parent, words_tokens, offsets_tokens) in zip(idx_rows_list, idx_rows_child, idx_rows_parent, words_tokens_list, offsets_tokens_list):
|
| 81 |
-
for word, offsets in zip(words_tokens, offsets_tokens):
|
| 82 |
-
stem = ps.stem(word)
|
| 83 |
-
if stem not in words_stems_dict:
|
| 84 |
-
words_stems_dict[stem] = {"count": 0, "word_prefix": stem, "offsets_array": []}
|
| 85 |
-
count, word_offsets = update_stems_list(words_stems_dict[stem], word, offsets, n_row=n_row, n_row_child=n_row_child, n_row_parent=n_row_parent)
|
| 86 |
-
words_stems_dict[stem] = {"count": count, "word_prefix": stem, "offsets_array": word_offsets}
|
| 87 |
-
return words_stems_dict
|
| 88 |
-
|
| 89 |
-
|
| 90 |
def update_stems_list(current_stem_tuple: dict, word: str, offsets: list, n_row: int, n_row_child: int, n_row_parent: int) -> tuple:
|
| 91 |
"""
|
| 92 |
Update the stem list with the new stem and its count.
|
|
@@ -106,3 +82,73 @@ def update_stems_list(current_stem_tuple: dict, word: str, offsets: list, n_row:
|
|
| 106 |
n += 1
|
| 107 |
word_offsets.append({"word": word, "offsets": list(offsets), "n_row": n_row, "n_row_child": n_row_child, "n_row_parent": n_row_parent})
|
| 108 |
return n, word_offsets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
from nltk import PorterStemmer
|
| 4 |
|
| 5 |
+
from my_ghost_writer.constants import app_logger, N_WORDS_GRAM
|
| 6 |
from my_ghost_writer.type_hints import RequestTextRowsParentList, ResponseTextRowsDict
|
| 7 |
|
| 8 |
|
| 9 |
ps = PorterStemmer()
|
| 10 |
|
| 11 |
|
| 12 |
+
def text_stemming(text: str | RequestTextRowsParentList, n = 3) -> ResponseTextRowsDict:
|
| 13 |
"""
|
| 14 |
Applies Porter Stemmer algorithm to reduce words in a given text to their base form;
|
| 15 |
then it uses WordPunctTokenizer() to produce a dict of words frequency with, for
|
|
|
|
| 17 |
|
| 18 |
Args:
|
| 19 |
text (str): Input string containing the text to be stemmed.
|
| 20 |
+
n (int): The maximum number of words to consider for n-grams (default is 3).
|
| 21 |
|
| 22 |
Returns:
|
| 23 |
tuple[int, dict]: a tuple with the number of processed total rows within the initial text and the word frequency dict
|
|
|
|
| 44 |
idx_rows = []
|
| 45 |
idx_rows_child = []
|
| 46 |
idx_rows_parent = []
|
| 47 |
+
rows_dict = {}
|
| 48 |
for textrow in valid_textrows_with_num:
|
| 49 |
row = textrow["text"]
|
| 50 |
+
idx_row = textrow["idxRow"]
|
| 51 |
+
rows_dict[idx_row] = row
|
| 52 |
+
idx_rows.append(idx_row)
|
| 53 |
try:
|
| 54 |
idx_rows_child.append(textrow["idxRowChild"])
|
| 55 |
idx_rows_parent.append(textrow["idxRowParent"])
|
|
|
|
| 58 |
idx_rows_parent.append(None)
|
| 59 |
row_words_tokens.append(wordpunct_tokenize(row))
|
| 60 |
row_offsets_tokens.append(WordPunctTokenizer().span_tokenize(row))
|
| 61 |
+
words_stems_dict = get_words_tokens_and_indexes_ngrams(row_words_tokens, row_offsets_tokens, idx_rows, idx_rows_child, idx_rows_parent, rows_dict=rows_dict, n=n)
|
| 62 |
n_total_rows = len(valid_textrows_with_num)
|
| 63 |
return n_total_rows, words_stems_dict
|
| 64 |
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def update_stems_list(current_stem_tuple: dict, word: str, offsets: list, n_row: int, n_row_child: int, n_row_parent: int) -> tuple:
|
| 67 |
"""
|
| 68 |
Update the stem list with the new stem and its count.
|
|
|
|
| 82 |
n += 1
|
| 83 |
word_offsets.append({"word": word, "offsets": list(offsets), "n_row": n_row, "n_row_child": n_row_child, "n_row_parent": n_row_parent})
|
| 84 |
return n, word_offsets
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def get_words_tokens_and_indexes_ngrams(
|
| 88 |
+
words_tokens_list: list[list[str]] | Iterator,
|
| 89 |
+
offsets_tokens_list: list[list[tuple[int, int]]] | Iterator,
|
| 90 |
+
idx_rows_list: list[int],
|
| 91 |
+
idx_rows_child: list[int],
|
| 92 |
+
idx_rows_parent: list[int],
|
| 93 |
+
rows_dict: dict[int, str],
|
| 94 |
+
n: int = N_WORDS_GRAM
|
| 95 |
+
) -> dict:
|
| 96 |
+
f"""
|
| 97 |
+
Like get_words_tokens_and_indexes, but supports joined n-grams (from 1 up to n words).
|
| 98 |
+
Returns a dict with n-gram stem as key and offsets/count as in example_result.
|
| 99 |
+
The 'word_prefix' is set to the most common 'word' in offsets_array.
|
| 100 |
+
|
| 101 |
+
Args:
|
| 102 |
+
words_tokens_list (list): List of lists of words tokens.
|
| 103 |
+
offsets_tokens_list (list): List of lists of offsets for each token.
|
| 104 |
+
idx_rows_list (list[int]): List of row indices corresponding to the tokens.
|
| 105 |
+
idx_rows_child (list[int]): List of child row indices corresponding to the tokens.
|
| 106 |
+
idx_rows_parent (list[int]): List of parent row indices corresponding to the tokens.
|
| 107 |
+
rows_dict (dict[int, str]): Dictionary mapping row indices to their text.
|
| 108 |
+
n (int): The maximum number of words to consider for n-grams (default is from the N_WORDS_GRAM constant,
|
| 109 |
+
right now it has value of ${N_WORDS_GRAM}).
|
| 110 |
+
|
| 111 |
+
Returns:
|
| 112 |
+
dict: Dictionary with n-gram stems as keys and a dictionary of their counts, word prefixes, and offsets as values.
|
| 113 |
+
"""
|
| 114 |
+
from collections import Counter
|
| 115 |
+
|
| 116 |
+
ngram_dict = {}
|
| 117 |
+
for (n_row, n_row_child, n_row_parent, words_tokens, offsets_tokens) in zip(
|
| 118 |
+
idx_rows_list, idx_rows_child, idx_rows_parent, words_tokens_list, offsets_tokens_list
|
| 119 |
+
):
|
| 120 |
+
words_tokens = list(words_tokens)
|
| 121 |
+
offsets_tokens = list(offsets_tokens)
|
| 122 |
+
length = len(words_tokens)
|
| 123 |
+
for n_words_ngram in range(1, n + 1):
|
| 124 |
+
for i in range(length - n_words_ngram + 1):
|
| 125 |
+
row = rows_dict[n_row]
|
| 126 |
+
ngram_words = words_tokens[i:i + n_words_ngram]
|
| 127 |
+
stem_list = [ps.stem(word=word) for word in ngram_words]
|
| 128 |
+
ngram_offsets = offsets_tokens[i:i + n_words_ngram]
|
| 129 |
+
start = ngram_offsets[0][0]
|
| 130 |
+
end = ngram_offsets[-1][1]
|
| 131 |
+
ngram_stem = " ".join(stem_list)
|
| 132 |
+
ngram = row[start:end]
|
| 133 |
+
if ngram_stem not in ngram_dict:
|
| 134 |
+
ngram_dict[ngram_stem] = {"count": 0, "word_prefix": ngram, "offsets_array": [], "n_words_ngram": n_words_ngram}
|
| 135 |
+
# Use update_stems_list to update count and offsets_array
|
| 136 |
+
count, offsets_array = update_stems_list(
|
| 137 |
+
ngram_dict[ngram_stem],
|
| 138 |
+
ngram,
|
| 139 |
+
[start, end],
|
| 140 |
+
n_row=n_row,
|
| 141 |
+
n_row_child=n_row_child,
|
| 142 |
+
n_row_parent=n_row_parent
|
| 143 |
+
)
|
| 144 |
+
ngram_dict[ngram_stem]["count"] = count
|
| 145 |
+
ngram_dict[ngram_stem]["offsets_array"] = offsets_array
|
| 146 |
+
|
| 147 |
+
# Update word_prefix to the most common 'word' in offsets_array
|
| 148 |
+
for entry in ngram_dict.values():
|
| 149 |
+
words = [item["word"] for item in entry["offsets_array"] if "word" in item]
|
| 150 |
+
if words:
|
| 151 |
+
most_common_word, _ = Counter(words).most_common(1)[0]
|
| 152 |
+
entry["word_prefix"] = most_common_word
|
| 153 |
+
|
| 154 |
+
return ngram_dict
|
tests/events/llm_generated_story_4.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Once upon a time, in the kingdom of Elwoodia, there lived a young and wise young Princess named Isabella. She saw a tiny creature like one of the fairies her mother told her.
|
| 2 |
+
One of the fairies flew to her and spoke in a soft voice. The fairy was dressed with colourful flowers and green leaves, with a colourful flower with many petals.
|
| 3 |
+
The fairy grabbed some rocks. One was red, crowned with colourful flowers and some green leaves. Many of the fairies were small but quick.
|
tests/events/response_get_words_tokens_and_indexes_ngrams_text4_n5.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tests/events/response_text_stemming_empty_rows.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tests/events/response_text_stemming_from_llm_generated_story_3.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tests/events/response_text_stemming_no_parents.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tests/events/response_text_stemming_with_parents.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tests/test_text_parsers.py
CHANGED
|
@@ -7,6 +7,29 @@ from nltk.tokenize import wordpunct_tokenize, WordPunctTokenizer
|
|
| 7 |
from tests import EVENTS_FOLDER
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
class TestTextParsers(unittest.TestCase):
|
| 11 |
def setUp(self):
|
| 12 |
with open(EVENTS_FOLDER / "get_words_tokens_and_indexes_inputs.json", "r") as src:
|
|
@@ -34,35 +57,17 @@ class TestTextParsers(unittest.TestCase):
|
|
| 34 |
self.ps = nltk.PorterStemmer()
|
| 35 |
self.wnl = nltk.WordNetLemmatizer()
|
| 36 |
|
| 37 |
-
def test_get_words_tokens_and_indexes(self):
|
| 38 |
-
from my_ghost_writer.text_parsers import get_words_tokens_and_indexes
|
| 39 |
-
row_words_tokens = self.get_words_tokens_and_indexes_inputs["row_words_tokens"]
|
| 40 |
-
row_offsets_tokens = self.get_words_tokens_and_indexes_inputs["row_offsets_tokens"]
|
| 41 |
-
idx_rows = self.get_words_tokens_and_indexes_inputs["idx_rows"]
|
| 42 |
-
idx_rows_child = self.get_words_tokens_and_indexes_inputs["idx_rows_child"]
|
| 43 |
-
idx_rows_parent = self.get_words_tokens_and_indexes_inputs["idx_rows_parent"]
|
| 44 |
-
words_stems_dict = get_words_tokens_and_indexes(
|
| 45 |
-
row_words_tokens,
|
| 46 |
-
row_offsets_tokens,
|
| 47 |
-
idx_rows,
|
| 48 |
-
idx_rows_child,
|
| 49 |
-
idx_rows_parent
|
| 50 |
-
)
|
| 51 |
-
with open(EVENTS_FOLDER / "response_text_stemming_with_parents.json", "r") as dst_json:
|
| 52 |
-
response_text_stemming_no_parents = json.load(dst_json)
|
| 53 |
-
expected_words_stems_dict = response_text_stemming_no_parents["words_stems_dict"]
|
| 54 |
-
self.assertDictEqual(words_stems_dict, expected_words_stems_dict)
|
| 55 |
-
|
| 56 |
def test_text_stemming_text(self):
|
| 57 |
from my_ghost_writer.text_parsers import text_stemming
|
| 58 |
self.maxDiff = None
|
| 59 |
n_total_rows, words_stems_dict = text_stemming(self.original_text)
|
| 60 |
self.assertEqual(n_total_rows, len(self.text_split_newline))
|
| 61 |
# with open(EVENTS_FOLDER / "response_text_stemming_from_llm_generated_story_3.json", "w") as dst_json:
|
| 62 |
-
# json.dump(words_stems_dict, dst_json, indent=2)
|
|
|
|
| 63 |
with open(EVENTS_FOLDER / "response_text_stemming_from_llm_generated_story_3.json", "r") as dst_json:
|
| 64 |
-
|
| 65 |
-
expected_words_stems_dict =
|
| 66 |
self.assertDictEqual(words_stems_dict, expected_words_stems_dict)
|
| 67 |
|
| 68 |
def test_text_stemming_input_str_json(self):
|
|
@@ -72,11 +77,11 @@ class TestTextParsers(unittest.TestCase):
|
|
| 72 |
n_total_rows, words_stems_dict = text_stemming(json_str)
|
| 73 |
self.assertEqual(n_total_rows, len(self.text_json_list_no_parents))
|
| 74 |
# with open(EVENTS_FOLDER / "response_text_stemming_empty_rows.json", "w") as dst_json:
|
| 75 |
-
# json.dump(words_stems_dict, dst_json, indent=2)
|
| 76 |
# pass
|
| 77 |
-
with open(EVENTS_FOLDER / "
|
| 78 |
-
|
| 79 |
-
expected_words_stems_dict =
|
| 80 |
self.assertDictEqual(words_stems_dict, expected_words_stems_dict)
|
| 81 |
|
| 82 |
def test_text_stemming_list_no_parents(self):
|
|
@@ -84,7 +89,7 @@ class TestTextParsers(unittest.TestCase):
|
|
| 84 |
self.maxDiff = None
|
| 85 |
n_total_rows, words_stems_dict = text_stemming(self.text_json_list_no_parents)
|
| 86 |
self.assertEqual(n_total_rows, len(self.text_json_list_no_parents))
|
| 87 |
-
# with open(EVENTS_FOLDER / "
|
| 88 |
# json.dump({"n_total_rows": n_total_rows, "words_stems_dict": words_stems_dict}, dst_json, indent=2)
|
| 89 |
# pass
|
| 90 |
with open(EVENTS_FOLDER / "response_text_stemming_no_parents.json", "r") as dst_json:
|
|
@@ -95,9 +100,9 @@ class TestTextParsers(unittest.TestCase):
|
|
| 95 |
def test_text_stemming_list_with_parents(self):
|
| 96 |
from my_ghost_writer.text_parsers import text_stemming
|
| 97 |
self.maxDiff = None
|
| 98 |
-
n_total_rows, words_stems_dict = text_stemming(self.text_json_list_with_parents)
|
| 99 |
self.assertEqual(n_total_rows, len(self.text_json_list_with_parents))
|
| 100 |
-
# with open(EVENTS_FOLDER / "
|
| 101 |
# json.dump({"n_total_rows": n_total_rows, "words_stems_dict": words_stems_dict}, dst_json, indent=2)
|
| 102 |
# pass
|
| 103 |
with open(EVENTS_FOLDER / "response_text_stemming_with_parents.json", "r") as dst_json:
|
|
@@ -134,5 +139,52 @@ class TestTextParsers(unittest.TestCase):
|
|
| 134 |
self.assertEqual(word_offsets, expected_offsets_array)
|
| 135 |
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
if __name__ == "__main__":
|
| 138 |
unittest.main()
|
|
|
|
| 7 |
from tests import EVENTS_FOLDER
|
| 8 |
|
| 9 |
|
| 10 |
+
def get_inputs_for(valid_textrows_with_num):
|
| 11 |
+
row_words_tokens = []
|
| 12 |
+
row_offsets_tokens = []
|
| 13 |
+
idx_rows = []
|
| 14 |
+
idx_rows_child = []
|
| 15 |
+
idx_rows_parent = []
|
| 16 |
+
rows_dict = {}
|
| 17 |
+
for textrow in valid_textrows_with_num:
|
| 18 |
+
row = textrow["text"]
|
| 19 |
+
idx_row = textrow["idxRow"]
|
| 20 |
+
rows_dict[idx_row] = row
|
| 21 |
+
idx_rows.append(idx_row)
|
| 22 |
+
try:
|
| 23 |
+
idx_rows_child.append(textrow["idxRowChild"])
|
| 24 |
+
idx_rows_parent.append(textrow["idxRowParent"])
|
| 25 |
+
except KeyError:
|
| 26 |
+
idx_rows_child.append(None)
|
| 27 |
+
idx_rows_parent.append(None)
|
| 28 |
+
row_words_tokens.append(wordpunct_tokenize(row))
|
| 29 |
+
row_offsets_tokens.append(WordPunctTokenizer().span_tokenize(row))
|
| 30 |
+
return row_words_tokens, row_offsets_tokens, idx_rows, idx_rows_child, idx_rows_parent, rows_dict
|
| 31 |
+
|
| 32 |
+
|
| 33 |
class TestTextParsers(unittest.TestCase):
|
| 34 |
def setUp(self):
|
| 35 |
with open(EVENTS_FOLDER / "get_words_tokens_and_indexes_inputs.json", "r") as src:
|
|
|
|
| 57 |
self.ps = nltk.PorterStemmer()
|
| 58 |
self.wnl = nltk.WordNetLemmatizer()
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def test_text_stemming_text(self):
|
| 61 |
from my_ghost_writer.text_parsers import text_stemming
|
| 62 |
self.maxDiff = None
|
| 63 |
n_total_rows, words_stems_dict = text_stemming(self.original_text)
|
| 64 |
self.assertEqual(n_total_rows, len(self.text_split_newline))
|
| 65 |
# with open(EVENTS_FOLDER / "response_text_stemming_from_llm_generated_story_3.json", "w") as dst_json:
|
| 66 |
+
# json.dump({"n_total_rows": n_total_rows, "words_stems_dict": words_stems_dict}, dst_json, indent=2)
|
| 67 |
+
# pass
|
| 68 |
with open(EVENTS_FOLDER / "response_text_stemming_from_llm_generated_story_3.json", "r") as dst_json:
|
| 69 |
+
response_text_stemming_from_llm_generated_story_3 = json.load(dst_json)
|
| 70 |
+
expected_words_stems_dict = response_text_stemming_from_llm_generated_story_3["words_stems_dict"]
|
| 71 |
self.assertDictEqual(words_stems_dict, expected_words_stems_dict)
|
| 72 |
|
| 73 |
def test_text_stemming_input_str_json(self):
|
|
|
|
| 77 |
n_total_rows, words_stems_dict = text_stemming(json_str)
|
| 78 |
self.assertEqual(n_total_rows, len(self.text_json_list_no_parents))
|
| 79 |
# with open(EVENTS_FOLDER / "response_text_stemming_empty_rows.json", "w") as dst_json:
|
| 80 |
+
# json.dump({"n_total_rows": n_total_rows, "words_stems_dict": words_stems_dict}, dst_json, indent=2)
|
| 81 |
# pass
|
| 82 |
+
with open(EVENTS_FOLDER / "response_text_stemming_empty_rows.json", "r") as dst_json:
|
| 83 |
+
response_text_stemming_empty_rows = json.load(dst_json)
|
| 84 |
+
expected_words_stems_dict = response_text_stemming_empty_rows["words_stems_dict"]
|
| 85 |
self.assertDictEqual(words_stems_dict, expected_words_stems_dict)
|
| 86 |
|
| 87 |
def test_text_stemming_list_no_parents(self):
|
|
|
|
| 89 |
self.maxDiff = None
|
| 90 |
n_total_rows, words_stems_dict = text_stemming(self.text_json_list_no_parents)
|
| 91 |
self.assertEqual(n_total_rows, len(self.text_json_list_no_parents))
|
| 92 |
+
# with open(EVENTS_FOLDER / "response_text_stemming_no_parents.json", "w") as dst_json:
|
| 93 |
# json.dump({"n_total_rows": n_total_rows, "words_stems_dict": words_stems_dict}, dst_json, indent=2)
|
| 94 |
# pass
|
| 95 |
with open(EVENTS_FOLDER / "response_text_stemming_no_parents.json", "r") as dst_json:
|
|
|
|
| 100 |
def test_text_stemming_list_with_parents(self):
|
| 101 |
from my_ghost_writer.text_parsers import text_stemming
|
| 102 |
self.maxDiff = None
|
| 103 |
+
n_total_rows, words_stems_dict = text_stemming(self.text_json_list_with_parents, n=3)
|
| 104 |
self.assertEqual(n_total_rows, len(self.text_json_list_with_parents))
|
| 105 |
+
# with open(EVENTS_FOLDER / "response_text_stemming_with_parents.json", "w") as dst_json:
|
| 106 |
# json.dump({"n_total_rows": n_total_rows, "words_stems_dict": words_stems_dict}, dst_json, indent=2)
|
| 107 |
# pass
|
| 108 |
with open(EVENTS_FOLDER / "response_text_stemming_with_parents.json", "r") as dst_json:
|
|
|
|
| 139 |
self.assertEqual(word_offsets, expected_offsets_array)
|
| 140 |
|
| 141 |
|
| 142 |
+
def test_get_words_tokens_and_indexes_ngrams_no_parents(self):
|
| 143 |
+
from my_ghost_writer.text_parsers import get_words_tokens_and_indexes_ngrams
|
| 144 |
+
|
| 145 |
+
with open(EVENTS_FOLDER / "llm_generated_story_4.txt", "r") as src:
|
| 146 |
+
text = src.read()
|
| 147 |
+
valid_textrows_with_num = [{"idxRow": i, "text": row} for i, row in enumerate(text.split("\n"))]
|
| 148 |
+
|
| 149 |
+
row_words_tokens, row_offsets_tokens, idx_rows, idx_rows_child, idx_rows_parent, rows_dict = get_inputs_for(
|
| 150 |
+
valid_textrows_with_num
|
| 151 |
+
)
|
| 152 |
+
words_stems_dict = get_words_tokens_and_indexes_ngrams(
|
| 153 |
+
row_words_tokens,
|
| 154 |
+
row_offsets_tokens,
|
| 155 |
+
idx_rows,
|
| 156 |
+
idx_rows_child,
|
| 157 |
+
idx_rows_parent,
|
| 158 |
+
rows_dict=rows_dict,
|
| 159 |
+
n=5
|
| 160 |
+
)
|
| 161 |
+
# with open(EVENTS_FOLDER / "response_get_words_tokens_and_indexes_ngrams_text4_n5.json", "w") as dst_json:
|
| 162 |
+
# json.dump({"words_stems_dict": words_stems_dict}, dst_json, indent=2)
|
| 163 |
+
with open(EVENTS_FOLDER / "response_get_words_tokens_and_indexes_ngrams_text4_n5.json", "r") as dst_json:
|
| 164 |
+
response_get_words_tokens_and_indexes_ngrams_text4_n5 = json.load(dst_json)
|
| 165 |
+
expected_words_stems_dict = response_get_words_tokens_and_indexes_ngrams_text4_n5["words_stems_dict"]
|
| 166 |
+
self.assertDictEqual(words_stems_dict, expected_words_stems_dict)
|
| 167 |
+
|
| 168 |
+
def test_get_words_tokens_and_indexes_ngrams_with_parents(self):
|
| 169 |
+
from my_ghost_writer.text_parsers import get_words_tokens_and_indexes_ngrams
|
| 170 |
+
self.maxDiff = None
|
| 171 |
+
row_words_tokens, row_offsets_tokens, idx_rows, idx_rows_child, idx_rows_parent, rows_dict = get_inputs_for(
|
| 172 |
+
self.text_json_list_with_parents
|
| 173 |
+
)
|
| 174 |
+
words_stems_dict = get_words_tokens_and_indexes_ngrams(
|
| 175 |
+
row_words_tokens,
|
| 176 |
+
row_offsets_tokens,
|
| 177 |
+
idx_rows,
|
| 178 |
+
idx_rows_child,
|
| 179 |
+
idx_rows_parent,
|
| 180 |
+
rows_dict=rows_dict,
|
| 181 |
+
n=3
|
| 182 |
+
)
|
| 183 |
+
with open(EVENTS_FOLDER / "response_text_stemming_with_parents.json", "r") as dst_json:
|
| 184 |
+
response_text_stemming_with_parents = json.load(dst_json)
|
| 185 |
+
expected_words_stems_dict = response_text_stemming_with_parents["words_stems_dict"]
|
| 186 |
+
self.assertDictEqual(words_stems_dict, expected_words_stems_dict)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
if __name__ == "__main__":
|
| 190 |
unittest.main()
|