grobid-client-python 0.0.4__py3-none-any.whl → 0.0.7__py3-none-any.whl

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.
@@ -33,7 +33,6 @@ class ServerUnavailableException(Exception):
33
33
  class GrobidClient(ApiClient):
34
34
 
35
35
  def __init__(self, grobid_server='localhost',
36
- grobid_port='8070',
37
36
  batch_size=1000,
38
37
  coordinates=["persName", "figure", "ref", "biblStruct", "formula", "s" ],
39
38
  sleep_time=5,
@@ -42,7 +41,6 @@ class GrobidClient(ApiClient):
42
41
  check_server=True):
43
42
  self.config = {
44
43
  'grobid_server': grobid_server,
45
- 'grobid_port': grobid_port,
46
44
  'batch_size': batch_size,
47
45
  'coordinates': coordinates,
48
46
  'sleep_time': sleep_time,
@@ -62,7 +60,7 @@ class GrobidClient(ApiClient):
62
60
 
63
61
  def _test_server_connection(self):
64
62
  """Test if the server is up and running."""
65
- the_url = self.config['grobid_server'] + "/api/isalive"
63
+ the_url = self.get_server_url("isalive")
66
64
  try:
67
65
  r = requests.get(the_url)
68
66
  except:
@@ -81,13 +79,13 @@ class GrobidClient(ApiClient):
81
79
  if output is not None:
82
80
  input_file_name = str(os.path.relpath(os.path.abspath(input_file), input_path))
83
81
  filename = os.path.join(
84
- output, os.path.splitext(input_file_name)[0] + ".tei.xml"
82
+ output, os.path.splitext(input_file_name)[0] + ".grobid.tei.xml"
85
83
  )
86
84
  else:
87
85
  input_file_name = ntpath.basename(input_file)
88
86
  filename = os.path.join(
89
87
  ntpath.dirname(input_file),
90
- os.path.splitext(input_file_name)[0] + ".tei.xml",
88
+ os.path.splitext(input_file_name)[0] + ".grobid.tei.xml",
91
89
  )
92
90
 
93
91
  return filename
@@ -214,8 +212,18 @@ class GrobidClient(ApiClient):
214
212
  input_file, status, text = r.result()
215
213
  filename = self._output_file_name(input_file, input_path, output)
216
214
 
217
- if text is None:
218
- print("Processing of", input_file, "failed with error", str(status))
215
+ if status != 200 or text is None:
216
+ print("Processing of", input_file, "failed with error", str(status), ",", text)
217
+ # writing error file with suffixed error code
218
+ try:
219
+ pathlib.Path(os.path.dirname(filename)).mkdir(parents=True, exist_ok=True)
220
+ with open(filename.replace(".grobid.tei.xml", "_"+str(status)+".txt"), 'w', encoding='utf8') as tei_file:
221
+ if text is not None:
222
+ tei_file.write(text)
223
+ else:
224
+ tei_file.write("")
225
+ except OSError:
226
+ print("Writing resulting TEI XML file", filename, "failed")
219
227
  else:
220
228
  # writing TEI file
221
229
  try:
@@ -247,7 +255,7 @@ class GrobidClient(ApiClient):
247
255
  )
248
256
  }
249
257
 
250
- the_url = self.config['grobid_server'] + "/api/"+service
258
+ the_url = self.get_server_url(service)
251
259
 
252
260
  # set the GROBID parameters
253
261
  the_data = {}
@@ -291,6 +299,9 @@ class GrobidClient(ApiClient):
291
299
  pdf_handle.close()
292
300
  return (pdf_file, status, res.text)
293
301
 
302
+ def get_server_url(self, service):
303
+ return self.config['grobid_server'] + "/api/" + service
304
+
294
305
  def process_txt(
295
306
  self,
296
307
  service,
@@ -308,10 +319,7 @@ class GrobidClient(ApiClient):
308
319
  with open(txt_file) as f:
309
320
  references = [line.rstrip() for line in f]
310
321
 
311
- the_url = "http://" + self.config["grobid_server"]
312
- if len(self.config["grobid_port"]) > 0:
313
- the_url += ":" + self.config["grobid_port"]
314
- the_url += "/api/" + service
322
+ the_url = self.get_server_url(service)
315
323
 
316
324
  # set the GROBID parameters
317
325
  the_data = {}
@@ -187,7 +187,7 @@
187
187
  same "printed page" as the copyright notice for easier
188
188
  identification within third-party archives.
189
189
 
190
- Copyright 2018-2021 The Contributors
190
+ Copyright 2018-2023 The Contributors
191
191
 
192
192
  Licensed under the Apache License, Version 2.0 (the "License");
193
193
  you may not use this file except in compliance with the License.
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: grobid-client-python
3
- Version: 0.0.4
3
+ Version: 0.0.7
4
4
  Summary: Simple python client for GROBID REST services
5
5
  Home-page: https://github.com/kermitt2/grobid_client_python
6
6
  Author: kermitt2
7
7
  License: LICENSE
8
- Platform: UNKNOWN
9
8
  Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
10
  Requires-Dist: requests
11
11
 
12
12
  [![PyPI version](https://badge.fury.io/py/grobid_client_python.svg)](https://badge.fury.io/py/grobid_client_python)
@@ -17,9 +17,17 @@ Requires-Dist: requests
17
17
 
18
18
  This Python client can be used to process in an efficient concurrent manner a set of PDF in a given directory by the [GROBID](https://github.com/kermitt2/grobid) service. It includes a command line for processing PDF on a file system and write results in a given output directory and a library for import in other python scripts. The client can also process similarly a list of files with reference strings (one per line).
19
19
 
20
+ ## Before you start
21
+
22
+ Please be aware that, at the moment, [grobid does not support Windows](https://grobid.readthedocs.io/en/latest/Troubleshooting/#windows-related-issues).
23
+ If you are a Windows user, don't worry. You can still [run grobid
24
+ via Docker](https://grobid.readthedocs.io/en/latest/Grobid-docker/).
25
+
20
26
  ## Build and run
21
27
 
22
- You need first to install and start the *grobid* service, latest stable version, see the [documentation](http://grobid.readthedocs.io/). It is assumed that the server will run on the address `http://localhost:8070`. You can change the server address by editing the file `config.json`.
28
+ You need first a running *grobid* service, latest stable version, see the [documentation](http://grobid.readthedocs.io/) for installation.
29
+ By default, it is assumed that the server will run on the address `http://localhost:8070`.
30
+ You can change the server address by editing the file `config.json`, see below.
23
31
 
24
32
  ## Requirements
25
33
 
@@ -29,7 +37,7 @@ This client has been developed and was tested with Python `3.5`-`3.8` and should
29
37
 
30
38
  Get the github repo:
31
39
 
32
- ```
40
+ ```console
33
41
  git clone https://github.com/kermitt2/grobid_client_python
34
42
  cd grobid_client_python
35
43
  python3 setup.py install
@@ -37,6 +45,7 @@ python3 setup.py install
37
45
 
38
46
  There is nothing more needed to start using the python command lines, see the next section.
39
47
 
48
+
40
49
  ## Usage and options
41
50
 
42
51
  ```
@@ -88,25 +97,33 @@ optional arguments:
88
97
 
89
98
  Examples:
90
99
 
100
+ ```console
91
101
  > grobid_client --input ~/tmp/in2 --output ~/tmp/out processFulltextDocument
102
+ ```
92
103
 
93
- This command will process all the PDF files present under the input directory recursively (files with extension `.pdf` only) with the `processFulltextDocument` service of GROBID, and write the resulting XML TEI files under the output directory, reusing the file name with a different file extension (`.tei.xml`), using the default `10` concurrent workers.
104
+ This command will process all the PDF files present under the input directory recursively (files with extension `.pdf` only) with the `processFulltextDocument` service of GROBID, and write the resulting XML TEI files under the output directory, reusing the file name with a different file extension (`.grobid.tei.xml`), using the default `10` concurrent workers.
94
105
 
95
106
  If `--output` is omitted, the resulting XML TEI documents will be produced alongside the PDF in the `--input` directory.
96
107
 
108
+ ```console
97
109
  > grobid_client --input ~/tmp/in2 --output ~/tmp/out --n 20 processHeaderDocument
110
+ ```
98
111
 
99
- This command will process all the PDF files present in the input directory (files with extension `.pdf` only) with the `processHeaderDocument` service of GROBID, and write the resulting XML TEI files under the output directory, reusing the file name with a different file extension (`.tei.xml`), using `20` concurrent workers.
112
+ This command will process all the PDF files present in the input directory (files with extension `.pdf` only) with the `processHeaderDocument` service of GROBID, and write the resulting XML TEI files under the output directory, reusing the file name with a different file extension (`.grobid.tei.xml`), using `20` concurrent workers.
100
113
 
101
- By default if an existing `.tei.xml` file is present in the output directory corresponding to a PDF in the input directory, this PDF will be skipped to avoid reprocessing several times the same PDF. To force the processing of PDF and over-write of existing TEI files, use the parameter `--force`.
114
+ By default if an existing `.grobid.tei.xml` file is present in the output directory corresponding to a PDF in the input directory, this PDF will be skipped to avoid reprocessing several times the same PDF. To force the processing of PDF and over-write of existing TEI files, use the parameter `--force`.
102
115
 
103
116
  `processCitationList` does not take a repertory of PDF as input, but a repertory of `.txt` files, with one reference raw string per line, for example:
104
117
 
118
+ ```console
105
119
  > grobid_client --input resources/test_txt/ --output resources/test_out/ --n 20 processCitationList
120
+ ```
106
121
 
107
122
  The following command example will process all the PDF files present in the input directory and add bounding box coordinates (`--teiCoordinates`) relative to the original PDFs for the elements listed in the config file. It will also segment the sentences (`--segmentSentences`, this is a "layout aware" sentence segmentation) in the identified paragraphs with bounding box coordinates for the sentences.
108
123
 
124
+ ```console
109
125
  > grobid_client --input ~/tmp/in2 --output ~/tmp/out --teiCoordinates --segmentSentences processFulltextDocument
126
+ ```
110
127
 
111
128
  The file `example.py` gives an example of usage as a library, from a another python script.
112
129
 
@@ -114,7 +131,7 @@ The file `example.py` gives an example of usage as a library, from a another pyt
114
131
 
115
132
  Import and call the client as follow:
116
133
 
117
- ```
134
+ ```python
118
135
  from grobid_client.grobid_client import GrobidClient
119
136
 
120
137
  client = GrobidClient(config_path="./config.json")
@@ -123,6 +140,32 @@ client.process("processFulltextDocument", "/mnt/data/covid/pdfs", n=20)
123
140
 
124
141
  See also `example.py`.
125
142
 
143
+ ## Configuration of the client
144
+
145
+ There are a few parameters that can be set with the `config.json` file.
146
+
147
+ - `grobid_server` indicates the URL of the GROBID server to be used by the client.
148
+
149
+ - `batch_size` is the the size of the pool of threads used by ThreadPoolExecutor, you normally don't want to change this. This should be a high number (default 1000) - but not too high to protect the memory on the machine running the client. This should not be confused with the concurrency parameter `n` which indicates how many parallel requests can be send to GROBID.
150
+
151
+ - `sleep_time` indicates in seconds the time to wait for sending a new request to GROBID when the server indicates that all its threads are currently used. The client need to re-send the query after a wait time that will allow the server to free some threads. This wait time usually depends on the service and the capacities of the server, we suggest 5-10 seconds for the `processFulltextDocument` service and 2 seconds for `processHeaderDocument` service.
152
+
153
+ - `timeout` is a client side timeout - the process on server side will still be running until the server finished the task or the server timeout is reached.
154
+
155
+ - `coordinates` indicates the structure XML elements that should contains PDF coordinates when the parameters `--teiCoordinates` is used see [here](https://grobid.readthedocs.io/en/latest/Coordinates-in-PDF/) for more details.
156
+
157
+ Here is the default `config.json` file for the client:
158
+
159
+ ```
160
+ {
161
+ "grobid_server": "http://localhost:8070",
162
+ "batch_size": 1000,
163
+ "sleep_time": 5,
164
+ "timeout": 60,
165
+ "coordinates": [ "persName", "figure", "ref", "biblStruct", "formula", "s" ]
166
+ }
167
+ ```
168
+
126
169
  ## Benchmarking
127
170
 
128
171
  Full text processing of __136 PDF__ (total 3443 pages, in average 25 pages per PDF) on Intel Core i7-4790K CPU 4.00GHz, 4 cores (8 threads), 16GB memory, `n` being the concurrency parameter:
@@ -150,5 +193,3 @@ Processing of 3500 raw bibliographical take 4.3 s with `n=10` (814 references pa
150
193
  Distributed under [Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0).
151
194
 
152
195
  Main author and contact: Patrice Lopez (<patrice.lopez@science-miner.com>)
153
-
154
-
@@ -0,0 +1,9 @@
1
+ grobid_client/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ grobid_client/client.py,sha256=crvUiVnnkn6tFwO4lKQg2cTxAHZxCBAmeNSVtuL4ZA0,6009
3
+ grobid_client/grobid_client.py,sha256=wHgfrA-HJUPaXgKz7LjKBJ7ol84Tj_vuDtA5HRbs6k4,16720
4
+ grobid_client_python-0.0.7.dist-info/LICENSE,sha256=L35Ao91pn_wG-mYDM3RfG7WKUjOnRmrCNHFSUQwXhKI,11184
5
+ grobid_client_python-0.0.7.dist-info/METADATA,sha256=vLlG2E2D4BNPGLmJJ187zfpp1IO14p9Ww6gkILugaUM,9862
6
+ grobid_client_python-0.0.7.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
7
+ grobid_client_python-0.0.7.dist-info/entry_points.txt,sha256=-WwPi8D8eXEbDB6QvitvHjxSGBeyLCyFvAoZcaDGswQ,67
8
+ grobid_client_python-0.0.7.dist-info/top_level.txt,sha256=HnjgWxdJNKTLIN86XkgRAfVeEK4C71fc9X4fe7qdMvA,14
9
+ grobid_client_python-0.0.7.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.34.2)
2
+ Generator: bdist_wheel (0.38.4)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,3 +1,2 @@
1
1
  [console_scripts]
2
2
  grobid_client = grobid_client.grobid_client:main
3
-
@@ -1,9 +0,0 @@
1
- grobid_client/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- grobid_client/client.py,sha256=crvUiVnnkn6tFwO4lKQg2cTxAHZxCBAmeNSVtuL4ZA0,6009
3
- grobid_client/grobid_client.py,sha256=cEjSial_8ubdOYcE7h1BkwIw0jGWuKqXat4XDeMki_s,16250
4
- grobid_client_python-0.0.4.dist-info/LICENSE,sha256=O3ECu2kCEzpF8fBgQabIWw1muFjkZeYP_szjjhzOBW4,11184
5
- grobid_client_python-0.0.4.dist-info/METADATA,sha256=BVyrwD78Kqi3mdcv6J1SBMck9g9YFTOaDbVTS5K4PlA,7760
6
- grobid_client_python-0.0.4.dist-info/WHEEL,sha256=g4nMs7d-Xl9-xC9XovUrsDHGXt-FT0E17Yqo92DEfvY,92
7
- grobid_client_python-0.0.4.dist-info/entry_points.txt,sha256=IfhMcY5YhUem5uQ4ku1HbfAWjNLvptr0wJbMUubrdCs,68
8
- grobid_client_python-0.0.4.dist-info/top_level.txt,sha256=HnjgWxdJNKTLIN86XkgRAfVeEK4C71fc9X4fe7qdMvA,14
9
- grobid_client_python-0.0.4.dist-info/RECORD,,