summaryrefslogtreecommitdiff
path: root/app/Util.hs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--app/Util.hs13
1 files changed, 9 insertions, 4 deletions
diff --git a/app/Util.hs b/app/Util.hs
index eefff84..ee8a5e6 100644
--- a/app/Util.hs
+++ b/app/Util.hs
@@ -23,9 +23,10 @@ import qualified Data.Vector as V
import Text.FuzzyFind (Alignment (score), bestMatch)
-data MatchResult a b
+data MatchResult a b c
= Exact a
| Fuzzy b
+ | Ambiguous [c]
| None
deriving Show
@@ -41,18 +42,22 @@ data DoubleMap code long = DoubleMap
, back :: Map long code
}
-findStationName :: T.Text -> FuzzySet -> MatchResult (Double, Text) (Double, Text)
+findStationName :: T.Text -> FuzzySet -> MatchResult (Double, Text) (Double, Text) (Double, Text)
findStationName query set = case sorted of
[exact] -> Exact exact
_ -> case maybeHbf of
station:_ -> Fuzzy station
_ -> case results of
- station:_ -> Fuzzy station
- _ -> None
+ [] -> None
+ [station] -> Fuzzy station
+ s1:s2:_
+ | fst s1 - fst s2 < 10 -> Ambiguous $ (takeWhile ((> fst s1 - 10) . fst) results)
+ | otherwise -> Fuzzy s1
where
sorted = results
& fmap (\(_, match) -> (fromIntegral . maybe 0 score . bestMatch (T.unpack query) $ T.unpack match, match))
& sortOn (Down . fst)
+ -- check if difference between first two is <10 or such
results = find query set
maybeHbf = filter (T.isInfixOf "Hbf" . snd) sorted