From deb3142a3129246739ec64d72b9e58701f6d0622 Mon Sep 17 00:00:00 2001 From: HOSSEIN Date: Sat, 22 Feb 2025 16:39:34 +0330 Subject: [PATCH] =?UTF-8?q?=D8=A8=D9=87=D8=A8=D9=88=D8=AF=20=DA=A9=D8=A7?= =?UTF-8?q?=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 = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"