- 获取Wikidata的所有properties:
在https://query.wikidata.org/直接输入一下代码,可以获取wikidata知识图谱的所有properties,根据下面SPARQL语言可以获得properties的链接URL、ID、name、 description、label等,
目前property的数量是10115个。
SELECT ?property ?propertyLabel ?propertyDescription (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE { ?property a wikibase:Property . OPTIONAL { ?property skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "en") } SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .} } GROUP BY ?property ?propertyLabel ?propertyDescription LIMIT 11000
Wikipedia的SPARQL查询界面:
在界面可以选择保存的数据格式,我这里保存为csv文件:
wikidata_all_properties_20220706.csv
property,propertyLabel,propertyDescription,altLabel_list http://www.wikidata.org/entity/P6,head of government,"head of the executive power of this town, city, municipality, state, country, or other governmental body","governor, prime minister, mayor, chancellor, president, first minister, premier, head of national government, government headed by, executive power headed by" http://www.wikidata.org/entity/P10,video,"relevant video. For images, use the property P18. For film trailers, qualify with ""object has role"" (P3831)=""trailer"" (Q622550)","trailer (Commons), gif, media, animation" http://www.wikidata.org/entity/P15,route map,image of route map at Wikimedia Commons,"watercourse map, underground map, transit map, subway map, street map, road map, road atlas, metro map, map of route, highway map, railway map, railroad map, schema" http://www.wikidata.org/entity/P14,traffic sign,"graphic symbol describing the item, used at the side of or above roads to give instructions or provide information to road users","highway shield, road sign, route shield, trail blazer, route marker, road marker, motorway sign, highway marker, shield" ...
- 其他:获取properties的embeddings
根据properties的相关信息,得到每个property的embedding,推荐采用MPNet模型,其专门用于获取sentence embedding的:
https://huggingface.co/sentence-transformers/all-mpnet-base-v1
具体转化代码如:
from sentence_transformers import SentenceTransformer import pandas as pd import numpy as np data = pd.read_csv('data/wikidata_all_properties_20220706.csv') sentences = ['{} {} {}'.format(n,d,l) for n,d,l in zip(data['propertyLabel'],data['propertyDescription'],data['altLabel_list'])] IDs = [id.rsplit('/',1)[-1] for id in data['property']] batch_size = 256 allembeddings = [] model = SentenceTransformer('sentence-transformers/all-mpnet-base-v1').to('cuda:1') for i in range(len(sentences)//batch_size+1): embeddings = model.encode(sentences[i*batch_size:(i+1)*batch_size]) allembeddings.append(embeddings) allembeddings = np.concatenate(allembeddings,axis=0) allembeddings = [list(emb) for emb in list(allembeddings)] data['embedding'] = allembeddings data['sentence'] = sentences data['ID'] = IDs D = data[['ID','propertyLabel','sentence','embedding']] D.to_csv('wikidata_all_properties_20220706+embeddings.csv', index=False)
这里转化embedding采用的是propertyLabel, propertyDescription, altLabel_list三部分文本信息转化的,可以根据需要进行调整