Wikidata:봇 만들기

From Wikidata
Jump to navigation Jump to search
This page is a translated version of the page Wikidata:Creating a bot and the translation is 18% complete.
Outdated translations are marked like this.

이 페이지는 위키데이터에서 사용할 을 어떻게 만드는지에 대하여 다룹니다. 당신의 코드를 공유하여, 새로운 기능의 추가·개선을 부탁드립니다.

요구 사양

봇을 만들기 위해 당신에게 필요한 것:

  • 약간의 코딩 기술(Python, Perl, PHP...)
  • 프레임워크(다음의 것들 중 하나) 그리고, 작동을 위한 코드
  • 봇 계정(과 봇 권한 승인)
  • 소스 코드 편집기(Notepad++, Geany, vi, emacs)

Recommendation

Pywikibot

다음 항목에서는 어떻게 설치하는지에 대해서와 pywikibot의 구성에 대해서 다룹니다. 당신은 한 번만 이 과정을 거치면 됩니다. 또한 '기본 봇 프로그래밍'에 대한 기본적인 몇 가지 예제가 있습니다.

설치

pywikibot 설치에 대한 더 자세한 사항에 대해서는 mw:Manual:Pywikibot/Installation을 참고하세요.

pywikibot 설치하기:

환경 설정

pywikibot 환경 설정에 대한 더 자세한 사항에 대해서는 mw:Manual:Pywikibot/Installation을 참고하세요.

반드시user-config.py을 봇 사용자이름, 패밀리 프로젝트(family project)와 언어로 만들어야 합니다. 위키데이터에서는 페밀리 프로젝트와 언어가 같습니다, wikidata.

당신은 두 편집의 간격을 줄일 수 있습니다: put_throttle = 1 put_throttle = 1

로그인

나중에 당신은 user-config.py파일에서 다음과 같이 환경 설정을 할 수 있습니다. :

$ python login.py

봇 암호를 물어보면, 입력하고 엔터키를 누릅니다. 당신이 올바르게 진행했다면, 로그인이 가능합니다.

Example 1: Get data

This example gets data for the page refering to Douglas Adams. Save the following source code in a file and execute it with python example1.py

item.get()는 Wikidata에 연결하고 데이터를 가져옵니다. 출력은 다음과 같습니다 (명확성을 위해 다시 형식화 됨).

{
    'claims': {
        'P646': [<pywikibot.page.Claim instance at 0x7f1880188b48>],
        'P800': [<pywikibot.page.Claim instance at 0x7f1880188488>, <pywikibot.page.Claim instance at 0x7f1880188368>]
        ...
    }
    'labels': {
        'gu': '\u0aa1\u0a97\u0acd\u0ab2\u0abe\u0ab8 \u0a8f\u0aa1\u0aae\u0acd\u0ab8',
        'scn': 'Douglas Adams',
        ...
    }
    'sitelinks': {
        'fiwiki': 'Douglas Adams',
        'fawiki': '\u062f\u0627\u06af\u0644\u0627\u0633 \u0622\u062f\u0627\u0645\u0632',
        'elwikiquote': '\u039d\u03c4\u03ac\u03b3\u03ba\u03bb\u03b1\u03c2 \u0386\u03bd\u03c4\u03b1\u03bc\u03c2',
        ...
    }
    'descriptions': {
        'eo': 'angla a\u016dtoro de sciencfikcio-romanoj kaj humoristo',
        'en': 'English writer and humorist',
    },
    'aliases': {
        'ru': ['\u0410\u0434\u0430\u043c\u0441, \u0414\u0443\u0433\u043b\u0430\u0441'],
        'fr': ['Douglas Noel Adams', 'Douglas No\xebl Adams'],
        ...
    }
}
['claims', 'labels', 'sitelinks', 'descriptions', 'aliases']
[[wikidata:Q42]]

키와 함께 사전을 인쇄합니다.

  • 페이지의 클레임 집합 : Property:P646은 Freebase 식별자, Property:P800은 "주목할 작업"등입니다.
  • 여러 언어로 된 항목의 레이블
  • 항목에 대한 사이트 링크, 여러 언어로 된 Wikipedia뿐만 아니라 여러 언어로 된 Wikiquote
  • 여러 언어로 된 항목 설명
  • 여러 언어로 된 항목의 별칭

그런 다음 사전에있는 키-값 쌍에 대한 모든 키가있는 목록입니다. 마지막으로 Douglas Adams에 대한 Wikidata 항목이 Q42임을 알 수 있습니다.

대안

위의 예제는 en wikipedia 기사를 사용하여 ItemPage를 가져옵니다. 또는 ItemPage를 직접 가져올 수도 있습니다.

예 2: 위키 간 링크 가져 오기

예를 들어 item.get() 뒤에 사이트 링크에 액세스 할 수 있습니다. 기사가있는 모든 위키 백과에 대한 링크입니다.

출력은 다음과 같습니다.

{'fiwiki': 'Douglas Adams', 'eowiki': 'Douglas Adams', 'dewiki': 'Douglas Adams', ...}

With item.iterlinks(), an iterator over all these sitelinks is returned, where each article is given not as plain text as above but already as a Page object for further treatment (e.g., edit the text in the corresponding Wikipedia articles).

Example 4: Set a description

This example sets an English and a German description for the item about Douglas Adams.

Setting labels and aliases works accordingly.

Example 6: Set a sitelink

To set a sitelink, we can either create a corresponding dict corresponding to Example 4 or use Page objects:

Example 7: Set a statement

Statements are set using the Claim class. In the following, we set for Douglas Adams place of birth (P19): Cambridge (Q350).

For other datatypes, this works similar. In the following, we add claims with string (IMDb ID (P345)) and coordinate (coordinate location (P625)) datatypes (URL is the same as string):

Example 8: Add a qualifier

Qualifiers are also represented by the Claim class. In the following, we add the qualifier incertae sedis (P678): family (Q35409) to the Claim "claim". Make sure you add the item before adding the qualifier.

Example 9: Add a source

또한 소스는 Claim 클래스로 표시됩니다. 한정자와 달리 소스에는 둘 이상의 클레임이 포함될 수 있습니다. 다음에서 stated in (P248) : Integrated Taxonomic Information System (Q82575)retrieved (P813)에서 2014 년 3 월 20 일 클레임 "소유권 주장"의 출처로 추가합니다. 클레임은 Wikidata에서 검색하거나 미리 항목 페이지에 추가해야합니다.

Example 10: Page generators

TODO

Example 11: Get values of sub-properties

In the following, we get values of sub-properties from branch described by source (P1343) -> Great Soviet Encyclopedia (1969–1978) (Q17378135) -> properties reference URL (P854) and title (P1476).

More examples

Some users share their source codes. Learn more in the next links:

Wikidata Integrator

WikidataIntegrator is a library for reading and writing to Wikidata/Wikibase. We created it for populating Wikidata with content from authoritative resources on Genes, Proteins, Diseases, Drugs and others. Details on the different tasks can be found on the bot's Wikidata page.

Pywikibot is an existing framework for interacting with the MediaWiki API. The reason why we came up with our own solution is that we need a high integration with the Wikidata SPARQL endpoint in order to ensure data consistency (duplicate checks, consistency checks, correct item selection, etc.). Compared to Pywikibot, WikidataIntegrator currently is not a full Python wrapper for the MediaWiki API but is solely focused on providing an easy means to generate Python-based Wikidata bots.

For more information, documentation, download & installation instructions, see here: https://github.com/SuLab/WikidataIntegrator/

Example Notebook

An example notebook demonstrating an example bot to add therapeutic areas to drug items, including using fastrun mode, checking references, and removing old statements:

http://public-paws.wmcloud.org/46883698/example%20ema%20bot.ipynb

WikibaseIntegrator

Forked from Wikidata Integrator by User:Myst in 2020 and has seen several improvements to the API that makes it even easier to create bots using the library.

For more information, documentation, download & installation instructions, see here: https://github.com/LeMyst/WikibaseIntegrator

Example semi-automatic script

LexUse semi-automatic tool for finding and adding usage examples to lexemes. It's free software written using Python 3 in 2020 Wikidata:LexUse.

Wikibase.NET (Deprecated)

Wikibase.NET is the api that replaces the now deprecated DotNetDataBot. Api client for the MediaWiki extension Wikibase. They aren't compatible because Wikibase.NET does no longer need the DotNetWikiBot framework.

Download & Installation

You can download Wikibase.NET from GitHub. Just follow the instructions on that page.

Known issues

Examples

Coming not soon...

DotNetDataBot (Deprecated)

Installation

Configuration

After unpacking the package you can see a file called DotNetDataBot.dll and one called DotNetDataBot.xml. The xml document is only for documentation. To use it you have to create a new refer in your project. Then you can write using DotNetDataBot; to import the framework.

Login

To login you have to create a new Site object with the url of the wiki, your bot's username and its password.

Example 1: Get id using wiki page

You can access the id of an item by searching for using the site and the title of the connected page.

Example 2: Get interwiki links

You can get the interwiki links of an item by loading the content and accessing the links field of the object.

Example 3: Set a description

To set a description, you must call the setDescription function.

Example 4: Set a label

It works the same way for setting a label. Just call setLabel.

Example 5: Get interwiki links for 100 pages

This feature is not supported. Just iterate over the list.

Wikibase api for PHP

This is an api client for Wikibase written in PHP. It can be downloaded from here.

Example 1: Basic example

Take a look at the source comments to understand how it works.


Example 2: Creating claims

Take a look at the source comments to understand how it works.

VBot (no updates since 2017)

Framework for Wikidata and Wikipedia. Read and write on Wikidata and other Wikimedia project and have a useful list generator to generate list of Wikipedia page and Wikidata entity. Can read also JSON dump of Wikidata.

Overview

Bot to read and edit Wikidata and Wikipedia.

  • License: CC0 1.0
  • Language C#
  • Can read and write entities with all datatype on Wikidata
  • Can read and write pages on all Wiki project
  • Can read parameter from template on wiki pages
  • Can read JSON dump
  • Can create lists using:
  • Tested with Visual Studio Express 2013 for Windows Desktop.
    • Is necessary to have Newtonsoft.Json. You can install it with NuGet inside Visual Studio
    • Is necessary to add manually a reference to System.Web for "HttpUtility.UrlEncode"

Download

The framework can be downloaded from GitHub here.

Instruction

Example 1

Update en label for all items with instance of (P31): short film (Q24862) that have director (P57) and that have publication date (P577) in 1908. (Use of Wikidata query)

LexData (Python; for Lexicographical data)

LexData is an easy to use python libary to create and edit Lexemes, Senses and Forms.

Tips

The documentation of LexData is still a bit lacking so look at existing implementations in MachtSinn or Wikdata Lexeme Forms for ideas how to use it.

If you only want to add statements to Lexemes (not forms or senses) WikibaseIntegrator might be a better choice, as it is more versatile and support a lot of data types.

Installation

You can install LexData via pip:

$ pip install LexData

Login

For all operations you need a WikidataSession. You can create it with your credentials, a bot password or an Edit Token (for example to edit via OAUTH):


Retrieve a Lexeme

You can open existing Lexemes and read their content.

Searching and creating Lexemes

If you don't know the L-Id of a lexeme you can search for it. And if it doesn't exist you can create it.

Adding information

You can easily create forms or senses, with or without additional claims:

Using Wikidata's API directly

The other sections describe how to use bot frameworks to access and update Wikidata information. You can also directly interact with the Wikibase API that Wikidata provides. You need to do this if you're developing your own framework or if you need to do something that a framework doesn't support. The documentation for the Wikibase API can be found at mediawiki.org. You can also play around with it at Special:ApiSandbox, try action=wbgetentities.

Wikibase provides its API as a set of modules for MediaWiki's "action" API. You access this by making HTTP requests to /w/api.php. The default response format is JSON. So for your language of choice, you only need a library to perform HTTP requests and a JSON or XML library to parse the responses.

Example 1: Get Q number

This example gets the item Q number for the English Wikipedia article about Andromeda Galaxy. The Wikibase API's main "workhorse" module action=wbgetentities provides this information. The HTTP request (using jsonfm format for human-readable JSON output) is simply

https://www.wikidata.org/w/api.php?action=wbgetentities&titles=Andromeda%20Galaxy&sites=enwiki&props=&format=jsonfm&formatversion=2

Try following the link. This requests no additional information about the entity; remove &props= from the URL to see much more information about it. See the generated help for wbgetentities for more parameters you can specify.

Python

The output is:

Q2469

Example 2: Get list of items without particular interwiki

...please contribute if you know how...

같이 보기

바깥 고리