#!/usr/bin/env python # coding: utf-8 # # Resolving # # Resolvers are helpers to find commonly used resources that one may want to link resources to. Resolvers are configured in the `Resolvers` section of the configuration file. # In[2]: from kgforge.core import KnowledgeGraphForge # In[3]: forge = KnowledgeGraphForge("../../configurations/demo-forge.yml") # # Imports # In[4]: from kgforge.core.commons.strategies import ResolvingStrategy # ## Terms # ### scope # Resolve a resource for `female` in the terms. # In[5]: female = forge.resolve("female", scope="terms") # In[6]: type(female) # In[7]: print(female) # ### target # In[8]: print(forge.resolve("female", scope="terms", target="sex")) # ### type # In[9]: print(forge.resolve("female", scope="terms", type="Class")) # ## Entity # ### scope # Resolving a resource for `EPFL` in the entities. # In[10]: epfl = forge.resolve("EPFL", scope="entities") # In[11]: type(epfl) # In[12]: print(epfl) # ### target # In[13]: print(forge.resolve("EPFL", scope="entities", target="agents")) # ### type # In[14]: print(forge.resolve("EPFL", scope="entities", type="Organization")) # ## Strategies # Different strategies can be used to resolve terms. # # In the following example, the missing 'e' at the end is intended for the demonstration. # In[15]: text = "mal" # ### best match # The default applied strategy is `strategy=ResolvingStrategy.BEST_MATCH`. # In[16]: print(forge.resolve(text, scope="terms")) # ### exact match # In[17]: print(forge.resolve(text, scope="terms", strategy=ResolvingStrategy.EXACT_MATCH)) # ### fuzzy match (all matches) # The result list is ordered by matching relevance. # In[18]: results = forge.resolve(text, scope="terms", strategy=ResolvingStrategy.ALL_MATCHES) # In[19]: type(results) # In[20]: len(results) # In[21]: type(results[0]) # In[22]: print(*results, sep="\n") # In[ ]: