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