rag_api/funcs.py
2025-03-05 16:05:44 +03:30

68 lines
1.9 KiB
Python

import json
def read_file_by_address(file_address):
with open(file_address, 'a+', encoding='utf-8') as file:
text = ''
try:
# Move the cursor to the beginning of the file to read its content
file.seek(0)
text = file.read()
except:
pass
return text
def save_to_file_by_address(file_address, text):
with open(file_address, 'a+', encoding='utf-8') as file:
previous_result = ''
try:
previous_result = file.read()
except:
pass
file.write(text)
file.close()
def create_curpos(laws_list:dict):
sections_170k = []
for law_id, laws_sections in laws_list.items():
sections = laws_sections
for section in sections:
sections_170k.append(section)
return sections_170k
def write_to_json(dict, file_address):
# تبدیل دیکشنری به فرمت JSON
json_data = json.dumps(dict, indent=2, ensure_ascii=False)
# ذخیره فایل
with open(file_address, 'w+', encoding='utf-8') as file:
file.write(json_data)
return True
def read_from_json(file_address):
data_dict = []
# خواندن اطلاعات از فایل JSON
with open(file_address, 'r', encoding='utf-8') as file:
loaded_data = json.load(file)
# نمایش اطلاعات خوانده شده
# for item in loaded_data:
# data_dict.append(item)
return loaded_data
def read_dict_from_json(file_address):
# خواندن اطلاعات از فایل JSON
with open(file_address, 'r', encoding='utf-8') as file:
loaded_data = json.load(file)
return loaded_data
if __name__ == "__main__":
print('start ... ')
laws_list = read_dict_from_json('./data/laws_list_170k_embed2.json')
sections_170k = create_curpos(laws_list=laws_list)
write_to_json(sections_170k, './data/sections_170k.json')
print('finished!')