embeding
This commit is contained in:
+47
-23
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from text_normalizer import TextNormalizer
|
||||
|
||||
# from general import *
|
||||
from general_functions import *
|
||||
from elastic_helper import ElasticHelper
|
||||
@@ -20,7 +21,7 @@ body = {
|
||||
|
||||
|
||||
|
||||
def exportTokensMulti(elastic, index_name_i, prefixname, fields=['content'], body={}):
|
||||
def exportTokensMulti(elastic, index_name_i, prefixname, fields=['content'], body={}, path_out="./data/"):
|
||||
counter = 0
|
||||
total = 0
|
||||
id = ""
|
||||
@@ -44,7 +45,7 @@ def exportTokensMulti(elastic, index_name_i, prefixname, fields=['content'], bod
|
||||
for field in fields :
|
||||
content = entry.get(field, "")
|
||||
# words = content.split()
|
||||
words = re.split(r"[\t\n\r\.\"«»\)\(\]\[,;،؛ \-!?:]+", content)
|
||||
words = text_normal.split_words(content)
|
||||
for word in words:
|
||||
if word in dict_word_count :
|
||||
dict_word_count[word] += 1
|
||||
@@ -52,8 +53,8 @@ def exportTokensMulti(elastic, index_name_i, prefixname, fields=['content'], bod
|
||||
dict_word_count[word] = 1
|
||||
|
||||
|
||||
content = text_normal.normalize_qanon(content)
|
||||
words = re.split(r"[\t\n\r\.\"«»\)\(\]\[,;،؛ \-!?:]+", content)
|
||||
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
|
||||
@@ -65,13 +66,15 @@ def exportTokensMulti(elastic, index_name_i, prefixname, fields=['content'], bod
|
||||
|
||||
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)
|
||||
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))
|
||||
with open(prefixname+"2.txt", "w", encoding="utf-8") as f:
|
||||
json.dump(sorted_dict_words, f, ensure_ascii=False, indent=4)
|
||||
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)
|
||||
@@ -100,9 +103,9 @@ def exportTokens(elastic, index_name_i, fields=['content'], body={}):
|
||||
|
||||
for field in fields :
|
||||
content = entry.get(field, "")
|
||||
content = text_normal.replace_alphabets(content)
|
||||
# content = text_normal.replace_alphabets(content)
|
||||
# words = content.split()
|
||||
words = re.split(r"[\t\n\r\.\"«»\)\(,;،؛ \-!?:]+", content)
|
||||
words = text_normal.split_words(content)
|
||||
|
||||
for word in words:
|
||||
if word in dict_word_count :
|
||||
@@ -115,20 +118,21 @@ def exportTokens(elastic, index_name_i, fields=['content'], body={}):
|
||||
except Exception as e:
|
||||
save_error(id, e)
|
||||
|
||||
def exportTokensToFile(elastic, index_name_i, filename, fields=['content'], body={}):
|
||||
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))
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
json.dump(sorted_dict_words, f, ensure_ascii=False, indent=4)
|
||||
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):
|
||||
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)
|
||||
return diff_to_file(dict1, dict2, filename, False, path_out)
|
||||
|
||||
def diff_to_file(dict1, dict2, filename, by_diff_count=False):
|
||||
def diff_to_file(dict1, dict2, filename, by_diff_count=False, path_out="./data/"):
|
||||
dict_no_exist = {}
|
||||
dict_diff_count = {}
|
||||
for item in dict1:
|
||||
@@ -137,17 +141,37 @@ def diff_to_file(dict1, dict2, filename, by_diff_count=False):
|
||||
elif dict1[item] != dict2[item] :
|
||||
dict_diff_count[item] = dict2[item] - dict1[item]
|
||||
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
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(filename, "a+", encoding="utf-8") as f:
|
||||
with open(path_out+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")
|
||||
|
||||
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"])
|
||||
|
||||
@@ -161,7 +185,7 @@ elastic = ElasticHelper("http://192.168.23.160:6900", "icI1nbkmMQy3WoeaHpBJ")
|
||||
|
||||
# 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")
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user