[英]Iterate through nested dictionaries from a JSON file
I have the following output from a json file我有来自 json 文件的以下 output
{
"Threshold": 0.6,
"Services": [
{
"Name": "Service1",
"Query": [
"query1",
"query2",
"query3"
],
"Products": [
{
"Name": "product1",
"Query": [
"query4",
"query5"
],
"Threshold": 0.75
},
{
"Name": "product2",
"Query": [
"query6",
"query7",
"query8"
],
"Threshold": 0.75
},
{
"Name": "product3",
"Query": [
"query9",
"query10"
],
"Threshold": 0.75
},
{
"Name": "product4",
"Query": [
"query11",
"query12"
],
"Threshold": 0.75
},
{
"Name": "product5",
"Query": [
"query13",
"query14"
],
"Threshold": 0.75
}
]
},
{
"Name": "Service2",
"Query": [
"query1",
"query2",
"query3"
],
"Products": [
{
"Name": "product1",
"Query": [
"query4",
"query5"
],
"Threshold": 0.75
},
{
"Name": "product2",
"Query": [
"query6",
"query7",
"query8"
],
"Threshold": 0.75
},
{
"Name": "product3",
"Query": [
"query9",
"query10"
],
"Threshold": 0.75
},
{
"Name": "product4",
"Query": [
"query11",
"query12"
],
"Threshold": 0.75
},
{
"Name": "product5",
"Query": [
"query13",
"query14"
],
"Threshold": 0.75
}
]
}
]
}
The structure of the file is as follows: There are two services and each of this service has five similar products.该文件的结构如下: 有两个服务,每个服务有五个相似的产品。 Each service has a list of queries that contain some keywords that describe the service.每个服务都有一个查询列表,其中包含一些描述该服务的关键字。
The same applies for the products.这同样适用于产品。
Each product as a list of query that contains keywords that describe the individual product.每个产品作为一个查询列表,其中包含描述单个产品的关键字。
I want to loop into the services and select the service1.我想循环进入服务和 select service1。 I then want to run the queries in an algorithm against a text to find whether one or all the queries are present in the text.然后,我想针对文本运行算法中的查询,以查找文本中是否存在一个或所有查询。 If the query or queries are present, I want to go into the products and start iterating through product1 to product5.如果存在查询或查询,我想将 go 放入产品并开始迭代 product1 到 product5。 If not, it should skip and go to the service2如果没有,它应该跳过和 go 到 service2
I want to do the same for the service2 and the respective products.我想对 service2 和相应的产品做同样的事情。
I can only run it when I run the following code:我只能在运行以下代码时运行它:
for service in configfile["Services"]:
if service["Name"] == "Service1":
The code must run without hardcoding the "Service1 or "Service2" name.代码必须在没有硬编码“Service1 或“Service2”名称的情况下运行。
In essence, I want to take the service1 and service2 and access the queries.本质上,我想获取 service1 和 service2 并访问查询。 I will run the code from Sentence transformers .我将从Sentence transformers运行代码。
# Query sentences:
queries = ['query1', 'query2', 'query3']
top_k = min(5, len(corpus))
for query in queries:
query_embedding = embedder.encode(query, convert_to_tensor=True)
# We use cosine-similarity and torch.topk to find the highest 5 scores
cos_scores = util.pytorch_cos_sim(query_embedding, corpus_embeddings)[0]
top_results = torch.topk(cos_scores, k=top_k)
print("\n\n======================\n\n")
print("Query:", query)
print("\nTop 5 most similar sentences in corpus:")
for score, idx in zip(top_results[0], top_results[1]):
print(corpus[idx], "(Score: {:.4f})".format(score))
I get some scores from this algorithm for each query.对于每个查询,我都会从该算法中获得一些分数。 I then sort the queries and I take the query with the largest value.然后我对查询进行排序,并获取具有最大值的查询。 If this value is above a threshold, I want to continue with the same algorithm in the queries of product1 to product5 that are in the services1.如果此值高于阈值,我想在 service1 中的 product1 到 product5 的查询中继续使用相同的算法。 Take the largest value and if the value is above a threshold, I will add it to a dictionary as "positive".取最大值,如果该值高于阈值,我会将其作为“正数”添加到字典中。 if not, I will add it as "negative".如果没有,我会将其添加为“负面”。
If the value is below the threshold, I want to skip the iteration to the products and go to Service2 and run the algorithm again for the queries and repeat the process.如果该值低于阈值,我想跳过迭代到产品和 go 到 Service2 并再次运行算法以进行查询并重复该过程。
It's not clear what you want to do with your products but since Services
and Products
are lists, you can simply loop through them:目前尚不清楚您想对您的产品做什么,但由于Services
和Products
是列表,您可以简单地遍历它们:
for serv in json_data.get("Services", []):
if serv.get("Query", None):
for prod in serv.get("Products", []):
# do your stuff with prod
print(serv.get("Name", None), prod.get("Name", None))
Output: Output:
Service1 product1
Service1 product2
Service1 product3
Service1 product4
Service1 product5
Service2 product1
Service2 product2
Service2 product3
Service2 product4
Service2 product5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.