import pickle import string import unittest from copy import deepcopy import pandas as pd from CommonServerPython import * from DBotPreprocessTextData import ( Tokenizer, clean_html_from_text, clean_text_of_incidents_list, concat_text_fields, hash_word, is_text_in_input_language, main, pre_process_batch, read_file, remove_duplicate_by_indices, remove_foreign_language, remove_line_breaks, remove_short_text, whitelist_dict_fields, ) def test_clean_html(mocker): html_string = """
My first paragraph
""" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == "My First Heading My first paragraph".lower() args["clean_html"] = False t2 = Tokenizer(**args) res2 = t2.word_tokenize(text) assert re.sub(r"\s+", "", res2["tokenizedText"]) == re.sub(r"\s+", "", text.lower()) def test_number_pattern(self): args = deepcopy(negative_initialization) args["replace_numbers"] = True t1 = Tokenizer(**args) text = "I have 3 dogs" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == "I have {} dogs".lower().format(t1.number_pattern) args["replace_numbers"] = False t1 = Tokenizer(**args) text = "I have 3 dogs" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == text.lower() def test_remove_new_lines(self): args = deepcopy(negative_initialization) args["remove_new_lines"] = True t1 = Tokenizer(**args) text = """ I have 3 dogs I lied """ res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == text.lower().replace("\n", "").replace("\r", "").strip() def test_hash_seed(self): def hash_djb2(s, seed=5381): """ Hash string with djb2 hash function :type s: ``str`` :param s: The input string to hash :type seed: ``int`` :param seed: The seed for the hash function (default is 5381) :return: The hashed value :rtype: ``int`` """ hash_name = seed for x in s: hash_name = ((hash_name << 5) + hash_name) + ord(x) return hash_name & 0xFFFFFFFF args = deepcopy(negative_initialization) args["hash_seed"] = 5 t1 = Tokenizer(**args) text = "hello world" res1 = t1.word_tokenize(text) assert res1["hashedTokenizedText"] == " ".join(str(hash_djb2(word, 5)) for word in text.split()) args["hash_seed"] = None t1 = Tokenizer(**args) text = "hello world" res1 = t1.word_tokenize(text) assert "hashedTokenizedText" not in res1 def test_remove_stop_words(self): args = deepcopy(negative_initialization) args["remove_stop_words"] = False t1 = Tokenizer(**args) text = "let it be" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == text args["remove_stop_words"] = True t1 = Tokenizer(**args) res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == "let" def test_remove_punct(self): args = deepcopy(negative_initialization) args["remove_punct"] = False t1 = Tokenizer(**args) text = "let, it. be!" res1 = t1.word_tokenize(text) expected_result = text for punct in string.punctuation: expected_result = expected_result.replace(punct, " " + punct) assert res1["tokenizedText"] == expected_result args["remove_punct"] = True t1 = Tokenizer(**args) res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == "let it be" def test_remove_non_alpha(self): tested_arg = "remove_non_alpha" args = deepcopy(negative_initialization) args[tested_arg] = False t1 = Tokenizer(**args) text = "see you s00n" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == text args[tested_arg] = True t1 = Tokenizer(**args) res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == "see you" def test_replace_emails(self): tested_arg = "replace_emails" args = deepcopy(negative_initialization) args[tested_arg] = False t1 = Tokenizer(**args) text = "my email is a@gmail.com" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == text args[tested_arg] = True t1 = Tokenizer(**args) res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == f"my email is {t1.email_pattern}" def test_replace_urls(self): tested_arg = "replace_urls" args = deepcopy(negative_initialization) args[tested_arg] = False t1 = Tokenizer(**args) text = "my url is www.google.com" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == text args[tested_arg] = True t1 = Tokenizer(**args) res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == f"my url is {t1.url_pattern}" def test_replace_numbers(self): tested_arg = "replace_numbers" args = deepcopy(negative_initialization) args[tested_arg] = False t1 = Tokenizer(**args) text = "i am 3 years old" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == text args[tested_arg] = True t1 = Tokenizer(**args) res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == f"i am {t1.number_pattern} years old" def test_lemma(self): tested_arg = "lemma" args = deepcopy(negative_initialization) args[tested_arg] = False t1 = Tokenizer(**args) text = "this tokenization method is exceeding my expectations" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == text args[tested_arg] = True t1 = Tokenizer(**args) res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == "this tokenization method be exceed my expectation" def test_max_test_length(self): text = "example sentence" list_text = [text] * 2 args = deepcopy(negative_initialization) t1 = Tokenizer(**args) t1.max_text_length = len(text) + 1 res1 = t1.word_tokenize(list_text) assert all(res1[i]["tokenizedText"] == text for i in range(len(list_text))) t1.max_text_length = len(text) - 1 res1 = t1.word_tokenize(list_text) assert all(res1[i]["tokenizedText"] == "" for i in range(len(list_text))) def test_tokenization_methold(self): tokenization_method = "byWords" language = "fake language" args = deepcopy(negative_initialization) args["tokenization_method"] = tokenization_method args["language"] = language t1 = Tokenizer(**args) text = "example sentence." res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == "".join(c for c in text if c not in string.punctuation) tokenization_method = "byLetters" args["tokenization_method"] = tokenization_method t1 = Tokenizer(**args) text = "example sentence" res1 = t1.word_tokenize(text) assert res1["tokenizedText"] == " ".join(c for c in text if c != " ") def test_original_words_to_tokens(self): t1 = Tokenizer(**negative_initialization) text = "I'm 29 years old and I don't live in Petach Tikva" res1 = t1.word_tokenize(text) expected = { "I'm": ["i", "'m"], "29": ["29"], "years": ["years"], "old": ["old"], "and": ["and"], "I": ["i"], "don't": ["do", "n't"], "live": ["live"], "in": ["in"], "Petach": ["petach"], "Tikva": ["tikva"], } assert res1["originalWordsToTokens"] == expected def test_read_file(mocker): mocker.patch.object(demisto, "getFilePath", return_value={"path": "./TestData/input_json_file_test"}) obj = read_file("231342@343", "json") assert len(obj) >= 1 with open("./TestData/input_json_file_test") as f: obj = read_file(f.read(), "json_string") assert len(obj) >= 1 with open("./TestData/input_pickle_file_test", "wb") as f: f.write(pickle.dumps(obj)) mocker.patch.object(demisto, "getFilePath", return_value={"path": "./TestData/input_pickle_file_test"}) obj_from_pickle = read_file("./TestData/input_pickle_file_test", "pickle") assert len(obj_from_pickle) >= 1 mocker.patch.object(demisto, "getFilePath", return_value={"path": "./TestData/input_json_file_test"}) with open("./TestData/input_json_file_test") as f: obj = read_file(f.read(), "json_string") df = pd.DataFrame.from_dict(obj) df.to_csv("./TestData/test.csv", index=False) mocker.patch.object(demisto, "getFilePath", return_value={"path": "./TestData/test.csv"}) obj2 = read_file("231342@343", "csv") assert len(obj2) == len(obj) with open("./TestData/input_json_file_test") as f: b64_input = base64.b64encode(f.read().encode("utf-8")) obj = read_file(b64_input, "json_b64_string") assert len(obj) >= 1