🎉

MyCapytainライブラリを試す

2024/09/04に公開

概要

以下のMyCapytainライブラリを試します。

https://github.com/Capitains/MyCapytain

背景

以下の記事で、CTS(Canonical Text Service)を取り上げました。

https://zenn.dev/nakamura196/articles/dd99925d373c2f

以下のページにおいて、CITE, CTS, CapiTainSの説明がなされています。

https://brillpublishers.gitlab.io/documentation-cts/DTS_Guidelines.html

The following document is about CITE, a system for the identification of texts and any other object. CTS is the name for the identification system itself. CapiTainS is the name for the software suite built around it. Before we go into details, we need to ask two questions:

(機械翻訳)以下の文書は、CITE(テキストおよびその他のオブジェクトを識別するためのシステム)に関するものです。CTSは、この識別システム自体の名前です。CapiTainSは、その周りに構築されたソフトウェアスイートの名前です。詳細に入る前に、2つの質問をする必要があります。

さらに、今回使用するMyCapytainライブラリは以下のように説明されています。

MyCapytain is a python library which provides a large set of methods to interact with Text Services API such as the Canonical Text Services, the Distributed Text Services. It also provides a programming interface to exploit local textual resources developed according to the Capitains Guidelines.

(機械翻訳)MyCapytainは、Canonical Text ServicesやDistributed Text ServicesといったText Services APIと連携するための多くのメソッドを提供するPythonライブラリです。また、Capitainsガイドラインに従って開発されたローカルのテキストリソースを活用するためのプログラミングインターフェースも提供しています。

セットアップ

git clone https://github.com/Capitains/MyCapytain
cd MyCapytain
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

使い方: DTS(Distributed Text Services)の利用

以下で、サンプルプログラムが用意されています。

https://github.com/Capitains/MyCapytain/blob/dev/notebooks/Example DTS Remote Resolver.ipynb

以下のAlpheios.netのAPIに問い合わせを行っています。

http://texts.alpheios.net/api/dts

例えば、以下により、Alpheiosによって読み取れる各テキストを検索します。

from MyCapytain.resolvers.dts.api_v1 import HttpDtsResolver

resolver = HttpDtsResolver("http://texts.alpheios.net/api/dts")

# We get the root collection
root = resolver.getMetadata()
# Then we retrieve dynamically all the readableDescendants : it browse automatically the API until
# it does not have seen any missing texts: be careful with this one on huge repositories
readable_collections = root.readableDescendants
print("We found %s collections that can be parsed" % len(readable_collections))

使い方: CTS(Canonical Text Service)の利用

CTSの利用にあたっては、以下でサンプルプログラムが公開されていますが、そのままでは動きませんでした。

https://mycapytain.readthedocs.io/en/latest/

まず http://cts.dh.uni-leipzig.de/api/cts/http://cts.informatik.uni-leipzig.de/pbc/cts/ に変更になっているようでした。

以下を修正する必要がありました。

# from MyCapytain.retrievers.cts5 import CTS
from MyCapytain.retrievers.cts5 import HttpCtsRetriever

そこで、以下のようにプログラムを修正することで、正しく問い合わせを行うことができました。

from MyCapytain.retrievers.cts5 import HttpCtsRetriever

retriever = HttpCtsRetriever("http://cts.informatik.uni-leipzig.de/pbc/cts/")

passage = retriever.getPassage("urn:cts:pbc:bible.parallel.eng.kingjames:1.3")

# Passage is now equal to the string content of http://cts.informatik.uni-leipzig.de/pbc/cts/?request=GetPassage&urn=urn:cts:pbc:bible.parallel.eng.kingjames:1.3
print(passage)

結果、以下が得られました。

<?xml version="1.0" encoding="UTF-8"?>
<GetPassage xmlns="http://relaxng.org/ns/structure/1.0" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns:tei="http://www.tei-c.org/ns/1.0">
  <request>
    <requestName>GetPassage</requestName>
    <requestUrn>urn:cts:pbc:bible.parallel.eng.kingjames:1.3</requestUrn>
  </request>
  <reply>
    <urn>urn:cts:pbc:bible.parallel.eng.kingjames:1.3</urn>
    <passage>Now the serpent was more subtil than any beast of the field which the LORD God had made . And he said unto the woman , Yea , hath God said , Ye shall not eat of every tree of the garden ? And the woman said unto the serpent , We may eat of the fruit of the trees of the garden : But of the fruit of the tree which is in the midst of the garden , God hath said , Ye shall not eat of it , neither shall ye touch it , lest ye die . And the serpent said unto the woman , Ye shall not surely die : For God doth know that in the day ye eat thereof , then your eyes shall be opened , and ye shall be as gods , knowing good and evil . And when the woman saw that the tree was good for food , and that it was pleasant to the eyes , and a tree to be desired to make one wise , she took of the fruit thereof , and did eat , and gave also unto her husband with her ; and he did eat . And the eyes of them both were opened , and they knew that they were naked ; and they sewed fig leaves together , and made themselves aprons . And they heard the voice of the LORD God walking in the garden in the cool of the day : and Adam and his wife hid themselves from the presence of the LORD God amongst the trees of the garden . And the LORD God called unto Adam , and said unto him , Where art thou ? And he said , I heard thy voice in the garden , and I was afraid , because I was naked ; and I hid myself . And he said , Who told thee that thou wast naked ? Hast thou eaten of the tree , whereof I commanded thee that thou shouldest not eat ? And the man said , The woman whom thou gavest to be with me , she gave me of the tree , and I did eat . And the LORD God said unto the woman , What is this that thou hast done ? And the woman said , The serpent beguiled me , and I did eat . And the LORD God said unto the serpent , Because thou hast done this , thou art cursed above all cattle , and above every beast of the field ; upon thy belly shalt thou go , and dust shalt thou eat all the days of thy life : And I will put enmity between thee and the woman , and between thy seed and her seed ; it shall bruise thy head , and thou shalt bruise his heel . Unto the woman he said , I will greatly multiply thy sorrow and thy conception ; in sorrow thou shalt bring forth children ; and thy desire shall be to thy husband , and he shall rule over thee . And unto Adam he said , Because thou hast hearkened unto the voice of thy wife , and hast eaten of the tree , of which I commanded thee , saying , Thou shalt not eat of it : cursed is the ground for thy sake ; in sorrow shalt thou eat of it all the days of thy life ; Thorns also and thistles shall it bring forth to thee ; and thou shalt eat the herb of the field ; In the sweat of thy face shalt thou eat bread , till thou return unto the ground ; for out of it wast thou taken : for dust thou art , and unto dust shalt thou return . And Adam called his wife's name Eve ; because she was the mother of all living . Unto Adam also and to his wife did the LORD God make coats of skins , and clothed them . And the LORD God said , Behold , the man is become as one of us , to know good and evil : and now , lest he put forth his hand , and take also of the tree of life , and eat , and live for ever : Therefore the LORD God sent him forth from the garden of Eden , to till the ground from whence he was taken . So he drove out the man ; and he placed at the east of the garden of Eden Cherubims , and a flaming sword which turned every way , to keep the way of the tree of life .</passage>
    <license>Public Domain</license>
    <source>http://paralleltext.info/data/ retrieved via Canonical Text Service http://cts.informatik.uni-leipzig.de/pbc/cts/ with CTS URN urn:cts:pbc:bible.parallel.eng.kingjames:1.3</source>
  </reply>
</GetPassage>

まとめ

基本的な動作確認のみですが、参考になりましたら幸いです。

Discussion