Verified Commit d1242151 authored by Camil Staps's avatar Camil Staps 🙂

Resolve clean-cloogle/cloogle.org#188 more generally: don't return results...

Resolve clean-cloogle/cloogle.org#188 more generally: don't return results 'contained' by other results
parent 3e28b4b9
......@@ -64,9 +64,9 @@ from Cloogle.API import :: FunctionKind, :: SyntaxExample,
* Annotations to store during search.
*/
:: AnnotationKey
= MatchingNGrams //* For name search, the number of matching ngrams
| UnifierSize //* For type search, the 'size' of the unifier
| ExactResult //* 1 if this was an exact match found with filterExactName
= NGramDistance //* For name search, the distance based on the number of matching ngrams
| UnifierSize //* For type search, the 'size' of the unifier
| ExactResult //* 1 if this was an exact match found with filterExactName
instance == AnnotationKey
instance < AnnotationKey
......@@ -113,6 +113,7 @@ location :: !Library !String !FilePath !LineNr !LineNr !Name -> Location
// Using TypeVar causes import clashes in CloogleDBFactory
, fe_representation :: !Maybe String //* A string representation of the entry
, fe_documentation :: !Maybe FunctionDoc //* Documentation on this entry
, fe_typedef :: !Maybe Index //* The TypeDefEntry for constructors/record fields
, fe_class :: !Maybe Index //* The class, for class members
, fe_derivations :: !Maybe [Index] //* The DerivaionEntries
, fe_usages :: ![Index] //* FunctionEntries where the implementation uses this function
......@@ -284,4 +285,11 @@ alwaysUniquePredicate :: !*CloogleDB -> *(String -> Bool, *CloogleDB)
getInstances :: !Name !*CloogleDB -> *([InstanceEntry], *CloogleDB)
getDerivations :: !Name !*CloogleDB -> *([DeriveEntry], *CloogleDB)
/**
* E.g., if there is some constructor in the result list but also its
* corresponding type definition, remove the constructor and update the type
* definition's distance if needed.
*/
removeContainedEntries :: !*CloogleDB -> *CloogleDB
getEntries :: !*CloogleDB -> *([(CloogleEntry, Map AnnotationKey Int)], *CloogleDB)
......@@ -54,7 +54,7 @@ JSONDecode{|()|} _ l = (Nothing, l)
instance == AnnotationKey
where
== MatchingNGrams MatchingNGrams = True
== NGramDistance NGramDistance = True
== UnifierSize UnifierSize = True
== _ _ = False
......@@ -101,6 +101,7 @@ where
, fe_generic_vars = Nothing
, fe_representation = Nothing
, fe_documentation = Nothing
, fe_typedef = Nothing
, fe_class = Nothing
, fe_derivations = Nothing
, fe_usages = []
......@@ -391,12 +392,18 @@ where
filterName :: !String !*CloogleDB -> *CloogleDB
filterName s wrap=:{db,name_ngrams,syntax,abc_instrs}
# (indices,db) = mapSt (uncurry getIndexWithDistance) ('NGrams'.search s name_ngrams) db
# db = 'DB'.searchIndices Intersect indices db
# db = 'DB'.searchWithIndices syntaxSearch syntax db
# db = 'DB'.searchWithIndices abcSearch abc_instrs db
= {wrap & db=db}
where
indices = [(i,[(MatchingNGrams,n)]) \\ (i,n) <- 'NGrams'.search s name_ngrams]
getIndexWithDistance :: !Index !Int !*(NativeDB CloogleEntry AnnotationKey Int)
-> *(!(!Index, ![(AnnotationKey,Int)]), !*NativeDB CloogleEntry AnnotationKey Int)
getIndexWithDistance idx n db
# (e,db) = 'DB'.getIndex idx db
# name = getName $ fromJust $ getLocation e.value
= ((idx, [(NGramDistance,0 - toInt (100.0 * toReal n ^ 2.0 / toReal (size name)))]), db)
cs = [c \\ c <-: s]
......@@ -512,6 +519,41 @@ getDerivations c wrap=:{db,derive_map}
where
idxs = get c derive_map
removeContainedEntries :: !*CloogleDB -> *CloogleDB
removeContainedEntries wrap=:{db}
# (es,db) = 'DB'.getEntriesWithIndices db
= {wrap & db=foldr remove db es}
where
remove :: !(Index, CloogleEntry, Map AnnotationKey Int) !*(NativeDB CloogleEntry AnnotationKey Int)
-> *NativeDB CloogleEntry AnnotationKey Int
remove (idx,e,annots`) db = case e of
FunctionEntry {fe_typedef=Just tdi}
# (tde,db) = 'DB'.getIndex tdi db
| not tde.included = db
# newannots = toList (updateAnnots annots tde.annotations)
= 'DB'.searchIndices AddExcluded [(tdi,newannots)] $ 'DB'.unsearchIndices [idx] db
FunctionEntry {fe_class=Just ci}
# (ce,db) = 'DB'.getIndex ci db
| not ce.included = db
# newannots = toList (updateAnnots annots ce.annotations)
= 'DB'.searchIndices AddExcluded [(ci,newannots)] $ 'DB'.unsearchIndices [idx] db
_ = db
where
annots = toList annots`
updateAnnots :: [(AnnotationKey,Int)] !(Map AnnotationKey Int) -> Map AnnotationKey Int
updateAnnots [] m = m
updateAnnots [(NGramDistance,n):as] m = updateAnnots as $
alter (\oldn -> case oldn of
Nothing -> Just n
Just n` -> Just (min n n`)) NGramDistance m
updateAnnots [(UnifierSize,s):as] m = updateAnnots as $
alter (\olds -> case olds of
Nothing -> Just s
Just s` -> Just (min s s`)) UnifierSize m
updateAnnots [(ExactResult,e):as] m = updateAnnots as $
alter (Just o fromMaybe e o fmap ((+) e)) ExactResult m
getEntries :: !*CloogleDB -> *([(CloogleEntry, Map AnnotationKey Int)], *CloogleDB)
getEntries wrap=:{db}
# (es,db) = 'DB'.getEntries db
......
......@@ -267,14 +267,19 @@ where
[[(i,idx) \\ i <- 'S'.toList imps] \\ idx <- midxs & (_, imps) <- tdb.temp_modules]
where midxs = [idx \\ (idx,ModuleEntry _) <- entridxs]
function_entries = flatten tdb.temp_functions ++ [(
{ fun
function_entries = flatten tdb.temp_functions ++
[({ fun
& fe_kind=case fun.fe_kind of Function -> ClassMember; Macro -> ClassMacro
, fe_loc='CDB'.setName fname cls.ce_loc
, fe_class=Just $ idxhd \ce -> case ce of
ClassEntry ce -> ce.ce_loc == cls.ce_loc
_ -> False
}, ids) \\ clss <- tdb.temp_classes, (cls,funs) <- clss, (fname,fun,ids) <- funs]
}, ids) \\ clss <- tdb.temp_classes, (cls,funs) <- clss, (fname,fun,ids) <- funs] ++
[({ f
& fe_typedef=Just $ idxhd \tde -> case tde of
TypeDefEntry tde -> tde.tde_loc == td.tde_loc
_ -> False
}, 'S'.newSet) \\ tds <- tdb.temp_types, td <- tds, f <- constructor_functions td ++ record_functions td]
entries = [e \\ Right e <- entries`]
entries` = map Right (
......@@ -426,7 +431,6 @@ indexModule include_locals root mod db w
{ db
& temp_functions =
[ [({f & fe_loc=castLoc modname loc},idents) \\ (loc,f,idents) <- functions ++ macros ++ generics]
, [(f, 'S'.newSet) \\ td <- typedefs, f <- constructor_functions td ++ record_functions td]
: db.temp_functions
]
, temp_classes = [[({ce & ce_loc=castLoc modname loc}, fs) \\ (loc,ce,fs) <- clss]:db.temp_classes]
......
......@@ -85,6 +85,7 @@ search {unify,name,className,typeName,using,modules,libraries,page,include_built
# strat = addStrategy (SSUsing <$> using) strat
// Search and return results
# cdb = searchStrategy strat cdb
# cdb = removeContainedEntries cdb
# (es,cdb) = getEntries cdb
# (es,cdb) = mapSt (makeResult mbType allsyns usedSynonyms) es cdb
= (sort $ catMaybes es, cdb)
......@@ -300,14 +301,9 @@ where
Just (Builtin _ d) -> Just d
_ -> Nothing
}
distance = sum
[ case 'M'.get MatchingNGrams annots of
Nothing -> 0
Just n -> 0 - toInt (100.0 * toReal n ^ 2.0 / toReal (size name))
, case 'M'.get ExactResult annots of
Just 1 -> -1
_ -> 0
]
distance =
fromMaybe 0 ('M'.get NGramDistance annots) -
1000 * fromMaybe 0 ('M'.get ExactResult annots)
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