Commit c60122d6 authored by André Espaze's avatar André Espaze
Browse files

keyword/search: get back to the previous search algorithm (in changeset 83df178a596f)

parent 48381ad36434
......@@ -72,7 +72,7 @@ update msg model =
[]
else
KeywordSelector.select xm (String.split ".") xs |> List.take 20
KeywordSelector.select xm xs |> List.take 20
delete expect url =
Http.request
......
......@@ -3,61 +3,46 @@ module KeywordSelector exposing (countKeywords, select)
import Common exposing (maybe)
type alias SplitFunc =
String -> List String
matchKeywordWithWeight : String -> SplitFunc -> ( String, Int ) -> Maybe Float
matchKeywordWithWeight item splitItem ( key, weight ) =
let
parts =
splitItem <| String.toLower item
partsLen =
List.length parts
addPart a b =
if String.contains key a then
let
prec =
toFloat weight / toFloat (String.length a)
in
Just <| Maybe.withDefault 0 b + prec
else
b
in
List.foldl addPart Nothing parts
|> Maybe.map (\x -> x / toFloat partsLen)
countKeywords : String -> SplitFunc -> List String -> List ( Int, String )
countKeywords keywordsString splitItem items =
countKeywords : String -> List String -> List ( Int, String )
countKeywords keywordsString series =
let
keywords =
String.words keywordsString
|> List.map (\k -> ( String.toLower k, String.length k ))
matchCounter : String -> Maybe Int
matchCounter item =
matchCounter serieName =
let
addKeywordPrec : ( String, Int ) -> Maybe Float -> Maybe Float
addKeywordPrec kw b =
Common.maybe
nameLen =
String.length serieName
searchName =
String.toLower serieName
matchKeyword : ( String, Int ) -> Maybe Int -> Maybe Int
matchKeyword ( key, weight ) b =
let
prec =
toFloat weight / toFloat nameLen
val =
round (1.0e6 + prec * 1.0e5) |> negate
in
if String.contains key searchName then
Just <| maybe val ((+) val) b
else
b
((+) (Maybe.withDefault 0 b) >> Just)
(matchKeywordWithWeight item splitItem kw)
in
List.foldl addKeywordPrec Nothing keywords
|> Maybe.map (\x -> round (1.0e6 * x) |> negate)
List.foldl matchKeyword Nothing keywords
in
List.sort <|
List.foldl
(\x b -> maybe b (\i -> ( i, x ) :: b) (matchCounter x))
[]
items
series
select : String -> SplitFunc -> List String -> List String
select k f =
List.map Tuple.second << countKeywords k f
select : String -> List String -> List String
select k =
List.map Tuple.second << countKeywords k
......@@ -100,7 +100,7 @@ update msg model =
[]
else
KeywordSelector.select xm (String.split ".") xs |> List.take 20
KeywordSelector.select xm xs |> List.take 20
in
case msg of
CatalogReceived (Ok x) ->
......
......@@ -41,59 +41,59 @@ testCountKeywords : Test
testCountKeywords =
let
runCount x =
countKeywords x (String.split ".") serieNames
countKeywords x serieNames
in
describe "test selector"
[ test "obs" <|
\_ ->
runCount "obs"
|> Expect.equal
[ ( -166667, "gas.<unknown>.exports.lng.ktd.obs" )
, ( -166667, "gas.argentina.exports.lng.ktd.obs" )
, ( -166667, "gas.argentina.net_imports.lng.ktd.obs" )
, ( -166667, "gas.australia.exports.lng.ktd.obs" )
, ( -166667, "gas.australia.net_imports.lng.ktd.obs" )
, ( -142857, "gas.united_arab_emirates.net_exports.lng.das_island.mcmd.obs" )
[ ( -1009091, "gas.<unknown>.exports.lng.ktd.obs" )
, ( -1009091, "gas.argentina.exports.lng.ktd.obs" )
, ( -1009091, "gas.australia.exports.lng.ktd.obs" )
, ( -1008108, "gas.argentina.net_imports.lng.ktd.obs" )
, ( -1008108, "gas.australia.net_imports.lng.ktd.obs" )
, ( -1005000, "gas.united_arab_emirates.net_exports.lng.das_island.mcmd.obs" )
]
, test "fcs" <|
\_ ->
runCount "fcs"
|> Expect.equal
[ ( -125000, "gas.uk.prod.bacton_seal.mcmd.fcst" )
, ( -125000, "gas.uk.prod.bacton_shell.mcmd.fcst" )
, ( -125000, "gas.uk.prod.st_fergus_mobil.mcmd.fcst" )
, ( -125000, "gas.uk.prod.st_fergus_shell.mcmd.fcst" )
, ( -107143, "gas.namibia.lng.imports.ktd.monthly.fcst" )
, ( -107143, "gas.nigeria.lng.outages.ktd.monthly.fcst" )
, ( -93750, "gas.nl.imports.lng.sendouts.mcmd.monthly.fcst" )
[ ( -1009091, "gas.uk.prod.bacton_seal.mcmd.fcst" )
, ( -1008824, "gas.uk.prod.bacton_shell.mcmd.fcst" )
, ( -1008108, "gas.uk.prod.st_fergus_mobil.mcmd.fcst" )
, ( -1008108, "gas.uk.prod.st_fergus_shell.mcmd.fcst" )
, ( -1007500, "gas.namibia.lng.imports.ktd.monthly.fcst" )
, ( -1007500, "gas.nigeria.lng.outages.ktd.monthly.fcst" )
, ( -1006667, "gas.nl.imports.lng.sendouts.mcmd.monthly.fcst" )
]
, test "prod fcs" <|
\_ ->
runCount "prod fcs"
|> Expect.equal
[ ( -291667, "gas.uk.prod.bacton_seal.mcmd.fcst" )
, ( -291667, "gas.uk.prod.bacton_shell.mcmd.fcst" )
, ( -291667, "gas.uk.prod.st_fergus_mobil.mcmd.fcst" )
, ( -291667, "gas.uk.prod.st_fergus_shell.mcmd.fcst" )
, ( -107143, "gas.namibia.lng.imports.ktd.monthly.fcst" )
, ( -107143, "gas.nigeria.lng.outages.ktd.monthly.fcst" )
, ( -93750, "gas.nl.imports.lng.sendouts.mcmd.monthly.fcst" )
[ ( -2021212, "gas.uk.prod.bacton_seal.mcmd.fcst" )
, ( -2020589, "gas.uk.prod.bacton_shell.mcmd.fcst" )
, ( -2018919, "gas.uk.prod.st_fergus_mobil.mcmd.fcst" )
, ( -2018919, "gas.uk.prod.st_fergus_shell.mcmd.fcst" )
, ( -1007500, "gas.namibia.lng.imports.ktd.monthly.fcst" )
, ( -1007500, "gas.nigeria.lng.outages.ktd.monthly.fcst" )
, ( -1006667, "gas.nl.imports.lng.sendouts.mcmd.monthly.fcst" )
]
, test "obs ktd arge" <|
\_ ->
runCount "obs ktd arge"
|> Expect.equal
[ ( -407407, "gas.argentina.exports.lng.ktd.obs" )
, ( -407407, "gas.argentina.net_imports.lng.ktd.obs" )
, ( -333333, "gas.<unknown>.exports.lng.ktd.obs" )
, ( -333333, "gas.australia.exports.lng.ktd.obs" )
, ( -333333, "gas.australia.net_imports.lng.ktd.obs" )
, ( -288889, "gas.argentina.exports.lng.ktd" )
, ( -288889, "gas.argentina.net_imports.lng.ktd" )
, ( -166667, "gas.nl.demand.industrial.ktd.full" )
, ( -148148, "gas.argentina.full" )
, ( -142857, "gas.namibia.lng.imports.ktd.monthly.fcst" )
, ( -142857, "gas.nigeria.lng.outages.ktd.monthly.fcst" )
, ( -142857, "gas.united_arab_emirates.net_exports.lng.das_island.mcmd.obs" )
[ ( -3030303, "gas.argentina.exports.lng.ktd.obs" )
, ( -3027027, "gas.argentina.net_imports.lng.ktd.obs" )
, ( -2024138, "gas.argentina.exports.lng.ktd" )
, ( -2021212, "gas.argentina.net_imports.lng.ktd" )
, ( -2018182, "gas.<unknown>.exports.lng.ktd.obs" )
, ( -2018182, "gas.australia.exports.lng.ktd.obs" )
, ( -2016216, "gas.australia.net_imports.lng.ktd.obs" )
, ( -1022222, "gas.argentina.full" )
, ( -1009091, "gas.nl.demand.industrial.ktd.full" )
, ( -1007500, "gas.namibia.lng.imports.ktd.monthly.fcst" )
, ( -1007500, "gas.nigeria.lng.outages.ktd.monthly.fcst" )
, ( -1005000, "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