168 lines
5.7 KiB
Python
168 lines
5.7 KiB
Python
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={}):
|
|
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 = re.split(r"[\t\n\r\.\"«»\)\(\]\[,;،؛ \-!?:]+", 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(content)
|
|
words = re.split(r"[\t\n\r\.\"«»\)\(\]\[,;،؛ \-!?:]+", 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))
|
|
with open(prefixname+".txt", "w", encoding="utf-8") as f:
|
|
json.dump(sorted_dict_words, 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))
|
|
with open(prefixname+"2.txt", "w", encoding="utf-8") as f:
|
|
json.dump(sorted_dict_words, 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 = re.split(r"[\t\n\r\.\"«»\)\(,;،؛ \-!?:]+", 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={}):
|
|
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))
|
|
with open(filename, "w", encoding="utf-8") as f:
|
|
json.dump(sorted_dict_words, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
def diff_files_to_file(file_dict1, file_dict2, filename):
|
|
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)
|
|
|
|
def diff_to_file(dict1, dict2, filename, by_diff_count=False):
|
|
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(filename, "w", encoding="utf-8") as f:
|
|
json.dump(dict_no_exist, f, ensure_ascii=False, indent=4)
|
|
|
|
if by_diff_count :
|
|
with open(filename, "a+", encoding="utf-8") as f:
|
|
json.dump(dict_diff_count, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
elastic = ElasticHelper("http://192.168.23.160:6900", "icI1nbkmMQy3WoeaHpBJ")
|
|
|
|
# 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_section.txt", "mj_qa_section2.txt", "diff_1_2.txt")
|
|
diff_files_to_file("mj_qa_section2.txt", "mj_qa_section.txt", "diff_2_1.txt")
|
|
|
|
print("finished")
|