我正在尝试按照下面提到的两个 DBpedia 属性来构建主题层次结构。
- skos:更广泛的财产
- dcterms:主题属性
我的意图是给这个词确定它的主题。例如,给定单词;'支持向量机',我想从中识别主题,例如分类算法、机器学习等。
但是,有时我对如何构建主题层次结构感到有些困惑,因为我获得了超过 5 个主题 URI 和许多更广泛属性的 URI。有没有办法测量强度或其他东西并减少我从 DBpedia 获得的额外 URI 并仅分配最高可能的 URI?
那里似乎有两个问题。
- 如何限制 DBpedia Spotlight 结果的数量。
- 如何限制特定结果的主题和类别数量。
我当前的代码如下。
from SPARQLWrapper import SPARQLWrapper, JSON
import requests
import urllib.parse
## initial consts
BASE_URL = 'http://api.dbpedia-spotlight.org/en/annotate?text={text}&confidence={confidence}&support={support}'
TEXT = 'First documented in the 13th century, Berlin was the capital of the Kingdom of Prussia (1701–1918), the German Empire (1871–1918), the Weimar Republic (1919–33) and the Third Reich (1933–45). Berlin in the 1920s was the third largest municipality in the world. After World War II, the city became divided into East Berlin -- the capital of East Germany -- and West Berlin, a West German exclave surrounded by the Berlin Wall from 1961–89. Following German reunification in 1990, the city regained its status as the capital of Germany, hosting 147 foreign embassies.'
CONFIDENCE = '0.5'
SUPPORT = '120'
REQUEST = BASE_URL.format(
text=urllib.parse.quote_plus(TEXT),
confidence=CONFIDENCE,
support=SUPPORT
)
HEADERS = {'Accept': 'application/json'}
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
all_urls = []
r = requests.get(url=REQUEST, headers=HEADERS)
response = r.json()
resources = response['Resources']
for res in resources:
all_urls.append(res['@URI'])
for url in all_urls:
sparql.setQuery("""
SELECT * WHERE {<"""
+url+
""">skos:broader|dct:subject ?resource
}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print('resource ---- ', result['resource']['value'])
如果需要,我很乐意提供更多示例。