to-164
This commit is contained in:
Executable
+400
@@ -0,0 +1,400 @@
|
||||
import os
|
||||
import json
|
||||
import orjson
|
||||
from pathlib import Path
|
||||
from collections import defaultdict
|
||||
from aiDataParser import AsyncCore
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional, Dict
|
||||
|
||||
|
||||
def load_orjson(path: str | Path):
|
||||
path = Path(path)
|
||||
with path.open("rb") as f:
|
||||
return orjson.loads(f.read())
|
||||
|
||||
|
||||
def save_orjson(path, data):
|
||||
with open(path, "wb") as f:
|
||||
f.write(
|
||||
orjson.dumps(data, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS)
|
||||
)
|
||||
|
||||
###################### len
|
||||
path = "/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/description_validdata1240000.json"
|
||||
data = load_orjson(path)
|
||||
print(f'path {len(data)}')
|
||||
unique = set([i['word'] for i in data])
|
||||
print(f'unique {len(list(unique))}')
|
||||
######### check
|
||||
# print("[","".join([
|
||||
# "ٔ",
|
||||
# "ٰ",
|
||||
# "ْ",
|
||||
# "",
|
||||
# "_"
|
||||
# ]),"]", sep="")
|
||||
# _path = '/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/all_word.json'
|
||||
# total_data = load_orjson(_path)
|
||||
# f_total_data =set()
|
||||
# f_total_char =set()
|
||||
# for i in total_data:
|
||||
# # item = i.split(' ')
|
||||
# # for j in item :
|
||||
# item = i.strip()
|
||||
# if len(item) >1:
|
||||
# f_total_data.add()
|
||||
# f_total_char.update(i)
|
||||
|
||||
# f_total_data = list(f_total_data)
|
||||
# f_total_char = list(f_total_char)
|
||||
# print(
|
||||
# f'f_total_data {len(f_total_data)}',
|
||||
# f'f_total_char {len(f_total_char)}',
|
||||
# sep='\n'
|
||||
# )
|
||||
# f_total_data = sorted(f_total_data, key=lambda i: ord(str(i)[0]) if str(i) else 0)
|
||||
# f_total_data = sorted(f_total_data, key=lambda i: ord(str(i)[0]) if str(i) else 0)
|
||||
# save_orjson(
|
||||
# data=f_total_data,
|
||||
# path=_path,
|
||||
# )
|
||||
# save_orjson(
|
||||
# data=f_total_char,
|
||||
# path="/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/f_total_char.json",
|
||||
# )
|
||||
######### test and numbering - normalization
|
||||
# import re
|
||||
|
||||
# # o1 ="/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/description_data_nor_morakab_verb.json"
|
||||
# # o2 ="/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/description_validdata1240000.json"
|
||||
# o3 ="/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/validation_description_data.json"
|
||||
# total_data = load_orjson(
|
||||
# "/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/description_merge.json"
|
||||
# )
|
||||
|
||||
# f_char = set()
|
||||
# for i in total_data:
|
||||
# f_char.add(i["word"])
|
||||
# f_char.add(i["description"])
|
||||
|
||||
|
||||
# def sort_data3(item):
|
||||
# return int(len(item["description"]))
|
||||
|
||||
# def remove_bracketed(text: str) -> str:
|
||||
# pattern = r"""
|
||||
# \([^)]*\) | \{[^}]*\} | \[[^\]]*\] |
|
||||
# «[^»]*» | “[^”]*” | ‘[^’]*’ |
|
||||
# `[^`]*` | <[^>]*> | '[^']*' | "[^"]*"
|
||||
# """
|
||||
# return re.sub(pattern, '', text, flags=re.VERBOSE | re.UNICODE)
|
||||
|
||||
# chars = load_orjson('/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/all_char.json')
|
||||
# # pattern = r"[`\"'{}[\]؟:!0123456789.،\(\)؛,]+"
|
||||
# pattern = f"[{''.join(map(re.escape, chars))}]"
|
||||
|
||||
# def keep_bracket_content(text: str) -> str:
|
||||
# """
|
||||
# متن داخل پرانتزها (یا هر نوع پرانتز دیگری) را نگه میدارد و پرانتزها را حذف میکند.
|
||||
|
||||
# پارامتر:
|
||||
# text (str): متن ورودی
|
||||
|
||||
# خروجی:
|
||||
# str: متن با حذف پرانتزها و نگه داشتن محتوا
|
||||
# """
|
||||
# pattern = r"""
|
||||
# \(([^)]*)\) | # ( ... )
|
||||
# \{([^}]*)\} | # { ... }
|
||||
# \[([^\]]*)\] | # [ ... ]
|
||||
# «([^»]*)» | # « ... »
|
||||
# “([^”]*)” | # “ ... ”
|
||||
# ‘([^’]*)’ | # ‘ ... ’
|
||||
# `([^`]*)` | # ` ... `
|
||||
# <([^>]*)> | # < ... >
|
||||
# '([^']*)' | # ' ... '
|
||||
# "([^"]*)" # " ... "
|
||||
# """
|
||||
# # تابعی که اولین گروه غیر None را برمیگرداند
|
||||
# def repl(match: re.Match) -> str:
|
||||
# # گروههای متشکل از متن داخل پرانتزها
|
||||
# groups = match.groups()
|
||||
# # اولین گروهی که مقدار دارد را برمیگردانیم
|
||||
# return next(g for g in groups if g is not None)
|
||||
|
||||
# return re.sub(pattern, repl, text, flags=re.VERBOSE | re.UNICODE)
|
||||
|
||||
# from typing import List
|
||||
|
||||
# def extract_bracket_content(text: str) -> List[str]:
|
||||
# """
|
||||
# تمام متنهای داخل پرانتزها (یا هر نوع پرانتزی که در الگوی زیر تعریف شده است)
|
||||
# را استخراج میکند و به صورت لیست برمیگرداند.
|
||||
|
||||
# پارامتر:
|
||||
# text (str): متن ورودی
|
||||
|
||||
# خروجی:
|
||||
# List[str]: لیستی از متنهای داخل پرانتزها
|
||||
# """
|
||||
# pattern = r"""
|
||||
# \(([^)]*)\) | # ( ... )
|
||||
# \{([^}]*)\} | # { ... }
|
||||
# \[([^\]]*)\] | # [ ... ]
|
||||
# «([^»]*)» | # « ... »
|
||||
# “([^”]*)” | # “ ... ”
|
||||
# ‘([^’]*)’ | # ‘ ... ’
|
||||
# `([^`]*)` | # ` ... `
|
||||
# <([^>]*)> | # < ... >
|
||||
# '([^']*)' | # ' ... '
|
||||
# "([^"]*)" # " ... "
|
||||
# """
|
||||
# # findall بر تمام گروههای مطابقتی که در pattern تعریف شدهاند، برمیگرداند
|
||||
# # هر گروه یک tuple است؛ ما فقط اولین عضو غیر None را میگیریم
|
||||
# matches = re.findall(pattern, text, flags=re.VERBOSE | re.UNICODE)
|
||||
|
||||
# # تبدیل tuple به لیست متنهای واقعی
|
||||
# contents = []
|
||||
# for match in matches:
|
||||
# # match یک tuple از طول 10 است؛ فقط اولین عضو غیر None را میگیریم
|
||||
# for group in match:
|
||||
# if group: # اگر گروه خالی نباشد
|
||||
# contents.append(group)
|
||||
# break # فقط اولین گروه را میگیریم
|
||||
|
||||
# return contents
|
||||
|
||||
# bracket_texts = set()
|
||||
# for item in total_data:
|
||||
# bracket_texts.update(extract_bracket_content(item.get('description', '')) )
|
||||
|
||||
# f_list = set()
|
||||
# valid_char = load_orjson("/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/f_total_char.json")
|
||||
# for item in bracket_texts:
|
||||
# if len(item) > 2:
|
||||
# if all(i in valid_char for i in item ):
|
||||
# valid_word = item.split(" ")
|
||||
# for j in valid_word:
|
||||
# f_list.add(j)
|
||||
|
||||
|
||||
# f_list = list(f_list)
|
||||
# f_list = sorted(f_list, key=len)
|
||||
# print(f'f_list {len(f_list)}')
|
||||
# save_orjson(
|
||||
# data=f_list,
|
||||
# path="/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/bracket_texts.json",
|
||||
# )
|
||||
# اگر میخواهید فقط اولین متن داخل پرانتز را نگه دارید:
|
||||
# item['description'] = bracket_texts[0] if bracket_texts else ''
|
||||
# # 3. تقسیم متن به کلمات و جمعآوری یکتا
|
||||
# f_list = set()
|
||||
# unvalid = 0
|
||||
# for text in f_char:
|
||||
# cleaned = remove_bracketed(text)
|
||||
# if cleaned != text: unvalid +=1
|
||||
|
||||
# for w in re.split(pattern, cleaned):
|
||||
# w = w.strip()
|
||||
# if len(w) > 1 :
|
||||
# f_list.add(w)
|
||||
|
||||
# f_list = list(f_list)
|
||||
# print(f"f_list {len(f_list)} unvalid{unvalid}")
|
||||
# f_list = sorted(f_list, key=len)
|
||||
|
||||
# save_orjson(
|
||||
# data=f_list,
|
||||
# path="/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/all_word.json",
|
||||
# )
|
||||
# رشتهها
|
||||
################################### gemma stuff
|
||||
|
||||
|
||||
# class Description(BaseModel):
|
||||
# name: str
|
||||
# description: str
|
||||
|
||||
|
||||
# class Data(BaseModel):
|
||||
# input_description: str
|
||||
# correct_spell: str = "" ######################### only in nor verb
|
||||
# close_description: List[Description]
|
||||
|
||||
|
||||
# class Result(BaseModel):
|
||||
# result: Data
|
||||
|
||||
|
||||
# data1 = "/home1/ava3/init_mahdi/project/aiDataParser/task/q_a_simple/output_phase2/final_data_make-more-knowldege-phase2-gemma-3-27b.json"
|
||||
# 117768 [new_descriptions, validation, word, user_prompt}]
|
||||
# باید از user_prompt داده ورودی داخل `` را بیرون بکشیم
|
||||
# در مورد `{word}` این اطلاعات را دارم `{description}`
|
||||
# new_descriptions
|
||||
# validation
|
||||
# # word
|
||||
# data2 = "/home1/ava3/init_mahdi/project/aiDataParser/task/q_a_simple/output_nor_verb1/final_data_knowldege-gemma-3-27b.json"
|
||||
# # 4167 [Result]
|
||||
# data3 = "/home1/ava3/init_mahdi/project/aiDataParser/task/q_a_simple/output/final_data_knowldege-gemma-3-27b.json"
|
||||
# # 1217790
|
||||
# data4 = "/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/all_persia_gemma327b/output/final_data_word-p2-gemma-3-27b.json"
|
||||
# # ()
|
||||
# data5 = "/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/gemma327b/output/final_data_word-p2-gemma-3-27b.json"
|
||||
# # ()
|
||||
# data6 = "/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/mistral-2509/final_data_word-p2-magistral-small-2509.json"
|
||||
# # ()
|
||||
# # data3 = ""
|
||||
# base_o_path = "/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data"
|
||||
# temp1 = []
|
||||
# _valid = 0
|
||||
# """
|
||||
# v1 :
|
||||
# - data3
|
||||
# - data2
|
||||
|
||||
|
||||
# """
|
||||
# data_list1 = load_orjson(data1)
|
||||
# print(f'data_list1 {type(data_list1)}')
|
||||
# print(f'data_list1 {len(data_list1)} {data_list1[0]}')
|
||||
|
||||
############# data4
|
||||
|
||||
############# data3
|
||||
# for item in data_list1:
|
||||
# word = item["word"]
|
||||
# result = item["result"]
|
||||
# if isinstance(result, str):
|
||||
# temp1.append({"description": "", "word": word})
|
||||
# # temp1.append(item)
|
||||
# elif isinstance(result, Dict):
|
||||
# temp1.append({"description": result["input_description"], "word": word})
|
||||
# for item in result["close_description"]:
|
||||
# temp1.append({"description": item["description"], "word": item["name"]})
|
||||
# _valid += 1
|
||||
# else:
|
||||
# print(f"Errpr {item}<<")
|
||||
|
||||
# def sort_data3(item):
|
||||
# return int(len(item['description']))
|
||||
|
||||
# temp1 = list(sorted(temp1, key=sort_data3))
|
||||
|
||||
############# data2
|
||||
# for item in data_list1:
|
||||
# word = item['word']
|
||||
# result = item['result']
|
||||
# if isinstance(result, str): pass
|
||||
# elif isinstance(result, Dict):
|
||||
# temp1.append(item)
|
||||
# result = Data.model_validate(result)
|
||||
# _valid += 1
|
||||
# else:
|
||||
# print(
|
||||
# f'Errpr {item}<<'
|
||||
# )
|
||||
import traceback
|
||||
|
||||
# data_list1 = data_list1[:400]
|
||||
############# data1
|
||||
# for item in data_list1:
|
||||
# try:
|
||||
# print(f'')
|
||||
# user_prompt = item['user_prompt'].strip('\n').split("`")[-2]
|
||||
# validation = item['validation']
|
||||
# new_descriptions = item['new_descriptions']
|
||||
# word = item["word"]
|
||||
# temp1.append(
|
||||
# {
|
||||
# "word":word,
|
||||
# "validation" : validation,
|
||||
# "description":user_prompt
|
||||
# }
|
||||
# )
|
||||
# for i in new_descriptions:
|
||||
# temp1.append(
|
||||
# {
|
||||
# "word":word,
|
||||
# "validation" : "unknown",
|
||||
# "description": i
|
||||
# }
|
||||
# )
|
||||
# except:
|
||||
# print(f'item {item}')
|
||||
# traceback.print_exc()
|
||||
|
||||
|
||||
############ save-test
|
||||
# print(f"temp1 {len(temp1)} _valid {_valid}")
|
||||
# save_orjson(data=temp1, path=base_o_path + "/validation_description_data.json")
|
||||
|
||||
# python3 -m aiDataParser.task.word_bank_proccess.phase2_word.finall_data.main
|
||||
################################### get togher
|
||||
# tag = 'gemma27b_p1'
|
||||
# o_path = '/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/word_bankv1.json'
|
||||
# input_data1 = load_orjson(
|
||||
# # '/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/word_correct2.json'
|
||||
# # "/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/make_data/all_correct_stem.json"
|
||||
# # "/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/make_data/important_data/verbs/finall_data_verbs.json"
|
||||
# "/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/all_word_gemma.json"
|
||||
# )
|
||||
# input_data = set()
|
||||
# for item in input_data1:
|
||||
# input_data.add(
|
||||
# item
|
||||
# )
|
||||
# # verb = dict_item['verb']
|
||||
# # pass_vands = dict_item['pass_vands']
|
||||
# # pish_vands = dict_item['pish_vands']
|
||||
|
||||
# # for item in pish_vands:
|
||||
# # i = item+verb
|
||||
# # input_data.add(i)
|
||||
|
||||
# # for item in pass_vands:
|
||||
# # i = verb+item
|
||||
# # input_data.add(i)
|
||||
|
||||
# input_data = list(input_data)
|
||||
# f_list = load_orjson(
|
||||
# o_path
|
||||
# )
|
||||
# print(f'input_data {len(input_data)}')
|
||||
# print(f'f_list {len(f_list)}')
|
||||
# ff_list = defaultdict(set)
|
||||
# for k, v in f_list.items():
|
||||
# ff_list[k].update(
|
||||
# v['tags']
|
||||
# )
|
||||
|
||||
# for word in input_data:
|
||||
# # if _word :
|
||||
# ff_list[word].add(
|
||||
# tag
|
||||
# )
|
||||
# # else:
|
||||
# # f_list[word] = {
|
||||
# # tag
|
||||
# # }
|
||||
|
||||
# fff_list = {}
|
||||
# for k, v in ff_list.items():
|
||||
# fff_list[k] = list(v)
|
||||
|
||||
|
||||
|
||||
# fff_list = dict(sorted(fff_list.items(),
|
||||
# key=lambda kv: len(kv[1]), reverse=True))
|
||||
|
||||
# save_orjson(
|
||||
# data=fff_list,
|
||||
# # path=o_path,
|
||||
# path="/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/finall_data/word_bankv_11.json"
|
||||
# )
|
||||
# print(
|
||||
# f'f_list {len(f_list)}',
|
||||
# ":D", sep='\n'
|
||||
# )
|
||||
|
||||
# python3 -m aiDataParser.task.word_bank_proccess.phase2_word.finall_data.main
|
||||
Reference in New Issue
Block a user