SAPMLTextObservationTopology
public class SAPMLTextObservationTopology
When you receive observations in observationHandler
, it can be tedious to filter out observations of interest. This class orders the array of SAPMLTextObservation
into a grid topology that makes it easier for users to traverse and search observations in observationHandler
. SAPMLTextObservationTopology
also helps in finding observations that are nearby for a given SAPMLTextObservation
.
## Example Initialization and Configuration
recognitionView.observationHandler = { observations
let topology = SAPMLTextObservationTopology(observations)
// Rows, Columns, and Blocks of these observations can be accessed by relevant instance variables
return true;
}
## Setting Custom blockCreationDistance While Creating a Topology This parameter in init helps in getting blocks that the user desires. blockCreationDistance indicates how far two observations can be from each other and still be in same block. The default value is 0.01. The larger the value, the more observations that are far away will be considered to be in the same block.
let topology = SAPMLTextObservationTopology(observations, blockCreationDistance: 0.1)
## Accessing Adjacent Observations
previousObservationInLine
, nextObservationInLine
, previousObservationInColumn
, and nextObservationInColumn
methods of SAPMLTextObservationTopology
help a user get the adjacent observation of a given SAPMLTextObservation
.
For example, to get the last name observation that is known to be in the same line as the first name observation and last name is to the right of the first name. Do the following to get the last name observation from the first name observation.
let lastNameObservation = topology.nextObservationInLine(firstNameObservation)
-
Observations which are ordered in grid topology
Declaration
Swift
open var observations: [SAPMLTextObservation] { get }
-
Each element in
rows
is an array of observations in a single line ordered left to right. rows[0] is the top most line, rows[1] after that and so on.Declaration
Swift
open var rows: [SAPMLTextRowObservation] { get }
-
Each element in
columns
is an array of observations in a single column ordered top to bottom. columns[0] is the left most column, columns[1] after that and so on.Declaration
Swift
open var columns: [SAPMLTextColumnObservation] { get }
-
Each element in
blocks
is aSAPMLTextRowObservation
in a block of text or paragraph.Declaration
Swift
open var blocks: [SAPMLTextBlockObservation] { get }
-
Creates topology out of provided observations and returns
SAPMLTextObservationTopology
objectDeclaration
Swift
public init(_ observations: [SAPMLTextObservation], blockCreationDistance eps: Double = 0.01)
-
Returns observation on the right side from the same line or from the next line if observation is last in the line. Returns nil if observation is rightmost in bottommost line
Declaration
Swift
public func nextObservationInLine(_ observation: SAPMLTextObservation) -> SAPMLTextObservation?
Parameters
observation
observation whose right neighbour is computed
-
Returns observation on the left side from the same line or from the previous line if observation is first in the line. Returns nil if observation is leftmost in topmost line.
Declaration
Swift
public func previousObservationInLine(_ observation: SAPMLTextObservation) -> SAPMLTextObservation?
Parameters
observation
observation whose left neighbour is computed
-
Returns observation which is just below the given observation from the same column or from the next column if observation is last in the column. Returns nil if observation is bottommost in rightmost column
Declaration
Swift
public func nextObservationInColumn(_ observation: SAPMLTextObservation) -> SAPMLTextObservation?
Parameters
observation
observation whose down neighbour is computed
-
Returns observation which is just above the given observation from the same column or from the previous column if observation is first in the column. Returns nil if observation is topmost in leftmost column.
Declaration
Swift
public func previousObservationInColumn(_ observation: SAPMLTextObservation) -> SAPMLTextObservation?
Parameters
observation
observation whose upper neighbour is computed