This commit is contained in:
hsafaei
2025-02-15 17:11:26 +03:30
4 changed files with 1395 additions and 54 deletions
+56
View File
@@ -0,0 +1,56 @@
[
{
"start": 1,
"end": 2,
"text": "وزارتخانه ها",
"type": "ORG2"
},
{
"start": 8,
"end": 12,
"text": "وزارت فرهنگ و ارشاد اسلامی",
"type": "ORG"
},
{
"start": 13,
"end": 14,
"text": "وزارت کشور",
"type": "ORG"
},
{
"start": 19,
"end": 20,
"text": "وزارت نیرو",
"type": "ORG"
},
{
"start": 23,
"end": 26,
"text": "سازمان حفظ محیط زیست",
"type": "ORG"
},
{
"start": 34,
"end": 38,
"text": "قانون اساسی جمهوری اسلامی ایران",
"type": "ref"
},
{
"start": 44,
"end": 55,
"text": "قانون الحاق یک ماده به قانون حمایت از خانواده و جوانی جمعیت",
"type": "ref"
},
{
"start": 58,
"end": 59,
"text": "تهران تهران",
"type": "loc"
},
{
"start": 61,
"end": 62,
"text": "قم جعفريه",
"type": "loc"
}
]
File diff suppressed because one or more lines are too long
+1258
View File
File diff suppressed because it is too large Load Diff
+77 -49
View File
@@ -2,99 +2,127 @@ import re
import json
from text_normalizer import TextNormalizer
def recerciveMatch(startIndex , i, tokens, dict, result) :
def recerciveMatch(startIndex , i, tokens, dict, result, entity_type):
end = -1
if i >= len(tokens) :
if i >= len(tokens):
return end
word = tokens[i]
if word in dict :
end = recerciveMatch(startIndex , i+1, tokens, dict[word], result)
if end and end != -1 : # find
if word in dict:
end = recerciveMatch(startIndex, i+1, tokens, dict[word], result, entity_type)
if end and end != -1:
return end
elif '=' in dict[word] :
elif '=' in dict[word]:
end = i+1
result.append({
"start" : startIndex,
"end" : i,
"text": " ".join(tokens[startIndex: end]),
"type" : dict[word]['=']
"start": startIndex,
"end": i,
"text": " ".join(tokens[startIndex:end]),
"type": dict[word]['='],
# "entity": entity_type
})
return end
else :
else:
return end
def find_organizations(text):
global result_dict
def find_entities(text, entity_dict, entity_type):
text_normal = TextNormalizer()
#words = text.split()
words = text_normal.split_words()
words = text_normal.split_words(text)
results = []
i = 0
while i < len(words):
#print('i =', i)
end = recerciveMatch(i, i, words, result_dict, results)
#print(end)
if end and end != -1 :
i=end
end = recerciveMatch(i, i, words, entity_dict, results, entity_type)
if end and end != -1:
i = end
else:
i=i+1
i += 1
return results
def create_nested_dictionary(lines, nested_dict={}):
text_normal = TextNormalizer()
#nested_dict = {}
for line in lines:
line = text_normal.normalize_qanon(line)
parts = line.split("=")
#words = re.findall(r'\b\w+\b', parts[0])
words = text_normal.split_words(parts[0])
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 :
# دو عنوان دقیقا مشابه هم با دو تگ مختلف
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
else:
dict_current_level['='] = tag
return nested_dict
def create_title_tree(title, tree):
words = title.split()
current_level = tree
for word in words:
if word not in current_level:
current_level[word] = {}
current_level = current_level[word]
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
### -----------------------------------------------
# Example usage:
try:
with open('./data/list_ORG.txt', 'r', encoding='utf-8') as f:
text = f.read()
org_text = f.read()
except FileNotFoundError:
print("Error: ORG+.txt file not found.")
print("Error: list_ORG.txt file not found.")
exit()
org_lines = org_text.strip().split('\n')
organization_dict = create_nested_dictionary(org_lines)
lines = text.strip().split('\n')
result_dict = create_nested_dictionary(lines)
try:
with open("./data/qanon_titles.json", "r", encoding="utf-8") as f:
title_data = json.load(f)
except FileNotFoundError:
print("Error: qanon_titles.json file not found.")
exit()
#text = "وزارت فرهنگ و ارشاد اسلامی با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
#text = "تمامی وزارتخانه‌ها و سازمانهای کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
title_tree = {}
for item in title_data:
create_title_tree(item["title"], title_tree)
text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
province_tree = create_province_tree('./data/ostan.txt')
#text = "تمامی ## و ## ## از جمله ## ## ## ## ## ## ## با جمعی از کارکنان ## ## در محل ## ## ## ## دیدار و گفتگو کردند"
text = "تمامی وزارتخانه ها و سازمان‌های کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند. همچنین ماده 5 قانون اساسی جمهوری اسلامی ایران بر این امر تأکید دارد. قانون الحاق یک ماده به قانون حمایت از خانواده و جوانی جمعیت در استان تهران تهران و قم جعفريه"
#text = "ماده 1 - براساس اصل 45 قانون اساسی جمهوری اسلامی ایران، آبهای دریاها و آبهای جاری در رودها و انهار طبیعی و دره‌ها و هر مسیر طبیعی ‌دیگر اعم از سطحی و زیر زمینی، سیلابها و فاضلابها و زه‌آبها و دریاچه‌ها و مردابها و برکه‌های طبیعی و چشمه‌ سارها و آبهای معدنی و منابع آبهای زیرزمینی از مشترکات بوده و در اختیار حکومت اسلامی است و طبق مصالح عامه از آنها بهره ‌برداری میشود. مسئولیت حفظ و اجازه و نظارت بر بهره‌ برداری از آنها به دولت محول میشود."
organizations_file = "organizations.json"
found_orgs = find_entities(text, organization_dict, "organization")
found_titles = find_entities(text, title_tree, "law_title")
found_provinces = find_entities(text, province_tree, "province")
found_orgs = find_organizations(text)
print(found_orgs)
# ترکیب نتایج
found_entities = found_orgs + found_titles + found_provinces
# ذخیره خروجی در فایل JSON
output_file = "found_entities.json"
with open(output_file, "w", encoding="utf-8") as f:
json.dump(found_entities, f, ensure_ascii=False, indent=4)
print(f"نتایج در فایل {output_file} ذخیره شد.")