This commit is contained in:
hsafaei
2025-02-22 16:44:17 +03:30
2 changed files with 50 additions and 40 deletions
Submodule SqlToElasticMajles added at 43f46a17a3
+50 -41
View File
@@ -2,6 +2,13 @@ import re
import json import json
from text_normalizer import TextNormalizer from text_normalizer import TextNormalizer
text_normal = TextNormalizer()
def tag_separation(text, default_tag):
parts = text.split("=")
title = parts[0].strip()
tag = parts[1].strip() if len(parts) > 1 else default_tag
return title, tag
def recerciveMatch(startIndex , i, tokens, dict, result): def recerciveMatch(startIndex , i, tokens, dict, result):
end = -1 end = -1
if i >= len(tokens): if i >= len(tokens):
@@ -26,7 +33,6 @@ def recerciveMatch(startIndex , i, tokens, dict, result):
return end return end
def find_entities(text, entity_dict): def find_entities(text, entity_dict):
text_normal = TextNormalizer()
words = text_normal.split_words(text) words = text_normal.split_words(text)
results = [] results = []
i = 0 i = 0
@@ -39,55 +45,50 @@ def find_entities(text, entity_dict):
return results return results
def create_nested_dictionary(lines, nested_dict={}): def create_nested_dictionary(lines, nested_dict={}):
text_normal = TextNormalizer()
for line in lines: for line in lines:
line = text_normal.normalize_qanon(line) line = text_normal.normalize_qanon(line)
parts = line.split("=") title, tag = tag_separation(line, '')
words = text_normal.split_words(parts[0]) words = text_normal.split_words(title)
dict_current_level = nested_dict dict_current_level = nested_dict
for word in words: for word in words:
if word not in dict_current_level: if word not in dict_current_level:
dict_current_level[word] = {} dict_current_level[word] = {}
dict_current_level = dict_current_level[word] dict_current_level = dict_current_level[word]
tag = parts[1] if len(parts) > 1 else '' if '=' in dict_current_level and dict_current_level['='] != tag:
if '=' in dict_current_level: dict_current_level['='] += ',' + tag
# دو عنوان دقیقا مشابه هم با دو تگ مختلف
if dict_current_level['='] != tag:
dict_current_level['='] = dict_current_level['='] + ',' + tag
else: else:
dict_current_level['='] = tag dict_current_level['='] = tag
return nested_dict return nested_dict
def create_title_tree(title, tree): def create_title_tree(title_with_tag, tree):
words = title.split() title, tag = tag_separation(title_with_tag, "ref")
words = text_normal.split_words(title)
dict_current_level = tree dict_current_level = tree
for word in words: for word in words:
if word not in dict_current_level: if word not in dict_current_level:
dict_current_level[word] = {} dict_current_level[word] = {}
dict_current_level = dict_current_level[word] dict_current_level = dict_current_level[word]
dict_current_level['='] = "ref" dict_current_level['='] = tag
def create_province_tree(input_file):
province_tree = {}
try:
with open(input_file, 'r', encoding='utf-8') as f:
for line in f:
text_normal = TextNormalizer() # ایجاد شی TextNormalizer بدون پارامتر
words = text_normal.split_words(line.strip()) # ارسال خط متنی به متد split_words
if len(words) >= 2: # اطمینان از اینکه حداقل دو کلمه داریم
province, city = words[:2] # استفاده از دو مقدار اول
if province not in province_tree:
province_tree[province] = {}
province_tree[province][city] = {"=": "loc"}
else:
print(f"Warning: Insufficient data in line: {line.strip()}")
except FileNotFoundError:
print(f"Error: {input_file} file not found.")
exit()
return province_tree
def create_province_tree(data, tree):
for key, values in data.items():
province_name, province_tag = tag_separation(key, "loc")
words = text_normal.split_words(province_name)
dict_current_level = tree
for word in words:
if word not in dict_current_level:
dict_current_level[word] = {}
dict_current_level = dict_current_level[word]
dict_current_level['='] = province_tag
for value in values:
city_name, city_tag = tag_separation(value, "loc")
city_words = text_normal.split_words(city_name)
city_level = tree
for word in city_words:
if word not in city_level:
city_level[word] = {}
city_level = city_level[word]
city_level['='] = city_tag
try: try:
with open('./data/list_ORG.txt', 'r', encoding='utf-8') as f: with open('./data/list_ORG.txt', 'r', encoding='utf-8') as f:
@@ -110,15 +111,23 @@ title_tree = {}
for item in title_data: for item in title_data:
create_title_tree(item["title"], title_tree) create_title_tree(item["title"], title_tree)
province_tree = create_province_tree('./data/ostan.txt')
#text = "وزارت فرهنگ و ارشاد اسلامی با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
#text = "تمامی وزارتخانه‌ها و سازمانهای کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
#text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
#text = "تمامی ## و ## ## از جمله ## ## ## ## ## ## ## با جمعی از کارکنان ## ## در محل ## ## ## ## دیدار و گفتگو کردند"
#text = "ماده 1 - براساس اصل 45 قانون اساسی جمهوری اسلامی ایران، آبهای دریاها و آبهای جاری در رودها و انهار طبیعی و دره‌ها و هر مسیر طبیعی ‌دیگر اعم از سطحی و زیر زمینی، سیلابها و فاضلابها و زه‌آبها و دریاچه‌ها و مردابها و برکه‌های طبیعی و چشمه‌ سارها و آبهای معدنی و منابع آبهای زیرزمینی از مشترکات بوده و در اختیار حکومت اسلامی است و طبق مصالح عامه از آنها بهره ‌برداری میشود. مسئولیت حفظ و اجازه و نظارت بر بهره‌ برداری از آنها به دولت محول میشود."
text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند. همچنین ماده 5 قانون اساسی جمهوری اسلامی ایران بر این امر تأکید دارد. قانون الحاق یک ماده به قانون حمایت از خانواده و جوانی جمعیت در استان تهران تهران و قم جعفريه" text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند. همچنین ماده 5 قانون اساسی جمهوری اسلامی ایران بر این امر تأکید دارد. قانون الحاق یک ماده به قانون حمایت از خانواده و جوانی جمعیت در استان تهران تهران و قم جعفريه"
# #text = "وزارت فرهنگ و ارشاد اسلامی با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
# #text = "تمامی وزارتخانه‌ها و سازمانهای کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
# #text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
# #text = "تمامی ## و ## ## از جمله ## ## ## ## ## ## ## با جمعی از کارکنان ## ## در محل ## ## ## ## دیدار و گفتگو کردند"
# #text = "ماده 1 - براساس اصل 45 قانون اساسی جمهوری اسلامی ایران، آبهای دریاها و آبهای جاری در رودها و انهار طبیعی و دره‌ها و هر مسیر طبیعی ‌دیگر اعم از سطحی و زیر زمینی، سیلابها و فاضلابها و زه‌آبها و دریاچه‌ها و مردابها و برکه‌های طبیعی و چشمه‌ سارها و آبهای معدنی و منابع آبهای زیرزمینی از مشترکات بوده و در اختیار حکومت اسلامی است و طبق مصالح عامه از آنها بهره ‌برداری میشود. مسئولیت حفظ و اجازه و نظارت بر بهره‌ برداری از آنها به دولت محول میشود."
try:
with open("./data/province_tree.json", "r", encoding="utf-8") as f:
province_data = json.load(f)
except FileNotFoundError:
print("Error: province_tree.json file not found.")
exit()
province_tree = {}
create_province_tree(province_data, province_tree)
found_orgs = find_entities(text, organization_dict) found_orgs = find_entities(text, organization_dict)
found_titles = find_entities(text, title_tree) found_titles = find_entities(text, title_tree)