Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't bother with fuzzy matching, just try to find all the given words as substrings in the target string (in any order) ranking of results is custom and based on document location, title length (rough approximation of % match), and whether the result is a section or an anchor element Signed-off-by: Alex Suraci <suraci.alex@gmail.com>
- Loading branch information
Showing
7 changed files
with
323 additions
and
714 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module Query exposing (Result, matchWords) | ||
|
||
import Dict exposing (Dict) | ||
import Maybe.Extra as ME | ||
|
||
|
||
type alias Result = | ||
List Match | ||
|
||
|
||
type alias Match = | ||
( Int, Int ) | ||
|
||
|
||
matchWords : String -> String -> Maybe Result | ||
matchWords needle haystack = | ||
let | ||
lns = | ||
String.words (String.toLower needle) | ||
|
||
lh = | ||
String.toLower haystack | ||
|
||
matches = | ||
List.map (wordMatches lh) lns | ||
in | ||
if List.any List.isEmpty matches then | ||
Nothing | ||
|
||
else | ||
matches | ||
|> List.concat | ||
|> List.sortWith largestMatchFirst | ||
|> List.foldl simplifyResult ( [], 0 ) | ||
|> Tuple.first | ||
|> Just | ||
|
||
|
||
wordMatches : String -> String -> Result | ||
wordMatches lowerHaystack lowerNeedle = | ||
let | ||
l = | ||
String.length lowerNeedle | ||
in | ||
String.indexes lowerNeedle lowerHaystack | ||
|> List.map (\i -> ( i, l )) | ||
|
||
|
||
largestMatchFirst : Match -> Match -> Order | ||
largestMatchFirst ( xi, xl ) ( yi, yl ) = | ||
if xi == yi then | ||
compare yl xl | ||
|
||
else | ||
compare xi yi | ||
|
||
|
||
simplifyResult : Match -> ( Result, Int ) -> ( Result, Int ) | ||
simplifyResult ( i, l ) ( ms, o ) = | ||
if i + l <= o then | ||
( ms, o ) | ||
|
||
else if i < o then | ||
( ms ++ [ ( o, l - (o - i) ) ], o + (l - (o - i)) ) | ||
|
||
else | ||
( ms ++ [ ( i, l ) ], i + l ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.