简体   繁体   English

按含义比较两个字符串

[英]Compare two strings by meaning

Are there any solutions how to compare short strings not by characters, but by meaning ?是否有任何解决方案如何不按字符而是按含义比较短字符串 I've tried to google it, but all search results are about comparing characters, length and so on.我试过用谷歌搜索,但所有搜索结果都是关于比较字符、长度等。 I'm not asking you about ready-to-use solutions, just show me the way, where I need "to dig".我不是在问你现成的解决方案,只是告诉我方法,我需要“挖掘”的地方。

Thank you in advance.先感谢您。

Your topic is not clear enough. 您的主题不够清楚。 When you compare string by meaning, you need to define the level of equal. 当按含义比较字符串时,需要定义相等级别。 for example "I have 10 dollars" and "there are 10 dollars in my pocket. Are they equal in your definition? sometimes there is implied meaning in the string. 例如“我有10美元”和“口袋里有10美元。它们在您的定义中是否相等?有时在字符串中有隐含的含义。

Answer to a very similar closed question , that wants to compare the context between two lists ['apple', 'spinach', 'clove'] and ['fruit', 'vegetable', 'spice'] , that uses the Google Knowledge Graph Search API :回答一个非常相似的封闭问题,该问题想要比较两个列表['apple', 'spinach', 'clove']['fruit', 'vegetable', 'spice']之间的上下文,使用Google Knowledge图搜索API

import json
from urllib.parse import urlencode
from urllib.request import urlopen

def get_descriptions_set(query: str) -> set[str]:
    descriptions = set()
    kg_response = get_kg_response(query)
    for element in kg_response['itemListElement']:
        if 'description' in element['result']:
            descriptions.add(element['result']['description'].lower())
    return descriptions

def get_kg_response(query: str) -> str:
    api_key = open('.api_key').read()
    service_url = 'https://kgsearch.googleapis.com/v1/entities:search'
    params = {
        'query': query,
        'limit': 10,
        'indent': True,
        'key': api_key,
    }
    url = f'{service_url}?{urlencode(params)}'
    response = json.loads(urlopen(url).read())
    return response

def main() -> None:
    list_1 = ['apple', 'spinach', 'clove']
    list_2 = ['fruit', 'vegetable', 'spice']
    list_1_kg_descrpitons = [get_descriptions_set(q) for q in list_1]
    print('\n'.join(f'{q} {descriptions}'
                    for q, descriptions in zip(list_1, list_1_kg_descrpitons)))
    list_2_matches_context = [
        d in descriptions
        for d, descriptions in zip(list_2, list_1_kg_descrpitons)
    ]
    print(list_2_matches_context)

if __name__ == '__main__':
    main()

Output:输出:

apple {'watch', 'technology company', 'fruit', 'american singer-songwriter', 'digital media player', 'mobile phone', 'tablet computer', 'restaurant company', 'plant'}
spinach {'video game', 'plant', 'vegetable', 'dish'}
clove {'village in england', 'spice', 'manga series', 'production company', '2018 film', 'american singer-songwriter', '2008 film', 'plant'}
[True, True, True]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM