poetree 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ MIT License
2
+ Copyright (c) 2021 Petr Plechac
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+ The above copyright notice and this permission notice shall be included in all
10
+ copies or substantial portions of the Software.
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17
+ SOFTWARE.
poetree-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,175 @@
1
+ Metadata-Version: 2.1
2
+ Name: poetree
3
+ Version: 0.0.1
4
+ Summary: An easy way to get data from PoeTree dataset
5
+ Home-page: https://github.com/versotym/poetree
6
+ Download-URL: https://github.com/versotym/poetree/archive/v0.0.1.tar.gz
7
+ Author: Petr Plechac
8
+ Author-email: plechac@ucl.cas.cz
9
+ License: MIT
10
+ Keywords: poetry,corpus,versification
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Topic :: Text Processing :: Linguistic
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.1
17
+ Classifier: Programming Language :: Python :: 3.2
18
+ Classifier: Programming Language :: Python :: 3.3
19
+ Classifier: Programming Language :: Python :: 3.4
20
+ Classifier: Programming Language :: Python :: 3.5
21
+ Classifier: Programming Language :: Python :: 3.6
22
+ Classifier: Programming Language :: Python :: 3.7
23
+ Classifier: Programming Language :: Python :: 3.8
24
+ Classifier: Programming Language :: Python :: 3.9
25
+ Classifier: Programming Language :: Python :: 3.10
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE.txt
28
+
29
+ # poetree
30
+ Poetree library provides an easy way to get data from PoeTree API.
31
+
32
+ [PoeTree](https://versologie.cz/poetree) is a standardized collection of poetry corpora comprising over 330,000 poems in ten languages (Czech, English, French, German, Hungarian, Italian, Portuguese, Russian, Slovenian and Spanish). Each corpus has been deduplicated, enriched with Universal Dependencies, provided with additional metadata and converted into a unified JSON structure.
33
+
34
+ ## Install
35
+ ```console
36
+ pip install poetree
37
+ ```
38
+ or
39
+ ```console
40
+ pip3 install poetree
41
+ ```
42
+
43
+ ## Usage
44
+ ```python
45
+ import poetree
46
+ ```
47
+
48
+ There are five classes available:
49
+ - ```Poetree``` represents the entire PoeTree collection [\[documentation\]](https://versologie.cz/poetree/python-doc/poetree)
50
+ - ```Corpus``` represents single corpus [\[documentation\]](https://versologie.cz/poetree/python-doc/corpus)
51
+ - ```Author``` represents single author [\[documentation\]](https://versologie.cz/poetree/python-doc/author)
52
+ - ```Source``` represents single book [\[documentation\]](https://versologie.cz/poetree/python-doc/source)
53
+ - ```Poem``` represents single poem [\[documentation\]](https://versologie.cz/poetree/python-doc/poem)
54
+
55
+ Each class has properties corresponding to the keys returned by API. To get for instance the number of authors and the number of poems in PoeTree.cs:
56
+
57
+ ```python
58
+ corpus = poetree.Corpus('cs')
59
+ print('number of authors:', corpus.n_authors)
60
+ print('number of poems:', corpus.n_poems)
61
+ ```
62
+ ```console
63
+ number of authors: 606
64
+ number of poems: 80229
65
+ ```
66
+
67
+ ### get_corpora(), get_authors(), get_sources(), get_poems()
68
+ Class instance may be created directly (as in the example above) or one may use a more general class to create a number of children instances by means of ```get_[something]()``` methods. To get for instance all the authors in PoeTree.en born between 1750 and 1760:
69
+
70
+ ```python
71
+ corpus = poetree.Corpus('en')
72
+ for author in corpus.get_authors(born_after=1750, born_before=1760):
73
+ print(f'{author.name} ({author.born})')
74
+ ```
75
+ ```console
76
+ Barlow, Joel (1754)
77
+ Blake, William (1757)
78
+ Crabbe, George (1754)
79
+ Freneau, Philip Morin (1752)
80
+ Roberts, David (1757)
81
+ Wheatley, Phillis (1753)
82
+ ```
83
+
84
+ Analogous methods in other classes are as follow:
85
+
86
+ <img src="https://versologie.cz/poetree/img/classes.png" style="width:300px;"/>
87
+
88
+ Objects created by ```get_[something]()``` are also stored in their parent object's property ```data_[something]``` for later use:
89
+
90
+ ```python
91
+ corpus = poetree.Corpus('en')
92
+ corpus.get_authors(born_after=1750, born_before=1760)
93
+ print([x.name for x in corpus.content_['authors']])
94
+ ```
95
+ ```console
96
+ ['Barlow, Joel', 'Blake, William', 'Crabbe, George', 'Freneau, Philip Morin', 'Roberts, David', 'Wheatley, Phillis']
97
+ ```
98
+
99
+ To iterate over all corpora, over all authors, over all their poems:
100
+ ```python
101
+ pt = poetree.Poetree()
102
+ for corpus in pt.get_corpora():
103
+ for author in corpus.get_authors():
104
+ for poem in author.get_poems():
105
+ # Do stuff...
106
+ ```
107
+
108
+ ### metadata()
109
+ Each class provides```metadata()``` method. By default it gives access to all metadata properties of the class:
110
+
111
+ ```python
112
+ corpus = poetree.Corpus('de')
113
+ metadata = corpus.metadata(output='pandas')
114
+ print(metadata)
115
+ ```
116
+ ```console
117
+ corpus desc n_authors n_poems n_lines n_types n_tokens
118
+ 0 de Compiled by Klemens Bobenhausen and Benjamin H... 245 53133 1701234 678426 12482485
119
+ ```
120
+
121
+ It can also be used to get metadata of all children instances it created using the ```target``` attribute:
122
+ ```python
123
+ corpus = poetree.Corpus('pt')
124
+ corpus.get_authors()
125
+ df = corpus.metadata(target='authors', output='pandas')
126
+ print(df)
127
+ ```
128
+ ```console
129
+ id_ name viaf wiki country born died n_poems corpus
130
+ 0 20 Alberto de Oliveira 29092555 Q1789301 br 1857 1937 1 pt
131
+ 1 6 Antônio Gonçalves Dias 73988798 Q611997 br 1823 1864 15 pt
132
+ 2 1 Augusto de Carvalho Rodrigues dos Anjos 44342515 Q769887 br 1884 1914 306 pt
133
+ 3 11 Basílio da Gama 84492428 Q1789371 pt 1740 1795 6 pt
134
+ 4 4 Cláudio Manuel da Costa 41967264 Q1789904 br 1729 1789 190 pt
135
+ 5 25 Delminda Silveira de Sousa 99136109 Q10264874 br 1854 1932 548 pt
136
+ 6 8 Emílio Nunes Correia de Meneses 71253555 Q10272640 br 1866 1918 90 pt
137
+ 7 17 Francisco de Sá de Meneses 32338601 Q4403329 pt 1600 1664 12 pt
138
+ 8 15 Gonçalves de Magalhães 88876973 Q2532628 br 1811 1882 56 pt
139
+ 9 16 Gregório de Matos Guerra 122323020 Q983565 br 1636 1696 704 pt
140
+ 10 13 Gustavo de Paula Teixeira 60882245 Q10293150 br 1881 1937 122 pt
141
+ 11 23 José Pedro Xavier Pinheiro 121971768 Q15631816 br 1822 1882 100 pt
142
+ 12 14 José Joaquim Correia de Almeida 50626334 Q25859802 br 1820 1905 953 pt
143
+ 13 7 José de Santa Rita Durão 730145601965601320827 Q6958165 br 1722 1784 10 pt
144
+ 14 5 João da Cruz e Sousa 4972695 Q2609059 br 1861 1898 407 pt
145
+ 15 10 Juvêncio de Araújo Figueredo 4738150325583310090002 Q10313264 br 1865 1927 438 pt
146
+ 16 24 Laurindo José da Silva Rabelo 65149066775765602956 Q6501838 br 1826 1864 184 pt
147
+ 17 9 Luís Nicolau Fagundes Varela 24730690 Q2088965 br 1841 1875 215 pt
148
+ 18 3 Luís Vaz de Camões 34454091 Q590 pt 1524 1580 10 pt
149
+ 19 2 Manuel Maria Barbosa l'Hedois du Bocage 66536132 Q630116 pt 1765 1805 146 pt
150
+ 20 18 Múcio Scevola Lopes Teixeira 11159986 Q10334840 br 1857 1926 86 pt
151
+ 21 19 Nicolau Tolentino de Almeida 54191676 Q740967 pt 1740 1811 244 pt
152
+ 22 21 Olavo Brás Martins dos Guimarães Bilac 73898176 Q982354 br 1865 1918 75 pt
153
+ 23 22 Sebastião Cícero dos Guimarães Passos 25942646 Q10292895 br 1867 1909 75 pt
154
+ 24 12 Tomás Antônio Gonzaga 19760014 Q1334602 br 1744 1810 110 pt
155
+ ```
156
+
157
+ ### get_body(), get_all()
158
+
159
+ When ```Poem``` instance is created, only its metadata are fetched from API. To get the body (lines, words and their annotation) one needs to call ```get_body()``` method first.
160
+
161
+ ```python
162
+ poem = poetree.Poem(id_=1, lang='cs')
163
+ body = poem.get_body()
164
+ print(body[0])
165
+ ```
166
+ ```console
167
+ {'id_': 1, 'id': 0, 'id_stanza': 1, 'text': 'Tvá loď jde po vysokém moři,', 'part': False, 'words': [{'id_': 1, 'id': 1, 'id_sentence': 1, 'head': 2, 'deprel': 'det', 'form': 'Tvá', 'lemma': 'tvůj', 'upos': 'DET', 'xpos': 'PSFS1-S1------1', 'feats': 'Case=Nom|Gender=Fem|Number=Sing|PronType=Dem'}, {'id_': 2, 'id': 2, 'id_sentence': 1, 'head': 3, 'deprel': 'nsubj', 'form': 'loď', 'lemma': 'loď', 'upos': 'NOUN', 'xpos': 'NNFS1-----A----', 'feats': 'Case=Nom|Gender=Fem|Number=Sing|Polarity=Pos'}, {'id_': 3, 'id': 3, 'id_sentence': 1, 'head': 0, 'deprel': 'root', 'form': 'jde', 'lemma': 'jít', 'upos': 'VERB', 'xpos': 'VB-S---3P-AA---', 'feats': 'Mood=Ind|Number=Sing|Person=3|Polarity=Pos|Tense=Pres|VerbForm=Fin|Voice=Act'}, {'id_': 4, 'id': 4, 'id_sentence': 1, 'head': 6, 'deprel': 'case', 'form': 'po', 'lemma': 'po', 'upos': 'ADP', 'xpos': 'RR--6----------', 'feats': 'AdpType=Prep|Case=Loc'}, {'id_': 5, 'id': 5, 'id_sentence': 1, 'head': 6, 'deprel': 'amod', 'form': 'vysokém', 'lemma': 'vysoký', 'upos': 'ADJ', 'xpos': 'AANS6----1A----', 'feats': 'Case=Loc|Degree=Pos|Gender=Neut|Number=Sing|Polarity=Pos'}, {'id_': 6, 'id': 6, 'id_sentence': 1, 'head': 3, 'deprel': 'obl', 'form': 'moři', 'lemma': 'moře', 'upos': 'NOUN', 'xpos': 'NNNS6-----A----', 'feats': 'Case=Loc|Gender=Neut|Number=Sing|Polarity=Pos'}, {'id_': 7, 'id': 7, 'id_sentence': 1, 'head': 9, 'deprel': 'punct', 'form': ',', 'lemma': ',', 'upos': 'PUNCT', 'xpos': 'Z:-------------', 'feats': '_'}]}
168
+ ```
169
+
170
+ To retrieve both the metadata and the body of the poem at the same time, there is a method ```get_all()```
171
+
172
+ ```python
173
+ poem = poetree.Poem(id_=1, lang='cs')
174
+ metadata_and_body = poem.get_all()
175
+ ```
@@ -0,0 +1,147 @@
1
+ # poetree
2
+ Poetree library provides an easy way to get data from PoeTree API.
3
+
4
+ [PoeTree](https://versologie.cz/poetree) is a standardized collection of poetry corpora comprising over 330,000 poems in ten languages (Czech, English, French, German, Hungarian, Italian, Portuguese, Russian, Slovenian and Spanish). Each corpus has been deduplicated, enriched with Universal Dependencies, provided with additional metadata and converted into a unified JSON structure.
5
+
6
+ ## Install
7
+ ```console
8
+ pip install poetree
9
+ ```
10
+ or
11
+ ```console
12
+ pip3 install poetree
13
+ ```
14
+
15
+ ## Usage
16
+ ```python
17
+ import poetree
18
+ ```
19
+
20
+ There are five classes available:
21
+ - ```Poetree``` represents the entire PoeTree collection [\[documentation\]](https://versologie.cz/poetree/python-doc/poetree)
22
+ - ```Corpus``` represents single corpus [\[documentation\]](https://versologie.cz/poetree/python-doc/corpus)
23
+ - ```Author``` represents single author [\[documentation\]](https://versologie.cz/poetree/python-doc/author)
24
+ - ```Source``` represents single book [\[documentation\]](https://versologie.cz/poetree/python-doc/source)
25
+ - ```Poem``` represents single poem [\[documentation\]](https://versologie.cz/poetree/python-doc/poem)
26
+
27
+ Each class has properties corresponding to the keys returned by API. To get for instance the number of authors and the number of poems in PoeTree.cs:
28
+
29
+ ```python
30
+ corpus = poetree.Corpus('cs')
31
+ print('number of authors:', corpus.n_authors)
32
+ print('number of poems:', corpus.n_poems)
33
+ ```
34
+ ```console
35
+ number of authors: 606
36
+ number of poems: 80229
37
+ ```
38
+
39
+ ### get_corpora(), get_authors(), get_sources(), get_poems()
40
+ Class instance may be created directly (as in the example above) or one may use a more general class to create a number of children instances by means of ```get_[something]()``` methods. To get for instance all the authors in PoeTree.en born between 1750 and 1760:
41
+
42
+ ```python
43
+ corpus = poetree.Corpus('en')
44
+ for author in corpus.get_authors(born_after=1750, born_before=1760):
45
+ print(f'{author.name} ({author.born})')
46
+ ```
47
+ ```console
48
+ Barlow, Joel (1754)
49
+ Blake, William (1757)
50
+ Crabbe, George (1754)
51
+ Freneau, Philip Morin (1752)
52
+ Roberts, David (1757)
53
+ Wheatley, Phillis (1753)
54
+ ```
55
+
56
+ Analogous methods in other classes are as follow:
57
+
58
+ <img src="https://versologie.cz/poetree/img/classes.png" style="width:300px;"/>
59
+
60
+ Objects created by ```get_[something]()``` are also stored in their parent object's property ```data_[something]``` for later use:
61
+
62
+ ```python
63
+ corpus = poetree.Corpus('en')
64
+ corpus.get_authors(born_after=1750, born_before=1760)
65
+ print([x.name for x in corpus.content_['authors']])
66
+ ```
67
+ ```console
68
+ ['Barlow, Joel', 'Blake, William', 'Crabbe, George', 'Freneau, Philip Morin', 'Roberts, David', 'Wheatley, Phillis']
69
+ ```
70
+
71
+ To iterate over all corpora, over all authors, over all their poems:
72
+ ```python
73
+ pt = poetree.Poetree()
74
+ for corpus in pt.get_corpora():
75
+ for author in corpus.get_authors():
76
+ for poem in author.get_poems():
77
+ # Do stuff...
78
+ ```
79
+
80
+ ### metadata()
81
+ Each class provides```metadata()``` method. By default it gives access to all metadata properties of the class:
82
+
83
+ ```python
84
+ corpus = poetree.Corpus('de')
85
+ metadata = corpus.metadata(output='pandas')
86
+ print(metadata)
87
+ ```
88
+ ```console
89
+ corpus desc n_authors n_poems n_lines n_types n_tokens
90
+ 0 de Compiled by Klemens Bobenhausen and Benjamin H... 245 53133 1701234 678426 12482485
91
+ ```
92
+
93
+ It can also be used to get metadata of all children instances it created using the ```target``` attribute:
94
+ ```python
95
+ corpus = poetree.Corpus('pt')
96
+ corpus.get_authors()
97
+ df = corpus.metadata(target='authors', output='pandas')
98
+ print(df)
99
+ ```
100
+ ```console
101
+ id_ name viaf wiki country born died n_poems corpus
102
+ 0 20 Alberto de Oliveira 29092555 Q1789301 br 1857 1937 1 pt
103
+ 1 6 Antônio Gonçalves Dias 73988798 Q611997 br 1823 1864 15 pt
104
+ 2 1 Augusto de Carvalho Rodrigues dos Anjos 44342515 Q769887 br 1884 1914 306 pt
105
+ 3 11 Basílio da Gama 84492428 Q1789371 pt 1740 1795 6 pt
106
+ 4 4 Cláudio Manuel da Costa 41967264 Q1789904 br 1729 1789 190 pt
107
+ 5 25 Delminda Silveira de Sousa 99136109 Q10264874 br 1854 1932 548 pt
108
+ 6 8 Emílio Nunes Correia de Meneses 71253555 Q10272640 br 1866 1918 90 pt
109
+ 7 17 Francisco de Sá de Meneses 32338601 Q4403329 pt 1600 1664 12 pt
110
+ 8 15 Gonçalves de Magalhães 88876973 Q2532628 br 1811 1882 56 pt
111
+ 9 16 Gregório de Matos Guerra 122323020 Q983565 br 1636 1696 704 pt
112
+ 10 13 Gustavo de Paula Teixeira 60882245 Q10293150 br 1881 1937 122 pt
113
+ 11 23 José Pedro Xavier Pinheiro 121971768 Q15631816 br 1822 1882 100 pt
114
+ 12 14 José Joaquim Correia de Almeida 50626334 Q25859802 br 1820 1905 953 pt
115
+ 13 7 José de Santa Rita Durão 730145601965601320827 Q6958165 br 1722 1784 10 pt
116
+ 14 5 João da Cruz e Sousa 4972695 Q2609059 br 1861 1898 407 pt
117
+ 15 10 Juvêncio de Araújo Figueredo 4738150325583310090002 Q10313264 br 1865 1927 438 pt
118
+ 16 24 Laurindo José da Silva Rabelo 65149066775765602956 Q6501838 br 1826 1864 184 pt
119
+ 17 9 Luís Nicolau Fagundes Varela 24730690 Q2088965 br 1841 1875 215 pt
120
+ 18 3 Luís Vaz de Camões 34454091 Q590 pt 1524 1580 10 pt
121
+ 19 2 Manuel Maria Barbosa l'Hedois du Bocage 66536132 Q630116 pt 1765 1805 146 pt
122
+ 20 18 Múcio Scevola Lopes Teixeira 11159986 Q10334840 br 1857 1926 86 pt
123
+ 21 19 Nicolau Tolentino de Almeida 54191676 Q740967 pt 1740 1811 244 pt
124
+ 22 21 Olavo Brás Martins dos Guimarães Bilac 73898176 Q982354 br 1865 1918 75 pt
125
+ 23 22 Sebastião Cícero dos Guimarães Passos 25942646 Q10292895 br 1867 1909 75 pt
126
+ 24 12 Tomás Antônio Gonzaga 19760014 Q1334602 br 1744 1810 110 pt
127
+ ```
128
+
129
+ ### get_body(), get_all()
130
+
131
+ When ```Poem``` instance is created, only its metadata are fetched from API. To get the body (lines, words and their annotation) one needs to call ```get_body()``` method first.
132
+
133
+ ```python
134
+ poem = poetree.Poem(id_=1, lang='cs')
135
+ body = poem.get_body()
136
+ print(body[0])
137
+ ```
138
+ ```console
139
+ {'id_': 1, 'id': 0, 'id_stanza': 1, 'text': 'Tvá loď jde po vysokém moři,', 'part': False, 'words': [{'id_': 1, 'id': 1, 'id_sentence': 1, 'head': 2, 'deprel': 'det', 'form': 'Tvá', 'lemma': 'tvůj', 'upos': 'DET', 'xpos': 'PSFS1-S1------1', 'feats': 'Case=Nom|Gender=Fem|Number=Sing|PronType=Dem'}, {'id_': 2, 'id': 2, 'id_sentence': 1, 'head': 3, 'deprel': 'nsubj', 'form': 'loď', 'lemma': 'loď', 'upos': 'NOUN', 'xpos': 'NNFS1-----A----', 'feats': 'Case=Nom|Gender=Fem|Number=Sing|Polarity=Pos'}, {'id_': 3, 'id': 3, 'id_sentence': 1, 'head': 0, 'deprel': 'root', 'form': 'jde', 'lemma': 'jít', 'upos': 'VERB', 'xpos': 'VB-S---3P-AA---', 'feats': 'Mood=Ind|Number=Sing|Person=3|Polarity=Pos|Tense=Pres|VerbForm=Fin|Voice=Act'}, {'id_': 4, 'id': 4, 'id_sentence': 1, 'head': 6, 'deprel': 'case', 'form': 'po', 'lemma': 'po', 'upos': 'ADP', 'xpos': 'RR--6----------', 'feats': 'AdpType=Prep|Case=Loc'}, {'id_': 5, 'id': 5, 'id_sentence': 1, 'head': 6, 'deprel': 'amod', 'form': 'vysokém', 'lemma': 'vysoký', 'upos': 'ADJ', 'xpos': 'AANS6----1A----', 'feats': 'Case=Loc|Degree=Pos|Gender=Neut|Number=Sing|Polarity=Pos'}, {'id_': 6, 'id': 6, 'id_sentence': 1, 'head': 3, 'deprel': 'obl', 'form': 'moři', 'lemma': 'moře', 'upos': 'NOUN', 'xpos': 'NNNS6-----A----', 'feats': 'Case=Loc|Gender=Neut|Number=Sing|Polarity=Pos'}, {'id_': 7, 'id': 7, 'id_sentence': 1, 'head': 9, 'deprel': 'punct', 'form': ',', 'lemma': ',', 'upos': 'PUNCT', 'xpos': 'Z:-------------', 'feats': '_'}]}
140
+ ```
141
+
142
+ To retrieve both the metadata and the body of the poem at the same time, there is a method ```get_all()```
143
+
144
+ ```python
145
+ poem = poetree.Poem(id_=1, lang='cs')
146
+ metadata_and_body = poem.get_all()
147
+ ```
@@ -0,0 +1,6 @@
1
+
2
+ from poetree.main import Poetree
3
+ from poetree.corpus import Corpus
4
+ from poetree.author import Author
5
+ from poetree.source import Source
6
+ from poetree.poem import Poem
@@ -0,0 +1,166 @@
1
+ from typing import Union
2
+ import pandas as pd
3
+ from .config import BASE_URL
4
+ from .glob import make_request, metadata, get_content
5
+ from .source import Source
6
+ from .poem import Poem
7
+
8
+
9
+ class Author:
10
+ '''
11
+ Class corresponding to a particular author.
12
+ '''
13
+
14
+ def __init__(
15
+ self,
16
+ lang : Union[None,str] = None,
17
+ base_url : str = BASE_URL,
18
+ id_ : Union[None,int] = None,
19
+ wiki : Union[None,str] = None,
20
+ viaf : Union[None,str] = None,
21
+ metadata : dict = None
22
+ ):
23
+ '''
24
+ Store author metadata (if initialized by Poetree instance) or get them
25
+ from API (if initialized directly). Create empty dict self.content_
26
+ that will hold lists of Source and Poem instances.
27
+
28
+ Arguments:
29
+ lang (str|None) : ISO code of the corpus, required if initialized directly
30
+ base_url (str) : API base URL (default: set in config.py)
31
+ id_ (int|None) : Id(DB) of the author
32
+ wiki (str|None) : Wiki id of the author
33
+ viaf (str|None) : Viaf id of the author
34
+ metadata (dict|None) : Author metadata passed when initialized by Poetree instance
35
+
36
+ Raises:
37
+ ValueError : If neither [metadata] nor [lang] is passed
38
+ : If neither [metadata] nor one of [id_, wiki, viaf] is passed
39
+
40
+ Returns:
41
+ None
42
+ '''
43
+ self.base_url = base_url
44
+ self.content_ = dict()
45
+
46
+ if metadata is not None:
47
+ self.metadata_ = metadata
48
+ elif lang is None:
49
+ raise ValueError (
50
+ 'Argument [lang] is required when initializing ' +
51
+ f'{__class__.__name__} instance directly'
52
+ )
53
+ elif id_ is not None:
54
+ self._get_author_metadata(lang, 'id_author', id_)
55
+ elif wiki is not None:
56
+ self._get_author_metadata(lang, 'wiki', wiki)
57
+ elif viaf is not None:
58
+ self._get_author_metadata(lang, 'viaf', viaf)
59
+ else:
60
+ raise ValueError (
61
+ 'One of the arguments [id_,wiki,viaf] is required when initializing ' +
62
+ f'{__class__.__name__} instance directly'
63
+ )
64
+ for k, v in self.metadata_.items(): setattr(self, k, v)
65
+
66
+
67
+ def _get_author_metadata(
68
+ self,
69
+ lang : str,
70
+ id_type : str,
71
+ id_val : Union[int,str],
72
+ ):
73
+ '''
74
+ Get metadata on author and store them in self.metadata_
75
+
76
+ Arguments:
77
+ lang (str) : ISO code of the corpus
78
+ id_type (str) : Which identifier to use for retrueving author
79
+ id_val (int|str) : Identifier value
80
+
81
+ Returns:
82
+ None
83
+ '''
84
+ self.metadata_ = make_request(
85
+ self.base_url,
86
+ 'author',
87
+ **{'corpus': lang, id_type: id_val}
88
+ )
89
+ self.metadata_['corpus'] = lang
90
+
91
+
92
+ def get_sources(self, **kwargs) -> list:
93
+ '''
94
+ Get metadata of sources by the author. Create a new Source instance
95
+ for each source, store it in a list and return it.
96
+
97
+ Arguments:
98
+ None
99
+
100
+ Keyword arguments:
101
+ published_after (int) : Limit to sources published no sooner than a given year
102
+ published_before (int) : Limit to sources published no later than a given year
103
+
104
+ Returns:
105
+ (list) : List holding instances of Source
106
+ '''
107
+ self.content_['sources'] = get_content(
108
+ self.base_url, 'sources', Source,
109
+ corpus = self.metadata_['corpus'],
110
+ id_author = self.metadata_['id_'],
111
+ **kwargs
112
+ )
113
+ return self.content_['sources']
114
+
115
+
116
+ def get_poems(self, **kwargs) -> list:
117
+ '''
118
+ Get metadata of poems by the author. Create a new Poem instance
119
+ for each poem, store it in a list and return it.
120
+
121
+ Arguments:
122
+ None
123
+
124
+ Keyword arguments:
125
+ id_source (int) : Limit to poems from certain source
126
+
127
+ Returns:
128
+ (list) : List holding instances of Poem
129
+ '''
130
+ self.content_['poems'] = get_content(
131
+ self.base_url, 'poems', Poem,
132
+ corpus = self.metadata_['corpus'],
133
+ id_author = self.metadata_['id_'],
134
+ **kwargs
135
+ )
136
+ return self.content_['poems']
137
+
138
+
139
+ def metadata(
140
+ self,
141
+ target : str = 'self',
142
+ output : str = 'list',
143
+ sortby : Union[str,list] = None,
144
+ reverse : bool = False
145
+ ) -> Union[list, pd.DataFrame, None]:
146
+ '''
147
+ Returns targt metadata either as a formatted table (tabular=True)
148
+ or as a list as received from API (tabular=False). The list may be
149
+ sorted according to any subdict key.
150
+
151
+ Params:
152
+ target (str) : Metadata of what to return; default: 'corpora'
153
+ output (str) : Output format: 'list': list as retrieved from API,
154
+ 'pandas': pd.DataFrame, 'print': stringified table
155
+ printed directly; default: 'list'
156
+ sortby (str|None) : Subdict key according to which sort the list;
157
+ default: None
158
+ reverse (bool) : Sort in reversed (descending) order; default False
159
+
160
+ Returns:
161
+ (list|pd.DataFrame|None) : metadata
162
+ '''
163
+ if target == 'self':
164
+ return metadata([self], output, sortby, reverse)
165
+ else:
166
+ return metadata(self.content_[target], output, sortby, reverse)
@@ -0,0 +1,2 @@
1
+
2
+ BASE_URL = 'http://versologie.cz/poetree/api'
@@ -0,0 +1,142 @@
1
+ from typing import Union
2
+ import pandas as pd
3
+ from .config import BASE_URL
4
+ from .glob import make_request, metadata, get_content
5
+ from .author import Author
6
+ from .source import Source
7
+
8
+
9
+ class Corpus:
10
+ '''
11
+ Class corresponding to a particular corpus.
12
+ '''
13
+
14
+ def __init__(
15
+ self,
16
+ lang : Union[str,None] = None,
17
+ base_url : str = BASE_URL,
18
+ metadata : Union[dict,None] = None
19
+ ):
20
+ '''
21
+ Store corpus metadata (if initialized by Poetree instance) or get them
22
+ from API (if initialized directly). Create empty dict self.content_
23
+ that will hold lists of Author and Source instances.
24
+
25
+ Arguments:
26
+ lang (str|None) : ISO code of the corpus, required if initialized directly
27
+ base_url (str) : API base URL (default: set in config.py)
28
+ metadata (dict|None) : Corpus metadata passed when initialized by Poetree instance
29
+
30
+ Raises:
31
+ ValueError : If neither [metadata] nor [lang] is passed
32
+
33
+ Returns:
34
+ None
35
+ '''
36
+ self.base_url = base_url
37
+ self.content_ = dict()
38
+ if metadata is not None:
39
+ self.metadata_ = metadata
40
+ elif lang is not None:
41
+ self._get_corpus_metadata(lang)
42
+ else:
43
+ raise ValueError (
44
+ 'Argument [lang] is required when initializing ' +
45
+ f'{__class__.__name__} instance directly'
46
+ )
47
+ for k, v in self.metadata_.items(): setattr(self, k, v)
48
+
49
+
50
+ def _get_corpus_metadata(self, lang:str):
51
+ '''
52
+ Get metadata on corpus and store them in self.metadata_
53
+
54
+ Arguments:
55
+ lang (str) : Language of the corpus (ISO code)
56
+
57
+ Returns:
58
+ None
59
+ '''
60
+ self.metadata_ = make_request(self.base_url, 'corpus', corpus=lang)
61
+ self.metadata_['corpus'] = lang
62
+
63
+
64
+ def get_authors(self, **kwargs) -> list:
65
+ '''
66
+ Get metadata of all available authors. Create a new Author instance for
67
+ each author, store it in a list and return it.
68
+
69
+ Arguments:
70
+ None
71
+
72
+ Keyword arguments:
73
+ country (str) : Limit to authors from certain countries. Either a single
74
+ value (country="pt") or stringified list (country="pt,br")
75
+ born_after (int) : Limit to authors born no sooner than a given year
76
+ born_before (int) : Limit to authors born no later than a given year
77
+ died_after (int) : Limit to authors that died no sooner than a given year
78
+ died_before (int) : Limit to authors that died no later than a given year
79
+
80
+ Returns:
81
+ (list) : List holding instances of Author
82
+ '''
83
+ if 'country' in kwargs and not isinstance(kwargs['country'], list):
84
+ kwargs['country'] = ','.join(kwargs['country'])
85
+ self.content_['authors'] = get_content(
86
+ self.base_url, 'authors', Author, corpus=self.metadata_['corpus'], **kwargs
87
+ )
88
+ return self.content_['authors']
89
+
90
+
91
+ def get_sources(self, **kwargs) -> list:
92
+ '''
93
+ Get metadata of all available sources. Create a new Source instance
94
+ for each source, store it in a list and return it.
95
+
96
+ Arguments:
97
+ None
98
+
99
+ Keyword arguments:
100
+ id_author (int) : Limit to sources by author with this id(DB)
101
+ wiki (str) : Limit to sources by author with this wiki id
102
+ viaf (str) : Limit to sources by author with this viaf id
103
+ published_after (int) : Limit to sources published no sooner than a given year
104
+ published_before (int) : Limit to sources published no later than a given year
105
+
106
+ Returns:
107
+ (list) : List holding instances of Source
108
+ '''
109
+ self.content_['sources'] = get_content(
110
+ self.base_url, 'sources', Source, corpus=self.metadata_['corpus'], **kwargs
111
+ )
112
+ return self.content_['sources']
113
+
114
+
115
+ def metadata(
116
+ self,
117
+ target : str = 'self',
118
+ output : str = 'list',
119
+ sortby : Union[str,list] = None,
120
+ reverse : bool = False
121
+ ) -> Union[list, pd.DataFrame, None]:
122
+ '''
123
+ Returns targt metadata either as a formatted table (tabular=True)
124
+ or as a list as received from API (tabular=False). The list may be
125
+ sorted according to any subdict key.
126
+
127
+ Params:
128
+ target (str) : Metadata of what to return; default: 'self'
129
+ output (str) : Output format: 'list': list as retrieved from API,
130
+ 'pandas': pd.DataFrame, 'print': stringified table
131
+ printed directly; default: 'list'
132
+ sortby (str|None) : Subdict key according to which sort the list;
133
+ default: None
134
+ reverse (bool) : Sort in reversed (descending) order; default False
135
+
136
+ Returns:
137
+ (list|pd.DataFrame|None) : metadata
138
+ '''
139
+ if target == 'self':
140
+ return metadata([self], output, sortby, reverse)
141
+ else:
142
+ return metadata(self.content_[target], output, sortby, reverse)