Verified Commit 48c386aa authored by Camil Staps's avatar Camil Staps 🚀

Add searchWithSuggestions from cloogle.org repository

parent 71d8bb5a
......@@ -30,3 +30,9 @@ DEFAULT_INCLUDE_APPS :== False
* Search for a request in the type database
*/
search :: !Request !*CloogleDB -> *([Result], *CloogleDB)
/**
* Search for a request, and also make suggestions for similar requests with
* better results.
*/
searchWithSuggestions :: !Request !*CloogleDB -> *([Result], [(Request,[Result])], *CloogleDB)
......@@ -306,6 +306,66 @@ where
fromMaybe 0 ('M'.get NGramDistance annots) -
1000 * fromMaybe 0 ('M'.get ExactResult annots)
searchWithSuggestions :: !Request !*CloogleDB -> *([Result], [(Request,[Result])], *CloogleDB)
searchWithSuggestions req db
# (res,db) = search req db
# (suggs,db) = suggestions req res db
= (res,suggs,db)
where
suggestions :: !Request ![Result] !*CloogleDB -> *([(Request, [Result])], *CloogleDB)
suggestions {page=Just n} _ db | n > 0 = ([], db)
suggestions orgreq orgresults db
# (swapped, db) = swap db
# (capitalized, db) = capitalize db
# (withapps, db) = addapps db
= (flatten [swapped, capitalized, withapps], db)
where
orgtype = orgreq.unify >>= parseType o fromString
swap db = case orgtype of
Just (Func is r cc) | length is < 3
-> appFst (filter enough) $ mapSt (\r -> appFst (tuple r) o search r o resetDB) reqs db
with
reqs = [{orgreq & unify=Just $ concat $ print False $ Func is` r cc}
\\ is` <- permutations is | is` <> is]
_ -> ([], db)
where
enough :: (Request, [Result]) -> Bool
enough (_, res) = enough` (length orgresults) res
where
enough` 0 _ = True
enough` _ [] = False
enough` n [_:xs] = enough` (n-1) xs
capitalize db = case t` of
Just t` | fromJust orgtype <> t`
-> appFst (\res -> [(req,res)]) $ search req $ resetDB db
with req = {orgreq & unify=Just $ concat $ print False t`}
_ -> ([], db)
where
t` = assignAll
[ ("int", Type "Int" [])
, ("bool", Type "Bool" [])
, ("char", Type "Char" [])
, ("real", Type "Real" [])
, ("file", Type "File" [])
, ("string", Type "String" [])
, ("dynamic", Type "Dynamic" [])
, ("world", Uniq (Type "World" []))
] =<< orgtype
addapps db
| fromMaybe DEFAULT_INCLUDE_APPS orgreq.include_apps == DEFAULT_INCLUDE_APPS
# req = {orgreq & include_apps=Just (not DEFAULT_INCLUDE_APPS)}
# (res,db) = search req $ resetDB db
| isEmpty res = ([], db)
| isEmpty orgresults = ([(req,res)], db)
# orghddistance = (fromJust (getBasicResult (hd orgresults))).distance
| all (\r -> (fromJust (getBasicResult r)).distance < orghddistance) $ take 3 res
= ([(req,res)], db)
= ([], db)
| otherwise = ([], db)
locResult :: Location -> LocationResult
locResult (Location lib mod filename dcl icl _) = (lib,mod,filename,dcl,icl)
......
Markdown is supported
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