import os from dotenv import load_dotenv from text_normalizer import TextNormalizer # from general import * from general_functions import * from elastic_helper import ElasticHelper body = { # "query": { # "bool": { # "must_not": [ # {"exists": {"field": "nlp_parser.type"}}, # {"match": {"content_len": 0}}, # {"match": {"parse_state": 1}}, # {"match": {"parse_state": 2}} # ] # } # } } def exportTokensMulti(elastic, index_name_i, prefixname, fields=['content'], body={}, path_out="./data/"): counter = 0 total = 0 id = "" dict_word_count = {} dict_word_count2 = {} try: try: list = elastic.es_iterate_all_documents(index_name_i, body) except Exception as e: save_error(0, e) count = 0 text_normal = TextNormalizer() result = [] for mentry in list: count += 1 entry = mentry["source"] id = entry.get("id", "") for field in fields : content = entry.get(field, "") # words = content.split() words = text_normal.split_words(content) for word in words: if word in dict_word_count : dict_word_count[word] += 1 else : dict_word_count[word] = 1 content = text_normal.normalize_qanon_test(content) words = text_normal.split_words(content) for word in words: if word in dict_word_count2 : dict_word_count2[word] += 1 else : dict_word_count2[word] = 1 if dict_word_count : sorted_dict_words = dict(sorted(dict_word_count.items(), key=lambda x: x[1], reverse=True)) filtered_dict = {k:v for (k,v) in sorted_dict_words.items() if not re.match(r'[a-zA-Z\d]+', k) } with open(path_out+prefixname+"1.txt", "w", encoding="utf-8") as f: json.dump(filtered_dict, f, ensure_ascii=False, indent=4) if dict_word_count2 : sorted_dict_words = dict(sorted(dict_word_count2.items(), key=lambda x: x[1], reverse=True)) filtered_dict = {k:v for (k,v) in sorted_dict_words.items() if not re.match(r'[a-zA-Z\d]+', k) } with open(path_out+prefixname+"2.txt", "w", encoding="utf-8") as f: json.dump(filtered_dict, f, ensure_ascii=False, indent=4) except Exception as e: save_error(id, e) def exportTokens(elastic, index_name_i, fields=['content'], body={}): dict_word_count={} counter = 0 total = 0 id = "" try: try: list = elastic.es_iterate_all_documents(index_name_i, body) except Exception as e: save_error(0, e) count = 0 text_normal = TextNormalizer() result = [] for mentry in list: count += 1 entry = mentry["source"] id = entry.get("id", "") for field in fields : content = entry.get(field, "") # content = text_normal.replace_alphabets(content) # words = content.split() words = text_normal.split_words(content) for word in words: if word in dict_word_count : dict_word_count[word] += 1 else : dict_word_count[word] = 1 return dict(dict_word_count) except Exception as e: save_error(id, e) def exportTokensToFile(elastic, index_name_i, filename, fields=['content'], body={}, path_out="./data/"): dict_words = exportTokens(elastic, index_name_i, fields, body) if dict_words : sorted_dict_words = dict(sorted(dict_words.items(), key=lambda x: x[1], reverse=True)) filtered_dict = {k:v for (k,v) in sorted_dict_words.items() if not re.match(r'[a-zA-Z\d]+', k) } with open(path_out+filename, "w", encoding="utf-8") as f: json.dump(filtered_dict, f, ensure_ascii=False, indent=4) def diff_files_to_file(file_dict1, file_dict2, filename, path_out="./data/"): dict1 = json.loads(open(file_dict1, "r", encoding="utf-8").read()) dict2 = json.loads(open(file_dict2, "r", encoding="utf-8").read()) return diff_to_file(dict1, dict2, filename, False, path_out) def diff_to_file(dict1, dict2, filename, by_diff_count=False, path_out="./data/"): dict_no_exist = {} dict_diff_count = {} for item in dict1: if item not in dict2 : dict_no_exist[item] = dict1[item] elif dict1[item] != dict2[item] : dict_diff_count[item] = dict2[item] - dict1[item] with open(path_out+filename, "w", encoding="utf-8") as f: json.dump(dict_no_exist, f, ensure_ascii=False, indent=4) if by_diff_count : with open(path_out+filename, "a+", encoding="utf-8") as f: json.dump(dict_diff_count, f, ensure_ascii=False, indent=4) load_dotenv(dotenv_path='config.env') host=os.getenv('HOST_160') password=os.getenv('HOST_160_PASS') # host=os.getenv('HOST_112') # password=os.getenv('HOST_112_PASS') # host=os.getenv('HOST_60') # password=os.getenv('HOST_60_PASS') print(host) # print(password) path_base_map = "G:/Project2/gateway2_main/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) #exportTokensToFile(elastic,"mj_qa_section", "content_mj_qa_section_1.txt") # exportTokensToFile(elastic,"test_qa_section", "content_test_qa_section_1.txt") # exportTokensToFile(elastic,"test_qa_section", "content_test_qa_section_normal_1.txt", ["content_normal"]) # diff_files_to_file("content_mj_qa_section_1.txt", "content_test_qa_section_1.txt", "diff_prev_content.txt") # diff_files_to_file("content_test_qa_section_1.txt", "content_mj_qa_section_1.txt", "diff_content_prev.txt") # diff_files_to_file("content_test_qa_section_normal_1.txt", "content_test_qa_section_1.txt", "diff_normal_content.txt") # diff_files_to_file("content_test_qa_section_1.txt", "content_test_qa_section_normal_1.txt", "diff_content_normal.txt") # exportTokensToFile(elastic,"mj_qa_section", "content_mj_qa_section_1.txt") exportTokensMulti(elastic,"mj_qa_section", "mj_qa_section") diff_files_to_file("mj_qa_section1.txt", "mj_qa_section2.txt", "diff_1_2.txt") diff_files_to_file("mj_qa_section2.txt", "mj_qa_section1.txt", "diff_2_1.txt") print("finished")