From 8717b81bbccfbc393261d9cdd821e3d850de24fb Mon Sep 17 00:00:00 2001 From: mxr612 Date: Tue, 14 May 2024 12:08:37 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E8=BD=AC=E7=94=A8json=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E5=AD=98=E5=82=A8=20-=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=AD=98=E5=82=A8=E8=AE=A1=E7=AE=97=E7=BB=93?= =?UTF-8?q?=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ItemRelate.py | 15 +++------------ main.py | 17 +++++++++++------ 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/ItemRelate.py b/ItemRelate.py index e44b7c0..24b733c 100644 --- a/ItemRelate.py +++ b/ItemRelate.py @@ -14,18 +14,9 @@ def embedding(s:str): return else: return client.embeddings.create( - input=s, model="text-embedding-3-small" # nomic-embed-text text-embedding-3-small + input=s, model="nomic-embed-text" # nomic-embed-text text-embedding-3-small ).data[0].embedding -class edge: - a:str - b:str - sim:float - def __init__(self, a:str, b:str, sim:float): - self.a=a - self.b=b - self.sim=sim - def main(scale:json): item=[] @@ -42,9 +33,9 @@ def main(scale:json): for i,v in enumerate(simi): for j in range(0,i): # print(simi[i][j],',',item[i],',',item[j]) - que.append(edge(item[i], item[j], simi[i][j])) + que.append({"from":item[j], "i":item[i], "similarity":simi[i][j]}) # print("\n") - sorted(que,key= lambda t : t.sim) + sorted(que,key= lambda t : t["similarity"]) return que \ No newline at end of file diff --git a/main.py b/main.py index 20b89bd..9c92272 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,15 @@ from ItemRelate import main as relate import json +import os -with open("TestFile/adhd.json") as json_file: - scale = json.load(json_file) +if os.path.getsize("TestFile/items.json") == 0: + with open("TestFile/adhd.json","r") as json_file: + scale = json.load(json_file) + que=relate(scale) + with open("TestFile/items.json","w") as items: + items.write(json.dumps(que)) +else: + with open("TestFile/items.json","r") as items: + que = json.load(items) -que=relate(scale) - -for i in que: - print(i.a, i.b, i.sim) +print(que)