pdftext 0.3.1__tar.gz → 0.3.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pdftext
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Extract structured text from pdfs quickly
5
5
  Home-page: https://github.com/VikParuchuri/pdftext
6
6
  License: Apache-2.0
@@ -69,7 +69,7 @@ The output will be a json list, with each item in the list corresponding to a si
69
69
 
70
70
  - `bbox` - the page bbox, in `[x1, y1, x2, y2]` format
71
71
  - `rotation` - how much the page is rotated, in degrees (`0`, `90`, `180`, or `270`)
72
- - `page_idx` - the index of the page
72
+ - `page` - the index of the page
73
73
  - `blocks` - the blocks that make up the text in the pdf. Approximately equal to a paragraph.
74
74
  - `bbox` - the block bbox, in `[x1, y1, x2, y2]` format
75
75
  - `lines` - the lines inside the block
@@ -46,7 +46,7 @@ The output will be a json list, with each item in the list corresponding to a si
46
46
 
47
47
  - `bbox` - the page bbox, in `[x1, y1, x2, y2]` format
48
48
  - `rotation` - how much the page is rotated, in degrees (`0`, `90`, `180`, or `270`)
49
- - `page_idx` - the index of the page
49
+ - `page` - the index of the page
50
50
  - `blocks` - the blocks that make up the text in the pdf. Approximately equal to a paragraph.
51
51
  - `bbox` - the block bbox, in `[x1, y1, x2, y2]` format
52
52
  - `lines` - the lines inside the block
@@ -3,6 +3,7 @@ from itertools import chain
3
3
  import sklearn
4
4
 
5
5
  from pdftext.pdf.utils import LINE_BREAKS, TABS, SPACES
6
+ from pdftext.settings import settings
6
7
 
7
8
 
8
9
  def update_current(current, new_char):
@@ -98,7 +99,7 @@ def update_block(blocks, block):
98
99
  return block
99
100
 
100
101
 
101
- def infer_single_page(text_chars):
102
+ def infer_single_page(text_chars, block_threshold=settings.BLOCK_THRESHOLD):
102
103
  prev_char = None
103
104
  prev_font_info = None
104
105
 
@@ -120,14 +121,15 @@ def infer_single_page(text_chars):
120
121
  sorted_keys = sorted(training_row.keys())
121
122
  training_row = [training_row[key] for key in sorted_keys]
122
123
 
123
- prediction = yield training_row
124
- if prediction == 0:
124
+ prediction_probs = yield training_row
125
+ # First item is probability of same line/block, second is probability of new line, third is probability of new block
126
+ if prediction_probs[0] >= .5:
125
127
  pass
126
- elif prediction == 2 and prev_char["char"] in LINE_BREAKS:
128
+ elif prediction_probs[2] > block_threshold:
127
129
  span = update_span(line, span)
128
130
  line = update_line(block, line)
129
131
  block = update_block(blocks, block)
130
- elif prev_char["char"] in LINE_BREAKS and prediction == 1: # Look for newline character as a forcing signal for a new line
132
+ elif prev_char["char"] in LINE_BREAKS and prediction_probs[1] >= .5: # Look for newline character as a forcing signal for a new line
131
133
  span = update_span(line, span)
132
134
  line = update_line(block, line)
133
135
  elif prev_font_info != font_info:
@@ -180,7 +182,7 @@ def inference(text_chars, model):
180
182
 
181
183
  # Disable nan, etc, validation for a small speedup
182
184
  with sklearn.config_context(assume_finite=True):
183
- predictions = model.predict(training_rows)
185
+ predictions = model.predict_proba(training_rows)
184
186
  for pred, page_idx in zip(predictions, training_idxs):
185
187
  next_prediction[page_idx] = pred
186
188
  sorted_keys = sorted(page_blocks.keys())
@@ -36,7 +36,7 @@ def handle_hyphens(text: str, keep_hyphens=False) -> str:
36
36
  new_text = ""
37
37
  found_hyphen = False
38
38
  for i in range(len(text) - 1):
39
- if text[i] == HYPHEN_CHAR and text[i+1] in LINE_BREAKS:
39
+ if text[i] == HYPHEN_CHAR:
40
40
  found_hyphen = True
41
41
  elif found_hyphen:
42
42
  if text[i] in LINE_BREAKS:
@@ -106,7 +106,7 @@ def merge_text(page: Dict, sort=False, hyphens=False) -> str:
106
106
  line_text = line_text.rstrip() + "\n"
107
107
 
108
108
  block_text += line_text
109
- block_text = block_text.rstrip() + "\n"
109
+ block_text = block_text.rstrip() + "\n\n"
110
110
  text += block_text
111
111
  text = handle_hyphens(text, keep_hyphens=hyphens)
112
112
  return text
@@ -7,10 +7,13 @@ class Settings(BaseSettings):
7
7
  BASE_PATH: str = os.path.dirname(os.path.dirname(__file__))
8
8
  MODEL_PATH: str = os.path.join(BASE_PATH, "models", "dt.joblib")
9
9
 
10
- # How many characters to buffer when reading a font name
11
- FONT_BUFFER_SIZE: int = 1024
10
+ # Fonts
11
+ FONT_BUFFER_SIZE: int = 1024 # How many characters to buffer when reading a font name
12
12
  FONTNAME_SAMPLE_FREQ: int = 10
13
13
 
14
+ # Inference
15
+ BLOCK_THRESHOLD: float = 0.8 # Confidence threshold for block detection
16
+
14
17
  # Benchmark
15
18
  RESULTS_FOLDER: str = "results"
16
19
  BENCH_DATASET_NAME: str = "vikp/pdf_bench"
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "pdftext"
3
- version = "0.3.1"
3
+ version = "0.3.2"
4
4
  description = "Extract structured text from pdfs quickly"
5
5
  authors = ["Vik Paruchuri <vik.paruchuri@gmail.com>"]
6
6
  license = "Apache-2.0"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes