tesseract v5.3.3.20231005
tesseract::ParagraphModelSmearer Class Reference

#include <paragraphs_internal.h>

Public Member Functions

 ParagraphModelSmearer (std::vector< RowScratchRegisters > *rows, int row_start, int row_end, ParagraphTheory *theory)
 
void Smear ()
 

Detailed Description

Definition at line 239 of file paragraphs_internal.h.

Constructor & Destructor Documentation

◆ ParagraphModelSmearer()

tesseract::ParagraphModelSmearer::ParagraphModelSmearer ( std::vector< RowScratchRegisters > *  rows,
int  row_start,
int  row_end,
ParagraphTheory theory 
)

Definition at line 1367 of file paragraphs.cpp.

1369 : theory_(theory), rows_(rows), row_start_(row_start), row_end_(row_end) {
1370 if (!AcceptableRowArgs(0, 0, __func__, rows, row_start, row_end)) {
1371 row_start_ = 0;
1372 row_end_ = 0;
1373 return;
1374 }
1375 open_models_.resize(open_models_.size() + row_end - row_start + 2);
1376}

Member Function Documentation

◆ Smear()

void tesseract::ParagraphModelSmearer::Smear ( )

Definition at line 1411 of file paragraphs.cpp.

1411 {
1412 CalculateOpenModels(row_start_, row_end_);
1413
1414 // For each row which we're unsure about (that is, it is LT_UNKNOWN or
1415 // we have multiple LT_START hypotheses), see if there's a model that
1416 // was recently used (an "open" model) which might model it well.
1417 for (int i = row_start_; i < row_end_; i++) {
1418 RowScratchRegisters &row = (*rows_)[i];
1419 if (row.ri_->num_words == 0) {
1420 continue;
1421 }
1422
1423 // Step One:
1424 // Figure out if there are "open" models which are left-alined or
1425 // right-aligned. This is important for determining whether the
1426 // "first" word in a row would fit at the "end" of the previous row.
1427 bool left_align_open = false;
1428 bool right_align_open = false;
1429 for (auto &m : OpenModels(i)) {
1430 switch (m->justification()) {
1431 case JUSTIFICATION_LEFT:
1432 left_align_open = true;
1433 break;
1435 right_align_open = true;
1436 break;
1437 default:
1438 left_align_open = right_align_open = true;
1439 }
1440 }
1441 // Step Two:
1442 // Use that knowledge to figure out if this row is likely to
1443 // start a paragraph.
1444 bool likely_start;
1445 if (i == 0) {
1446 likely_start = true;
1447 } else {
1448 if ((left_align_open && right_align_open) || (!left_align_open && !right_align_open)) {
1449 likely_start = LikelyParagraphStart((*rows_)[i - 1], row, JUSTIFICATION_LEFT) ||
1450 LikelyParagraphStart((*rows_)[i - 1], row, JUSTIFICATION_RIGHT);
1451 } else if (left_align_open) {
1452 likely_start = LikelyParagraphStart((*rows_)[i - 1], row, JUSTIFICATION_LEFT);
1453 } else {
1454 likely_start = LikelyParagraphStart((*rows_)[i - 1], row, JUSTIFICATION_RIGHT);
1455 }
1456 }
1457
1458 // Step Three:
1459 // If this text line seems like an obvious first line of an
1460 // open model, or an obvious continuation of an existing
1461 // modelled paragraph, mark it up.
1462 if (likely_start) {
1463 // Add Start Hypotheses for all Open models that fit.
1464 for (unsigned m = 0; m < OpenModels(i).size(); m++) {
1465 if (ValidFirstLine(rows_, i, OpenModels(i)[m])) {
1466 row.AddStartLine(OpenModels(i)[m]);
1467 }
1468 }
1469 } else {
1470 // Add relevant body line hypotheses.
1471 SetOfModels last_line_models;
1472 if (i > 0) {
1473 (*rows_)[i - 1].StrongHypotheses(&last_line_models);
1474 } else {
1475 theory_->NonCenteredModels(&last_line_models);
1476 }
1477 for (auto model : last_line_models) {
1478 if (ValidBodyLine(rows_, i, model)) {
1479 row.AddBodyLine(model);
1480 }
1481 }
1482 }
1483
1484 // Step Four:
1485 // If we're still quite unsure about this line, go through all
1486 // models in our theory and see if this row could be the start
1487 // of any of our models.
1488 if (row.GetLineType() == LT_UNKNOWN ||
1489 (row.GetLineType() == LT_START && !row.UniqueStartHypothesis())) {
1490 SetOfModels all_models;
1491 theory_->NonCenteredModels(&all_models);
1492 for (auto &all_model : all_models) {
1493 if (ValidFirstLine(rows_, i, all_model)) {
1494 row.AddStartLine(all_model);
1495 }
1496 }
1497 }
1498 // Step Five:
1499 // Since we may have updated the hypotheses about this row, we need
1500 // to recalculate the Open models for the rest of rows[i + 1, row_end)
1501 if (row.GetLineType() != LT_UNKNOWN) {
1502 CalculateOpenModels(i + 1, row_end_);
1503 }
1504 }
1505}
@ JUSTIFICATION_LEFT
Definition: publictypes.h:248
@ JUSTIFICATION_RIGHT
Definition: publictypes.h:250
std::vector< const ParagraphModel * > SetOfModels
bool ValidBodyLine(const std::vector< RowScratchRegisters > *rows, int row, const ParagraphModel *model)
bool ValidFirstLine(const std::vector< RowScratchRegisters > *rows, int row, const ParagraphModel *model)
void NonCenteredModels(SetOfModels *models)

The documentation for this class was generated from the following files: