+let fwd_simpl ~dbd t =
+ let map inners row =
+ match row.(0), row.(1), row.(2) with
+ | Some source, Some inner, Some index ->
+ source,
+ List.mem
+ (int_of_string index, (UriManager.uri_of_string inner)) inners
+ | _ -> "", false
+ in
+ let rec rank ranks (source, ok) =
+ match ranks, ok with
+ | [], false -> [source, 0]
+ | [], true -> [source, 1]
+ | (uri, i) :: tl, false when uri = source -> (uri, 0) :: tl
+ | (uri, 0) :: tl, true when uri = source -> (uri, 0) :: tl
+ | (uri, i) :: tl, true when uri = source -> (uri, succ i) :: tl
+ | hd :: tl, _ -> hd :: rank tl (source, ok)
+ in
+ let compare (_, x) (_, y) = compare x y in
+ let filter n (uri, rank) =
+ if rank > 0 then Some (UriManager.uri_of_string uri) else None
+ in
+ match get_metadata t with
+ | None -> []
+ | Some (outer, inners) ->
+ let select = "source, h_inner, h_index" in
+ let from = "genLemma" in
+ let where =
+ Printf.sprintf "h_outer = \"%s\""
+ (Mysql.escape (UriManager.string_of_uri outer)) in
+ let query = Printf.sprintf "SELECT %s FROM %s WHERE %s" select from where in
+ let result = Mysql.exec dbd query in
+ let lemmas = Mysql.map result ~f:(map inners) in
+ let ranked = List.fold_left rank [] lemmas in
+ let ordered = List.rev (List.fast_sort compare ranked) in
+ map_filter filter 0 ordered