بهبود کارایی کد
This commit is contained in:
+28
-15
@@ -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 = "تمامی وزارتخانه ها و سازمانهای کشور از جمله وزارت فرهنگ و ارشاد اسلامی وزارت کشور با جمعی از کارکنان وزارت نیرو در محل سازمان حفظ محیط زیست دیدار و گفتگو کردند"
|
||||
|
||||
Reference in New Issue
Block a user