mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Always return classifier results as suggestion
The classifier results are spliced into the suggestion list at second place. When linking they are only used if nlp didn't find anything.
This commit is contained in:
@ -45,6 +45,19 @@ case class MetaProposalList private (proposals: List[MetaProposal]) {
|
||||
|
||||
def sortByWeights: MetaProposalList =
|
||||
change(_.sortByWeight)
|
||||
|
||||
def insertSecond(ml: MetaProposalList): MetaProposalList =
|
||||
MetaProposalList.flatten0(
|
||||
Seq(this, ml),
|
||||
(map, next) =>
|
||||
map.get(next.proposalType) match {
|
||||
case Some(MetaProposal(mt, values)) =>
|
||||
val cand = NonEmptyList(values.head, next.values.toList ++ values.tail)
|
||||
map.updated(next.proposalType, MetaProposal(mt, MetaProposal.flatten(cand)))
|
||||
case None =>
|
||||
map.updated(next.proposalType, next)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
object MetaProposalList {
|
||||
@ -74,20 +87,25 @@ object MetaProposalList {
|
||||
* is preserved and candidates of proposals are appended as given
|
||||
* by the order of the given `seq'.
|
||||
*/
|
||||
def flatten(ml: Seq[MetaProposalList]): MetaProposalList = {
|
||||
val init: Map[MetaProposalType, MetaProposal] = Map.empty
|
||||
|
||||
def updateMap(
|
||||
map: Map[MetaProposalType, MetaProposal],
|
||||
mp: MetaProposal
|
||||
): Map[MetaProposalType, MetaProposal] =
|
||||
map.get(mp.proposalType) match {
|
||||
case Some(mp0) => map.updated(mp.proposalType, mp0.addIdRef(mp.values.toList))
|
||||
case None => map.updated(mp.proposalType, mp)
|
||||
}
|
||||
|
||||
val merged = ml.foldLeft(init)((map, el) => el.proposals.foldLeft(map)(updateMap))
|
||||
def flatten(ml: Seq[MetaProposalList]): MetaProposalList =
|
||||
flatten0(
|
||||
ml,
|
||||
(map, mp) =>
|
||||
map.get(mp.proposalType) match {
|
||||
case Some(mp0) => map.updated(mp.proposalType, mp0.addIdRef(mp.values.toList))
|
||||
case None => map.updated(mp.proposalType, mp)
|
||||
}
|
||||
)
|
||||
|
||||
private def flatten0(
|
||||
ml: Seq[MetaProposalList],
|
||||
merge: (
|
||||
Map[MetaProposalType, MetaProposal],
|
||||
MetaProposal
|
||||
) => Map[MetaProposalType, MetaProposal]
|
||||
): MetaProposalList = {
|
||||
val init = Map.empty[MetaProposalType, MetaProposal]
|
||||
val merged = ml.foldLeft(init)((map, el) => el.proposals.foldLeft(map)(merge))
|
||||
fromMap(merged)
|
||||
}
|
||||
|
||||
|
@ -68,4 +68,35 @@ object MetaProposalListTest extends SimpleTestSuite {
|
||||
assertEquals(candidates.head, cand1)
|
||||
assertEquals(candidates.tail.head, cand2)
|
||||
}
|
||||
|
||||
test("insert second") {
|
||||
val cand1 = Candidate(IdRef(Ident.unsafe("123"), "name"), Set.empty)
|
||||
val cand2 = Candidate(IdRef(Ident.unsafe("456"), "name"), Set.empty)
|
||||
val cand3 = Candidate(IdRef(Ident.unsafe("789"), "name"), Set.empty)
|
||||
val cand4 = Candidate(IdRef(Ident.unsafe("abc"), "name"), Set.empty)
|
||||
val cand5 = Candidate(IdRef(Ident.unsafe("def"), "name"), Set.empty)
|
||||
|
||||
val mpl1 = MetaProposalList
|
||||
.of(
|
||||
MetaProposal(MetaProposalType.CorrOrg, NonEmptyList.of(cand1, cand2)),
|
||||
MetaProposal(MetaProposalType.ConcPerson, NonEmptyList.of(cand3))
|
||||
)
|
||||
|
||||
val mpl2 = MetaProposalList
|
||||
.of(
|
||||
MetaProposal(MetaProposalType.CorrOrg, NonEmptyList.of(cand4)),
|
||||
MetaProposal(MetaProposalType.ConcPerson, NonEmptyList.of(cand5))
|
||||
)
|
||||
|
||||
val result = mpl1.insertSecond(mpl2)
|
||||
assertEquals(
|
||||
result,
|
||||
MetaProposalList(
|
||||
List(
|
||||
MetaProposal(MetaProposalType.CorrOrg, NonEmptyList.of(cand1, cand4, cand2)),
|
||||
MetaProposal(MetaProposalType.ConcPerson, NonEmptyList.of(cand3, cand5))
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user