diff --git a/SqlToElasticMajles b/SqlToElasticMajles new file mode 160000 index 0000000..43f46a1 --- /dev/null +++ b/SqlToElasticMajles @@ -0,0 +1 @@ +Subproject commit 43f46a17a3c0cd254cc0c2ab6a7c1d33a543f4b0 diff --git a/tagging_ner_lists.py b/tagging_ner_lists.py index 0009560..ac567e8 100644 --- a/tagging_ner_lists.py +++ b/tagging_ner_lists.py @@ -2,6 +2,13 @@ import re import json 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): end = -1 if i >= len(tokens): @@ -26,7 +33,6 @@ def recerciveMatch(startIndex , i, tokens, dict, result): return end def find_entities(text, entity_dict): - text_normal = TextNormalizer() words = text_normal.split_words(text) results = [] i = 0 @@ -39,55 +45,50 @@ def find_entities(text, entity_dict): return results def create_nested_dictionary(lines, nested_dict={}): - text_normal = TextNormalizer() for line in lines: line = text_normal.normalize_qanon(line) - parts = line.split("=") - words = text_normal.split_words(parts[0]) + title, tag = tag_separation(line, '') + words = text_normal.split_words(title) dict_current_level = nested_dict for word in words: if word not in 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: - # دو عنوان دقیقا مشابه هم با دو تگ مختلف - if dict_current_level['='] != tag: - dict_current_level['='] = dict_current_level['='] + ',' + tag + if '=' in dict_current_level and dict_current_level['='] != tag: + dict_current_level['='] += ',' + tag else: dict_current_level['='] = tag return nested_dict -def create_title_tree(title, tree): - words = title.split() +def create_title_tree(title_with_tag, tree): + title, tag = tag_separation(title_with_tag, "ref") + words = text_normal.split_words(title) 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['='] = "ref" - -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 - - + dict_current_level['='] = tag +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: with open('./data/list_ORG.txt', 'r', encoding='utf-8') as f: @@ -110,15 +111,23 @@ title_tree = {} for item in title_data: create_title_tree(item["title"], title_tree) -province_tree = create_province_tree('./data/ostan.txt') +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) -#text = "وزارت فرهنگ و ارشاد اسلامی با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند" -#text = "تمامی وزارتخانه‌ها و سازمانهای کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند" -#text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند" -#text = "تمامی ## و ## ## از جمله ## ## ## ## ## ## ## با جمعی از کارکنان ## ## در محل ## ## ## ## دیدار و گفتگو کردند" -#text = "ماده 1 - براساس اصل 45 قانون اساسی جمهوری اسلامی ایران، آبهای دریاها و آبهای جاری در رودها و انهار طبیعی و دره‌ها و هر مسیر طبیعی ‌دیگر اعم از سطحی و زیر زمینی، سیلابها و فاضلابها و زه‌آبها و دریاچه‌ها و مردابها و برکه‌های طبیعی و چشمه‌ سارها و آبهای معدنی و منابع آبهای زیرزمینی از مشترکات بوده و در اختیار حکومت اسلامی است و طبق مصالح عامه از آنها بهره ‌برداری میشود. مسئولیت حفظ و اجازه و نظارت بر بهره‌ برداری از آنها به دولت محول میشود." -text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند. همچنین ماده 5 قانون اساسی جمهوری اسلامی ایران بر این امر تأکید دارد. قانون الحاق یک ماده به قانون حمایت از خانواده و جوانی جمعیت در استان تهران تهران و قم جعفريه" found_orgs = find_entities(text, organization_dict) found_titles = find_entities(text, title_tree)