From d0cc83f12906fe910350f1b0884af3844fe7a4e7 Mon Sep 17 00:00:00 2001 From: HOSSEIN Date: Mon, 17 Feb 2025 19:12:02 +0330 Subject: [PATCH 1/2] =?UTF-8?q?=D8=AC=D8=AF=D8=A7=D8=B3=D8=A7=D8=B2=DB=8C?= =?UTF-8?q?=20=D8=A7=D8=B3=D8=AA=D8=A7=D9=86=20=D9=88=20=D8=B4=D9=87=D8=B1?= =?UTF-8?q?=20=D8=A7=D8=B2=20=D9=85=D8=AA=D9=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SqlToElasticMajles | 1 + tagging_ner_lists.py | 52 ++++++++++++++++++++------------------------ 2 files changed, 25 insertions(+), 28 deletions(-) create mode 160000 SqlToElasticMajles 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..ec881f9 100644 --- a/tagging_ner_lists.py +++ b/tagging_ner_lists.py @@ -67,27 +67,15 @@ def create_title_tree(title, tree): 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 - - - +def create_province_tree(data, tree): + for key, values in data.items(): + words = key.split() + 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['='] = "loc" try: with open('./data/list_ORG.txt', 'r', encoding='utf-8') as f: @@ -110,15 +98,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) From deb3142a3129246739ec64d72b9e58701f6d0622 Mon Sep 17 00:00:00 2001 From: HOSSEIN Date: Sat, 22 Feb 2025 16:39:34 +0330 Subject: [PATCH 2/2] =?UTF-8?q?=D8=A8=D9=87=D8=A8=D9=88=D8=AF=20=DA=A9?= =?UTF-8?q?=D8=A7=D8=B1=D8=A7=DB=8C=DB=8C=20=DA=A9=D8=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tagging_ner_lists.py | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/tagging_ner_lists.py b/tagging_ner_lists.py index ec881f9..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,43 +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" + dict_current_level['='] = tag def create_province_tree(data, tree): for key, values in data.items(): - words = key.split() + 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['='] = "loc" + 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: @@ -98,7 +111,7 @@ title_tree = {} for item in title_data: create_title_tree(item["title"], title_tree) -text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند. همچنین ماده 5 قانون اساسی جمهوری اسلامی ایران بر این امر تأکید دارد. قانون الحاق یک ماده به قانون حمایت از خانواده و جوانی جمعیت در استان تهران تهران و قم جعفریه" +text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند. همچنین ماده 5 قانون اساسی جمهوری اسلامی ایران بر این امر تأکید دارد. قانون الحاق یک ماده به قانون حمایت از خانواده و جوانی جمعیت در استان تهران تهران و قم جعفريه" # #text = "وزارت فرهنگ و ارشاد اسلامی با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند" # #text = "تمامی وزارتخانه‌ها و سازمانهای کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند" # #text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"