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 61% complete.
Outdated translations are marked like this.

このページでは、ウィキデータのためのボットを作成する方法について説明しています。新たな使用事例の追加や改変の提案、あなたの持っているコードの共有をぜひ検討してください。

要件

ボットを作成ために必要なのは:

  • コーディングスキル (Python, Perl, PHP...)
  • フレームワーク (下記のうちひとつ) とタスクの実行と完了コード
  • ボットアカウント (使用申請によるフラグも必要)
  • ソースコードエディタ (Notepad++, Geany, vim, emacs)

推奨

Pywikibot

ここではインストール、設定、pywikibotを使用してログインする方法を案内しています。これら3つのステップを実行する必要があるのは、初回だけです。また、ボットのプログラミングの基礎を知るための、いくつかの基本的な事例を紹介します。

インストール

pywikibotインストールの詳細についてはmw:Manual:Pywikibot/InstallationWikidata:Pywikibot - Python 3 Tutorial/Setting up Shopを参照
pywikibotをインストールせず使うには、mw:Manual:Pywikibot/PAWSを参照

pywikibotのインストール:

  • Pythonをインストールする(バージョンは3.5.2以降が必須です。)
  • pywikibotをダウンロードする

設定

pywikibot設定の詳細はmw:Manual:Pywikibot/user-config.pyを参照。

ボットのユーザ名、対象のプロジェクトと言語をuser-config.pyで設定しなければなりません。ウィキデータではプロジェクトと言語のパラメータは同じくwikidataとなります。

以下を追記すると遅延間隔を減らすことができます: put_throttle = 1

ログイン

user-config.pyファイルで設定を済ませた後、以下のようにログインします:

$ python login.py

ボットのパスワードを質問されたら入力してエンターキーを押してください。そうすれば、ログイン状態になっているはずです。

例1: データの取得

この例はダグラス・アダムズを指すページに対するデータを取得します。次のソースコードをファイルに保存してpython example1.pyを実行してください。

item.get()はウィキデータへ接続してデータを取得します。その出力(読み易く再成形済み)は:

{
    '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]]

以下のキーで辞書が出力されます:

  • ページの主張のセット:Freebase識別子Property:P646、「主な作品Property:P800など
  • 項目のラベルを多言語で
  • 項目のサイトリンクを、多言語版ウィキペディアだけでなく、多言語版ウィキクオートも
  • 項目の説明を多言語で
  • 項目の別名を多言語で

その後、辞書のキーと値の組に対するすべてのキーのリスト。 最後に、ダグラス・アダムズに関するウィキデータ項目がQ42であると確認できます。

代替策

上記の例は英語版ウィキペディアの記事を使ってItemPageを取得しました。その代わりに、直接ItemPageを取得することもできます:

例2: ウィキ間リンクの取得

item.get() (接続と取得開始)に続き、例えばサイトリンクを読み出すとします。記事のあるウィキペディア群へのリンクを取得します。

以下がその出力です。

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

item.iterlinks() を使い、上記のサイトリンクすべてに対するイテレータ(反復子)を返すことで、前例とは異なり、記事ごとにプレーンテキストではなくページオブジェクトとして次の処理に渡せる形で出力します(例えば引き続き、ウィキペディアの対応する記事中でテキスト編集に用いるなど)。

例4: 説明の設置

この例では、ダグラス・アダムズについての、英語およびドイツ語の説明を設定します。

ラベルと別名の設定はそれに応じて機能します。

例6: サイトリンクの設置

サイトリンクを設けるには、例4に対応するdictを作成するかページオブジェクトを利用します。

例7: 文の設置

文はClaimクラスを使用して設定されます。以下では、ダグラス・アダムスのplace of birth (P19)Cambridge (Q350)を設定しました。

他のデータ型でも同様に動作します。以下では、IMDb ID (P345)coordinate location (P625)を追加します。

例8: 修飾子の追加

修飾子は文のクラスによっても記述されます。以下では、文にincertae sedis (P678): family (Q35409)を追加します。修飾子を追加する前に忘れずにアイテムを追加してください。

例9: 情報源の追加

情報源も文のクラスで表現されます。修飾子と異なり、情報源は複数の分を含むことがあります。以下では主張の情報源としてstated in (P248):Integrated Taxonomic Information System (Q82575)retrieved (P813) March 20, 2014を追加します。文は事前にアイテムに追加されなければならない。

例10:ページの作成

TODO

例11:サブプロパティの値を取得する

以下では、described by source (P1343) -> Great Soviet Encyclopedia (1969–1978) (Q17378135) -> reference URL (P854)title (P1476)という分枝からサブプロパティの値を取得します。

その他の例

ソースコードを共有する利用者もいます。詳しくは次のリンクを参照。

ウィキデータ・インテグレーター

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/

ノートブックの例

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

半自動的スクリプトの例

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(非推奨)

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.

ダウンロードとインストール

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

既知の問題点

近日公開「しません」...

DotNetDataBot (Deprecated)

インストール

設定

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.

ログイン

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

例1: ウィキページで使用しているIDの取得

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

例2: ウィキ間リンクの取得

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

例3: 説明の設置

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

例4: ラベルの設置

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.

PHPウィキベースAPI

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

例1:基本的な例

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


例2: 主張の作成

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.

概要

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"

ダウンロード

The framework can be downloaded from GitHub here.

説明

例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.

ヒント

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.

インストール

You can install LexData via pip:

$ pip install LexData

ログイン

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):


語彙素の取得

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.

情報の追加

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

ウィキデータの API を直接使用する

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.

例1 : Qナンバーの取得

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...

関連項目

外部リンク