Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

python - Compare dictionaries from different files

The problem I have is a bit far-fetched, but I will try to express it as clearly as I can.

I have two python files that contain different dictionaries.

In the file science.py the result of the dictionary is as follows:

    dict1 = { "microbiome": {
                  "lang": "en",
                  "lemmatizer": "microbiome",
                  "Wikidata": "http://www.wikidata.org/entity/Q1330402"
                  "DBpedia": "http://dbpedia.org/resource/Microbiota"
          }, "teaching innovation": {
                 "lang": "en",
                 "lemmatizer": "innovación docente",
                 "Wikidata": null,
                 "DBpedia": null
              },
               ...... 
          }

The content of the structure of this dictionary is built in a function that I have called process (word).

The output of the compact.py file is as follows:

    dict2 = { http://dbpedia.org/resource/Microbiota :[
                 {'keyword': 'Microbiota', 'language': 'ca'}, 
                 {'keyword': 'Microbiota', 'language': 'en'}, 
                 {'keyword': 'Microbioma', 'language': 'es'}],
             'teaching innovation': [{'keyword': 'Semantics', 
                                     'language': 'en'},
                        {'keyword': 'Semàntica', 'language': 'ca'},
                        {'keyword': 'Semántica lingüística', 
                        'language': 'es'}],
            .... }

The content of the structure of this dictionary is built by means of a function called query_kw (uri) where a query is made to the DBpedia page to get the translations of the word from the uri. This returns a list with the different translations of the word in the form of dictionaries.

As seen in dict2, it contains translations of a word along with the language they are in. What I am trying to do is check if the word from dict1 is found in dict2. In the small example that I have put above you can see. If we look at dict1 a dictionary key is" microbiome "then I have to check if it is in the dictionary list of dict2. If it is not found as is the case then add it to the dict2 list in the same way as the others ({'keyword': microbiome, 'language': 'en'}).

Then the result would have to be the following:

           dict2 = { http://dbpedia.org/resource/Microbiota :[
                        {'keyword': 'Microbiota', 'language': 'ca'}, 
                        {'keyword': 'Microbiota', 'language': 'en'}, 
                        {'keyword': 'Microbioma', 'language': 'es'},
                        {'keyword': 'microbiome', 'language': 'en'}],
                    'teaching innovation': [{'keyword': 'Semantics', 
                                          'language': 'en'},
                        {'keyword': 'Semàntica', 'language': 'ca'},
                        {'keyword': 'Semántica lingüística', 
                                     'language': 'es'}],
                        {'keyword': 'teaching innovation', 
                                     'language': 'en'}],
                 .... }

What I have tried has been the following (I know it is not optimal but it was to have an idea of ??how I can do it):

wrapper = query_kw(uri)
res_keys = process(palabra)
for k in rest_keys.keys():
    wrap = {'keyword': rest_keys[k], 'language': rest_keys[k]['language']
    for ind in wrapper:
        for key, value in wrapper[ind]:
            for ky, v in wrap:  
                if key is not ky and value is not v:
                    wrapper.append(wrap)

I already said, it is not beautiful to see nor is it optimal and above all it does not work correctly, but I can not think of how to do it. I hope I have explained myself well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, I've put up this short script, you can run the full code to verify it applied to the sample in your question.

Basically I use a dict comprehension, and use the DBpedia as the key that correlates both dictionaries.

Let me know if you have questions (and if this is what you intended).

def merge_dicts(dict1, dict2):
    # Assuming every item on dict1 has a `DBpedia` key and a `lang` key
    new_items = {items['DBpedia']: [{'keyword': key, 'language': items['lang']}] for key, items in dict1.items()}
    for url, new_item_list in new_items.items():
        # Assuming every item on dict2 has the `DBpedia` url as keys
        for item in dict2[url]:
            found = False
            for new_item in new_item_list:
                if item['keyword'].strip().lower() == new_item['keyword'].strip().lower():
                    found = True
                    break
            if found:
                break
        if not found:
            dict2[url].append(new_item)

    return dict2

dict1 = { "microbiome": {
          "lang": "en",
          "lemmatizer": "microbiome",
          "Wikidata": "http://www.wikidata.org/entity/Q1330402",
          "DBpedia": "http://dbpedia.org/resource/Microbiota"
    }
}
        
dict2 = { 'http://dbpedia.org/resource/Microbiota' :[
     {'keyword': 'Microbiota', 'language': 'ca'}, 
     {'keyword': 'Microbiota', 'language': 'en'}, 
     {'keyword': 'Microbioma', 'language': 'es'}
     ]
     }

print(merge_dicts(dict1, dict2))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...