sorted similarity of item link

This commit is contained in:
mxr612 2024-05-14 04:50:01 +08:00
parent a150d02d7a
commit 727d4e57a8
4 changed files with 96 additions and 25 deletions

50
ItemRelate.py Normal file
View File

@ -0,0 +1,50 @@
import os
os.environ["OPENAI_API_KEY"]= "sk-PRJ811XeKzEy20Ug3dA98a34Af8b40B5816dE15503D33599"
os.environ["OPENAI_BASE_URL"]= "http://154.9.28.247:3000/v1/"
from openai import OpenAI
client = OpenAI()
from sklearn.metrics.pairwise import cosine_similarity
import json
def embedding(s:str):
if len(s)==0:
return
else:
return client.embeddings.create(
input=s, model="text-embedding-3-small" # 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=[]
vec=[]
for i in scale["item"]:
item.append(i)
vec.append(embedding(scale["item"][i]))
simi=cosine_similarity(vec)
que=[]
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]))
# print("\n")
sorted(que,key= lambda t : t.sim)
return que

36
TestFile/adhd.json Normal file
View File

@ -0,0 +1,36 @@
{
"name":"成人多动症自筛表 (6 itmes)",
"introduction":1,
"item":{
"asrs1": "当一项工作最有挑战性的部分完成后,你经常难以做到对细节部分更精益求精吗?",
"asrs2": "你经常很难把有组织性的任务、事情布置得井井有条吗? ",
"asrs3": "当你从事一项需要脑力的工作的时候,你经常回避或者延长开始的时间吗?",
"asrs4": "你经常很难记住约会或者一些必须完成的事情吗?",
"asrs5": "当您必须安静地坐很长时间时,是否经常表现得坐立不安,手脚动作多?",
"asrs6": "你是否在自觉做事情时表现得极度活跃,非完成某项事情不可,好像在被发动机所驱使一样?"
},
"value": {
"从不":0,
"很少":1,
"有时":2,
"经常":3,
"总是":4
},
"result":[
{
"name": "ADHD",
"sum": [
"asrs1",
"asrs2",
"asrs3",
"asrs4",
"asrs5",
"asrs6"
],
"re_sum": [],
"result": {
"0": 2
}
}
]
}

View File

@ -1,25 +0,0 @@
import os
os.environ["OPENAI_API_KEY"]= "sk-PRJ811XeKzEy20Ug3dA98a34Af8b40B5816dE15503D33599"
os.environ["OPENAI_BASE_URL"]= "http://154.9.28.247:3000/v1/"
from openai import OpenAI
client = OpenAI()
from sklearn.metrics.pairwise import cosine_similarity
def embedding(s:str):
if len(s)==0:
return
else:
return client.embeddings.create(
input=s, model="text-embedding-3-large" # nomic-embed-text text-embedding-3-small
).data[0].embedding
a=embedding("I tend to draw fine distinctions between similar feelings (e.g., depressed and blue; annoyed and irritated).")
# b=embedding("我喜欢界定两种相似的情绪(如沮丧和忧伤,烦恼和被激怒)。")
# c=embedding("I like to define two similar emotions (e.g., frustration and sadness, annoyance and irritation).")
print(a,"\n",len(a))
# s = cosine_similarity([a, b, c])
# print(s)

10
main.py Normal file
View File

@ -0,0 +1,10 @@
from ItemRelate import main as relate
import json
with open("TestFile/adhd.json") as json_file:
scale = json.load(json_file)
que=relate(scale)
for i in que:
print(i.a, i.b, i.sim)