refactor
This commit is contained in:
@@ -5,6 +5,7 @@ node_modules
|
||||
/html
|
||||
data_back/
|
||||
data/
|
||||
output/
|
||||
|
||||
# local env files
|
||||
config.env
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
[
|
||||
{
|
||||
"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"
|
||||
}
|
||||
]
|
||||
-1258
File diff suppressed because it is too large
Load Diff
+19
-12
@@ -2,36 +2,36 @@ import re
|
||||
import json
|
||||
from text_normalizer import TextNormalizer
|
||||
|
||||
def recerciveMatch(startIndex , i, tokens, dict, result, entity_type):
|
||||
def recerciveMatch(startIndex , i, tokens, dict, result):
|
||||
end = -1
|
||||
if i >= len(tokens):
|
||||
return end
|
||||
|
||||
word = tokens[i]
|
||||
if word in dict:
|
||||
end = recerciveMatch(startIndex, i+1, tokens, dict[word], result, entity_type)
|
||||
if end and end != -1:
|
||||
end = recerciveMatch(startIndex, i+1, tokens, dict[word], result)
|
||||
if end and end != -1: # finded
|
||||
return end
|
||||
elif '=' in dict[word]:
|
||||
end = i+1
|
||||
result.append({
|
||||
"start": startIndex,
|
||||
"begin": startIndex,
|
||||
"end": i,
|
||||
"text": " ".join(tokens[startIndex:end]),
|
||||
"type": dict[word]['='],
|
||||
# "entity": entity_type
|
||||
"result_key": dict[word]['='],
|
||||
|
||||
})
|
||||
return end
|
||||
else:
|
||||
return end
|
||||
|
||||
def find_entities(text, entity_dict, entity_type):
|
||||
def find_entities(text, entity_dict):
|
||||
text_normal = TextNormalizer()
|
||||
words = text_normal.split_words(text)
|
||||
results = []
|
||||
i = 0
|
||||
while i < len(words):
|
||||
end = recerciveMatch(i, i, words, entity_dict, results, entity_type)
|
||||
end = recerciveMatch(i, i, words, entity_dict, results)
|
||||
if end and end != -1:
|
||||
i = end
|
||||
else:
|
||||
@@ -51,6 +51,7 @@ def create_nested_dictionary(lines, nested_dict={}):
|
||||
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
|
||||
else:
|
||||
@@ -111,17 +112,23 @@ for item in title_data:
|
||||
|
||||
province_tree = create_province_tree('./data/ostan.txt')
|
||||
|
||||
|
||||
#text = "وزارت فرهنگ و ارشاد اسلامی با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
|
||||
#text = "تمامی وزارتخانهها و سازمانهای کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
|
||||
#text = "تمامی وزارتخانه ها و سازمانهای کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
|
||||
#text = "تمامی ## و ## ## از جمله ## ## ## ## ## ## ## با جمعی از کارکنان ## ## در محل ## ## ## ## دیدار و گفتگو کردند"
|
||||
#text = "ماده 1 - براساس اصل 45 قانون اساسی جمهوری اسلامی ایران، آبهای دریاها و آبهای جاری در رودها و انهار طبیعی و درهها و هر مسیر طبیعی دیگر اعم از سطحی و زیر زمینی، سیلابها و فاضلابها و زهآبها و دریاچهها و مردابها و برکههای طبیعی و چشمه سارها و آبهای معدنی و منابع آبهای زیرزمینی از مشترکات بوده و در اختیار حکومت اسلامی است و طبق مصالح عامه از آنها بهره برداری میشود. مسئولیت حفظ و اجازه و نظارت بر بهره برداری از آنها به دولت محول میشود."
|
||||
text = "تمامی وزارتخانه ها و سازمانهای کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند. همچنین ماده 5 قانون اساسی جمهوری اسلامی ایران بر این امر تأکید دارد. قانون الحاق یک ماده به قانون حمایت از خانواده و جوانی جمعیت در استان تهران تهران و قم جعفريه"
|
||||
|
||||
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_entities(text, organization_dict)
|
||||
found_titles = find_entities(text, title_tree)
|
||||
found_provinces = find_entities(text, province_tree)
|
||||
|
||||
# ترکیب نتایج
|
||||
found_entities = found_orgs + found_titles + found_provinces
|
||||
|
||||
# ذخیره خروجی در فایل JSON
|
||||
output_file = "found_entities.json"
|
||||
output_file = "./output/ner_found_entities.json"
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
json.dump(found_entities, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user