Class: Rpdfium::Util::WordMerger
- Inherits:
-
Object
- Object
- Rpdfium::Util::WordMerger
- Defined in:
- lib/rpdfium/util/word_merger.rb
Overview
Fonde word adiacenti sulla stessa riga in un’unica word con bbox aggregata e text concatenato.
Tre strategie disponibili come metodi separati:
-
‘merge_by_proximity` — fonde tutte le word adiacenti che soddisfano il criterio di vicinanza. Strategia base.
-
‘merge_by_label` — fonde solo word che condividono la stessa “label” (chiave esterna calcolata dal chiamante). Utile per preservare la semantica quando label diverse cadono sulla stessa riga (es. flag in colonne adiacenti).
-
‘merge_unlabeled` — fonde solo word “orfane” (label nil) lasciando intatte quelle con label. Inverso di merge_by_label.
Tutte ritornano una nuova lista di word, con quelle fuse rappresentate come hash ‘{ text:, x0:, x1:, top:, bottom: }`.
Constant Summary collapse
- DEFAULT_X_GAP =
20.0- DEFAULT_Y_TOL =
3.0
Instance Method Summary collapse
-
#initialize(x_gap: DEFAULT_X_GAP, y_tol: DEFAULT_Y_TOL) ⇒ WordMerger
constructor
A new instance of WordMerger.
-
#merge_by_label(words, labels_by_word) ⇒ Object
Fonde solo word con la stessa label.
-
#merge_by_proximity(words) ⇒ Object
Fonde tutte le word adiacenti (stessa riga + gap orizzontale ≤ x_gap).
-
#merge_unlabeled(words, labels_by_word) ⇒ Object
Fonde solo word con label nil (orfane).
Constructor Details
#initialize(x_gap: DEFAULT_X_GAP, y_tol: DEFAULT_Y_TOL) ⇒ WordMerger
Returns a new instance of WordMerger.
35 36 37 38 |
# File 'lib/rpdfium/util/word_merger.rb', line 35 def initialize(x_gap: DEFAULT_X_GAP, y_tol: DEFAULT_Y_TOL) @x_gap = x_gap @y_tol = y_tol end |
Instance Method Details
#merge_by_label(words, labels_by_word) ⇒ Object
Fonde solo word con la stessa label.
48 49 50 51 52 |
# File 'lib/rpdfium/util/word_merger.rb', line 48 def merge_by_label(words, labels_by_word) merge_groups(words) do |a, b| labels_by_word[a] == labels_by_word[b] end end |
#merge_by_proximity(words) ⇒ Object
Fonde tutte le word adiacenti (stessa riga + gap orizzontale ≤ x_gap).
41 42 43 |
# File 'lib/rpdfium/util/word_merger.rb', line 41 def merge_by_proximity(words) merge_groups(words) { |a, b| true } end |
#merge_unlabeled(words, labels_by_word) ⇒ Object
Fonde solo word con label nil (orfane).
55 56 57 58 59 |
# File 'lib/rpdfium/util/word_merger.rb', line 55 def merge_unlabeled(words, labels_by_word) merge_groups(words) do |a, b| labels_by_word[a].nil? && labels_by_word[b].nil? end end |