Commit 4acab467 authored by André Espaze's avatar André Espaze
Browse files

Move from Fuzzy to a keyword counting selection

parent c41329b9b626
......@@ -13,8 +13,7 @@
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"justgage/tachyons-elm": "4.1.3",
"rtfeldman/elm-css": "16.0.1",
"tripokey/elm-fuzzy": "5.2.1"
"rtfeldman/elm-css": "16.0.1"
},
"indirect": {
"Skinney/murmur3": "2.0.8",
......@@ -26,7 +25,11 @@
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
"direct": {
"elm-explorations/test": "1.2.2"
},
"indirect": {
"elm/random": "1.0.0"
}
}
}
\ No newline at end of file
}
module KeywordSelector exposing (countKeywords, select)
import Dict
countKeywords : String -> List String -> List ( Int, String )
countKeywords keywordsString series =
let
keywords =
String.words keywordsString |> List.map String.toLower
matchCounter : Dict.Dict String Int
matchCounter =
let
matchKeyword k serieName b =
if String.contains k <| String.toLower serieName then
Dict.update
serieName
(\maybeCounter ->
Maybe.map (\x -> (-) x 1) maybeCounter
|> Maybe.withDefault 0
|> Just
)
b
else
b
in
List.foldl
(\k b -> List.foldl (matchKeyword k) b series)
Dict.empty
keywords
in
List.map2 Tuple.pair (Dict.values matchCounter) (Dict.keys matchCounter)
|> List.sort
select : String -> List String -> List String
select k =
List.map Tuple.second << countKeywords k
......@@ -14,12 +14,12 @@ module Main exposing (main)
import Browser
import Dict
import Fuzzy exposing (match)
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (class, classList)
import Html.Styled.Events exposing (onInput, onMouseDown)
import Http
import Json.Decode as Decode
import KeywordSelector
import Tachyons.Classes as T
import Time
......@@ -62,12 +62,12 @@ update msg model =
newModel x =
( x, Cmd.none )
fuzzyMatch xm xs =
keywordMatch xm xs =
if String.length xm < 2 then
[]
else
List.sortBy (\x -> match [] [] xm x |> .score) xs |> List.take 20
KeywordSelector.select xm xs |> List.take 20
in
case msg of
CatalogReceived (Ok x) ->
......@@ -87,7 +87,7 @@ update msg model =
newModel { model | searchString = x }
MakeSearch ->
newModel { model | searchedSeries = fuzzyMatch model.searchString model.series }
newModel { model | searchedSeries = keywordMatch model.searchString model.series }
view : Model -> Html Msg
......
module Suite exposing (testCountKeywords)
import Expect
import KeywordSelector exposing (countKeywords)
import Test exposing (..)
serieNames : List String
serieNames =
[ "gas.<unknown>.exports.lng.ktd.obs"
, "gas.price.everyday.ttf.penceth"
, "gas.uk.prod.st_fergus_shell.mcmd.fcst"
, "gas.uk.prod.bacton_shell.mcmd.fcst"
, "gas.uk.prod.st_fergus_mobil.mcmd.fcst"
, "gas.uk.prod.bacton_seal.mcmd.fcst"
, "gas.australia.exports.lng.ktd.obs"
, "27553_M_10"
, "27553_M_0"
, "38755_M_0"
, "27917_M_31"
, "27553_M_12"
, "27454_M_2019-10-01000000"
, "27553_M_1"
, "27917_M_33"
, "38755_D_0"
, "gas.australia.net_imports.lng.ktd.obs"
, "gas.nigeria.lng.outages.ktd.monthly.fcst"
, "gas.nl.imports.lng.sendouts.mcmd.monthly.fcst"
, "gas.namibia.lng.imports.ktd.monthly.fcst"
, "gas.nl.demand.industrial.ktd.full"
, "gas.united_arab_emirates.net_exports.lng.das_island.mcmd.obs"
, "gas.argentina.exports.lng.ktd.obs"
, "gas.argentina.net_imports.lng.ktd.obs"
]
testCountKeywords : Test
testCountKeywords =
let
runCount x =
countKeywords x serieNames
in
describe "test selector"
[ test "obs" <|
\_ ->
runCount "obs"
|> Expect.equal
[ ( 0, "gas.<unknown>.exports.lng.ktd.obs" )
, ( 0, "gas.argentina.exports.lng.ktd.obs" )
, ( 0, "gas.argentina.net_imports.lng.ktd.obs" )
, ( 0, "gas.australia.exports.lng.ktd.obs" )
, ( 0, "gas.australia.net_imports.lng.ktd.obs" )
, ( 0, "gas.united_arab_emirates.net_exports.lng.das_island.mcmd.obs" )
]
, test "fcs" <|
\_ ->
runCount "fcs"
|> Expect.equal
[ ( 0, "gas.namibia.lng.imports.ktd.monthly.fcst" )
, ( 0, "gas.nigeria.lng.outages.ktd.monthly.fcst" )
, ( 0, "gas.nl.imports.lng.sendouts.mcmd.monthly.fcst" )
, ( 0, "gas.uk.prod.bacton_seal.mcmd.fcst" )
, ( 0, "gas.uk.prod.bacton_shell.mcmd.fcst" )
, ( 0, "gas.uk.prod.st_fergus_mobil.mcmd.fcst" )
, ( 0, "gas.uk.prod.st_fergus_shell.mcmd.fcst" )
]
, test "prod fcs" <|
\_ ->
runCount "prod fcst"
|> Expect.equal
[ ( -1, "gas.uk.prod.bacton_seal.mcmd.fcst" )
, ( -1, "gas.uk.prod.bacton_shell.mcmd.fcst" )
, ( -1, "gas.uk.prod.st_fergus_mobil.mcmd.fcst" )
, ( -1, "gas.uk.prod.st_fergus_shell.mcmd.fcst" )
, ( 0, "gas.namibia.lng.imports.ktd.monthly.fcst" )
, ( 0, "gas.nigeria.lng.outages.ktd.monthly.fcst" )
, ( 0, "gas.nl.imports.lng.sendouts.mcmd.monthly.fcst" )
]
, test "obs ktd arg" <|
\_ ->
runCount "obs ktd arg"
|> Expect.equal
[ ( -2, "gas.argentina.exports.lng.ktd.obs" )
, ( -2, "gas.argentina.net_imports.lng.ktd.obs" )
, ( -1, "gas.<unknown>.exports.lng.ktd.obs" )
, ( -1, "gas.australia.exports.lng.ktd.obs" )
, ( -1, "gas.australia.net_imports.lng.ktd.obs" )
, ( 0, "gas.namibia.lng.imports.ktd.monthly.fcst" )
, ( 0, "gas.nigeria.lng.outages.ktd.monthly.fcst" )
, ( 0, "gas.nl.demand.industrial.ktd.full" )
, ( 0, "gas.united_arab_emirates.net_exports.lng.das_island.mcmd.obs" )
]
]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment