Skip to content

Commit

Permalink
trim rows
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Aug 13, 2024
1 parent d70e898 commit bd1acdf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ data FoundRowCandidate e = FoundRowCandidate
data ParticipatingEntity e = ParticipatingEntity
{ entity :: e
, searchOffsets :: InspectionOffsets
, candidateRows :: [[Maybe e]]
}
deriving (Functor, Generic, ToJSON)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ allStructureRows =
concatMap transformRows
where
transformRows :: StructureWithGrid b a -> [StructureRow b a]
transformRows g = zipWith (StructureRow g) [0 ..] $ entityGrid g
transformRows g = zipWith (\idx z -> StructureRow g idx $ trimRow z) [0 ..] $ entityGrid g

mkOffsets :: Foldable f => Int32 -> f a -> InspectionOffsets
mkOffsets pos xs =
Expand Down Expand Up @@ -83,7 +83,11 @@ mkEntityLookup grids =

tuples = HM.toList $ HM.mapWithKey mkSmValue groupedByUniqueRow

groupedByUniqueRow = binTuplesHM $ NE.toList $ NE.map (rowContent . myRow &&& id) neList
groupedByUniqueRow =
binTuplesHM $
NE.toList $
NE.map (trimmed . rowContent . myRow &&& id) neList

bounds = sconcat $ NE.map expandedOffsets neList
sm = makeStateMachine tuples

Expand All @@ -96,26 +100,37 @@ mkEntityLookup grids =

deriveEntityOffsets :: PositionWithinRow b a -> InspectionOffsets
deriveEntityOffsets (PositionWithinRow pos r) =
mkOffsets pos $ rowContent r
mkOffsets pos $ fullRow $ rowContent r

-- The members of "rowMembers" are of 'Maybe' type; the 'Nothing's
-- are dropped but accounted for when indexing the columns.
explodeRowEntities ::
(Hashable a, Eq a) =>
StructureRow b a ->
[SingleRowEntityOccurrences b a]
explodeRowEntities r@(StructureRow _ _ rowMembers) =
explodeRowEntities r@(StructureRow _ _ trimmedRow) =
map f $ HM.toList $ binTuplesHM unconsolidated
where
f (e, occurrences) =
SingleRowEntityOccurrences r e occurrences $
sconcat $
NE.map deriveEntityOffsets occurrences

rowMembers = trimmed trimmedRow
unconsolidated =
map swap $
catMaybes $
zipWith (\idx -> fmap (PositionWithinRow idx r,)) [0 ..] rowMembers

trimRow :: SymbolSequence a -> TrimmedRowContent a
trimRow xs =
TrimmedRowContent
{ fullRow = xs
, trimmed = xs
, leftDrops = 0
, rightDrops = 0
}

-- * Util

-- | Place the second element of the tuples into bins by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ entityModified entLoader modification cLoc recognizer =
stateRevision <- case HM.lookup newEntity entLookup of
Nothing -> return oldRecognitionState
Just finder -> do
let msg = FoundParticipatingEntity $ ParticipatingEntity newEntity (finder ^. inspectionOffsets)
let msg =
FoundParticipatingEntity $
ParticipatingEntity
newEntity
(finder ^. inspectionOffsets)
(map fst $ finder ^. searchPairs)
stateRevision' = oldRecognitionState & recognitionLog %~ (msg :)

registerRowMatches entLoader cLoc finder stateRevision'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,17 @@ data StructureRow b a = StructureRow
{ wholeStructure :: StructureWithGrid b a
, rowIndex :: Int32
-- ^ vertical index of the row within the structure
, rowContent :: SymbolSequence a
, rowContent :: TrimmedRowContent a
}

-- | Chops off leading and trailing "transparent" cells
-- (i.e. 'Nothing' cells).
-- Note that interior cells are not trimmed.
data TrimmedRowContent a = TrimmedRowContent
{ fullRow :: SymbolSequence a
, trimmed :: SymbolSequence a
, leftDrops :: Int
, rightDrops :: Int
}

-- | This wrapper facilitates naming the original structure
Expand Down

0 comments on commit bd1acdf

Please sign in to comment.