标签:map Wikidata embedding altLabel wikidata property data properties
- 获取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.csvproperty,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三部分文本信息转化的,可以根据需要进行调整
标签:map,Wikidata,embedding,altLabel,wikidata,property,data,properties 来源: https://www.cnblogs.com/huadongw/p/16456166.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。