amalgame/commit

added most/least generic option to most_generic

authorJacco van Ossenbruggen
Tue Aug 26 15:06:49 2014 +0200
committerJacco van Ossenbruggen
Tue Aug 26 15:06:49 2014 +0200
commitaccdaeda480f54af94e0c73bd864689d6b7b5037
tree9c226673e909e1a3867400a5a3fae7ae9b8e1785
parent15d60d836ea0e697aa48ecddf2e12fbd4b2aa26b
Diff style: patch stat
diff --git a/lib/ag_modules/most_generic.pl b/lib/ag_modules/most_generic.pl
index f34cbbd..71f0318 100644
--- a/lib/ag_modules/most_generic.pl
+++ b/lib/ag_modules/most_generic.pl
@@ -1,6 +1,5 @@
 :- module(most_generic,[]).
 
-:- use_module(library(semweb/rdf_db)).
 :- use_module(library(skos/util)).
 :- use_module(library(amalgame/map)).
 
@@ -11,6 +10,9 @@
 parameter(type,
 	  oneof([target, source]), target,
 	  'target = select most general (broader) target for each source, source = select most general source for each target').
+parameter(most_least,
+	  oneof([most, least]), most,
+	  'select most or least generic alternative').
 
 amalgame_module(amalgame:'MostGeneric').
 
@@ -20,17 +22,22 @@ amalgame_module(amalgame:'MostGeneric').
 
 selecter(AlignmentGraph, Sel, Disc, Und, Options) :-
 	option(type(SourceOrTarget), Options, target),
+	option(most_least(Most), Options, most),
 	(   SourceOrTarget = target
-	->  partition_(SourceOrTarget, AlignmentGraph, Sel, Disc, Und)
+	->  partition_(SourceOrTarget, Most, AlignmentGraph, Sel, Disc, Und)
 	;   predsort(ag_map:compare_align(target), AlignmentGraph, SortedAlignmentGraph),
-	    partition_(SourceOrTarget, SortedAlignmentGraph, Sel0, Disc0, Und0),
+	    partition_(SourceOrTarget, Most, SortedAlignmentGraph, Sel0, Disc0, Und0),
 	    predsort(ag_map:compare_align(source), Sel0,  Sel),
 	    predsort(ag_map:compare_align(source), Disc0, Disc),
 	    predsort(ag_map:compare_align(source), Und0,  Und)
 	).
 
-ap(Type, Result, D, align(S,T,P), align(S,T,Pnew)) :-
-	append([[method(most_generic),
+method_name(most, most_generic).
+method_name(least, least_generic).
+
+ap(Type, Result, Most, D, align(S,T,P), align(S,T,Pnew)) :-
+	method_name(Most, Method),
+	append([[method(Method),
 		 score([ result(Result),
 			 type(Type),
 			 nr_ambiguous_correspondences(D)
@@ -39,68 +46,87 @@ ap(Type, Result, D, align(S,T,P), align(S,T,Pnew)) :-
 	       P,
 	       Pnew).
 
-partition_(_, [], [], [], []).
-partition_(target, [A|As], Sel, Dis, Und) :-
+partition_(_, _, [], [], [], []).
+partition_(target, Most, [A|As], Sel, Dis, Und) :-
 	A = align(S,_,_),
 	same_source(As, S, Same, Rest),
 	length(Same, L), D is L +1,
-	(   hierarchy_related(Same, D, target, A, Parent, Dis0)
+	(   hierarchy_related(Same, D, target, Most, A, Parent, Dis0)
 	->  Sel = [Parent|SelRest],
 	    append(Dis0, DisRest, Dis),
 	    Und = UndRest
 	;   Undecided = [A|Same],
-	    maplist(ap(target, undecided, D), Undecided, UndecidedP),
+	    maplist(ap(target, undecided, Most, D), Undecided, UndecidedP),
 	    append(UndecidedP, UndRest, Und),
 	    Sel = SelRest,
 	    Dis = DisRest
 	),
-	partition_(target, Rest, SelRest, DisRest, UndRest).
-partition_(source, [A|As], Sel, Dis, Und) :-
+	partition_(target, Most, Rest, SelRest, DisRest, UndRest).
+partition_(source, Most, [A|As], Sel, Dis, Und) :-
 	A = align(_,T,_),
 	same_target(As, T, Same, Rest),
 	length(Same, L), D is L + 1,
-	(   hierarchy_related(Same, D, source, A, Parent, Dis0)
+	(   hierarchy_related(Same, D, source, Most, A, Parent, Dis0)
 	->  Sel = [Parent|SelRest],
 	    append(Dis0, DisRest, Dis),
 	    Und = UndRest
 	;   Undecided = [A|Same],
-	    maplist(ap(source, undecided, D), Undecided, UndecidedP),
+	    maplist(ap(source, undecided, Most, D), Undecided, UndecidedP),
 	    append(UndecidedP, UndRest, Und),
 	    Sel = SelRest,
 	    Dis = DisRest
 	),
-	partition_(source, Rest, SelRest, DisRest, UndRest).
+	partition_(source, Most, Rest, SelRest, DisRest, UndRest).
 
-hierarchy_related([], Type, L, align(S,T,P), align(S,T,Pnew), []) :-
+hierarchy_related([], L, Type, Most, align(S,T,P), align(S,T,Pnew), []) :-
 	append([[method(most_generic),
 		    score([ result(selected),
 			    type(Type),
+			    most(Most),
 			    nr_ambiguous_correspondences(L)
 			  ])
 		]],P, Pnew).
 
-hierarchy_related([A|As], L, target, G0, G, [A2|Rest]) :-
+hierarchy_related([A|As], L, target, Most, G0, G, [A2|Rest]) :-
 	A = align(_,T,_),
 	G0 = align(_,T0,_),
 	(   skos_descendant_of(T0, T)
-	->  G1 = G0,
-	    A1 = A
+	->  (Most == most
+	    ->	G1 = G0,
+		A1 = A
+	    ;	G1 = A,
+		A1 = G0
+	    )
 	;   skos_descendant_of(T, T0)
-	->  G1 = A,
-	    A1 = G0
+	->  (Most == most
+	    ->	G1 = A,
+		A1 = G0
+	    ;	G1 = G0,
+		A1 = A
+	    )
 	),
-	ap(target, discarded, L, A1, A2),
-	hierarchy_related(As, L, target, G1, G, Rest).
+	ap(target, discarded, Most, L, A1, A2),
+	hierarchy_related(As, L, target, Most, G1, G, Rest).
 
-hierarchy_related([A|As], L, source, G0, G, [A2|Rest]) :-
+hierarchy_related([A|As], L, source, Most, G0, G, [A2|Rest]) :-
 	A = align(S,_,_),
 	G0 = align(S0,_,_),
 	(   skos_descendant_of(S0, S)
-	->  G1 = G0,
-	    A1 = A
+	->  ( Most == most
+	    ->	G1 = G0,
+		A1 = A
+	    ;	G1 = A,
+		A1 = G0
+	    )
 	;   skos_descendant_of(S, S0)
-	->  G1 = A,
-	    A1 = G0
+	->  (Most == most
+	    ->	G1 = A,
+		A1 = G0
+	    ;	G1 = G0,
+		A1 = A
+	    )
 	),
-	ap(target, discarded, L, A1, A2),
-	hierarchy_related(As, L, source, G1, G, Rest).
+	ap(target, discarded, Most, L, A1, A2),
+	hierarchy_related(As, L, source, Most, G1, G, Rest).
+
+