<commit_before>:- module(drugs,
	  []).

:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_parameters)).
:- use_module(library(semweb/rdf_db)).
:- use_module(library(http/http_json)).
:- use_module(library(semweb/rdf_litindex)).
:- use_module(library(semweb/rdf_label)).

:- http_handler(cliopatria(aers/api/drug/mentions), http_drug_mentions, []).
:- http_handler(cliopatria(aers/api/drug/brands), http_drug_brands, []).
:- http_handler(cliopatria(aers/api/drug/synonyms), http_drug_synonyms, []).
:- http_handler(cliopatria(aers/api/drug/brand/mentions), http_drug_brand_mentions, []).
:- http_handler(cliopatria(aers/api/drug/synonym/mentions), http_drug_synonym_mentions, []).

%%	http_drug_mentions(+Request)

http_drug_mentions(Request) :-
	http_parameters(Request,
			[q(Q, []),
			 method(Method,
			       [default(word),
				one_of([exact,word,stem,sounds,corrected])
			       ])
			]),
	drug_mentions(Q, Method, Mentions),
	maplist(mention_json, Mentions, JSON),
	reply_json(JSON).

drug_mentions(Q, Method, Mentions) :-
	findall(Count-M,
		(   setof(R, drug_mention(Method, Q, M, R), Rs),
		    length(Rs, Count)
		),
		Mentions0),
	keysort(Mentions0, Mentions1),
	reverse(Mentions1, Mentions).

drug_mention(exact, Q, Literal, Report) :-
	rdf(DrugUse, aers:drugname, literal(exact(Q), Literal)),
	rdf(Report, aers:drug, DrugUse).
drug_mention(Method, Q, Lit, Report) :-
	(   Method = word
	->  Query = case(Q)
	;   Method = stem
	->  Query = stem(Q)
	;   Method = sounds
	->  Query = sounds(Q)
	;   Method = corrected
	->  Query = case(Q)
	),
	!,
	rdf_find_literals(Query, Literals),
	member(Lit, Literals),
	(   Method = corrected
	->  rdf(DrugUse, aers:drugname_corrected, literal(Lit))
	;   rdf(DrugUse, aers:drugname, literal(Lit))
	),
	rdf(Report, aers:drug, DrugUse).

drug_list_mentions([], _, []).
drug_list_mentions([Q|Qs], Method, [Q-Mentions|Rest]) :-
	drug_mentions(Q, Method, Mentions),
	drug_list_mentions(Qs, Method, Rest).



%%	http_drug_brands(+Request)

http_drug_brands(Request) :-
	http_parameters(Request,
			[q(Q, []),
			 source(Source,
				[oneof([drugbank,orangebook]),
				 default(drugbank)<commit_msg>FIX return drugname with spelling errors<commit_after>:- module(drugs,
	  []).

:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_parameters)).
:- use_module(library(semweb/rdf_db)).
:- use_module(library(http/http_json)).
:- use_module(library(semweb/rdf_litindex)).
:- use_module(library(semweb/rdf_label)).

:- http_handler(cliopatria(aers/api/drug/mentions), http_drug_mentions, []).
:- http_handler(cliopatria(aers/api/drug/brands), http_drug_brands, []).
:- http_handler(cliopatria(aers/api/drug/synonyms), http_drug_synonyms, []).
:- http_handler(cliopatria(aers/api/drug/brand/mentions), http_drug_brand_mentions, []).
:- http_handler(cliopatria(aers/api/drug/synonym/mentions), http_drug_synonym_mentions, []).

%%	http_drug_mentions(+Request)

http_drug_mentions(Request) :-
	http_parameters(Request,
			[q(Q, []),
			 method(Method,
			       [default(word),
				one_of([exact,word,stem,sounds,corrected])
			       ])
			]),
	drug_mentions(Q, Method, Mentions),
	maplist(mention_json, Mentions, JSON),
	reply_json(JSON).

drug_mentions(Q, Method, Mentions) :-
	findall(Count-M,
		(   setof(R, drug_mention(Method, Q, M, R), Rs),
		    length(Rs, Count)
		),
		Mentions0),
	keysort(Mentions0, Mentions1),
	reverse(Mentions1, Mentions).

drug_mention(exact, Q, Literal, Report) :-
	rdf(DrugUse, aers:drugname, literal(exact(Q), Literal)),
	rdf(Report, aers:drug, DrugUse).
drug_mention(Method, Q, Lit, Report) :-
	(   Method = word
	->  Query = case(Q)
	;   Method = stem
	->  Query = stem(Q)
	;   Method = sounds
	->  Query = sounds(Q)
	;   Method = corrected
	->  Query = case(Q)
	),
	!,
	rdf_find_literals(Query, Literals),
	member(L, Literals),
	(   Method = corrected
	->  rdf(DrugUse, aers:drugname_corrected, literal(L)),
	    rdf(DrugUse, aers:drugname, literal(Lit))
	;   rdf(DrugUse, aers:drugname, literal(L)),
	    Lit = L
	),
	rdf(Report, aers:drug, DrugUse).

drug_list_mentions([], _, []).
drug_list_mentions([Q|Qs], Method, [Q-Mentions|Rest]) :-
	drug_mentions(Q, Method, Mentions),
	drug_list_mentions(Qs, Method, Rest).



%%	http_drug_brands(+Request)

http_drug_brands(Request) :-
	http_parameters(Request,
			[q(Q, []),
			 source(Source,
				[oneof([drugbank,orangebook]),
				 default(drugbank)
{'fraction_non_alphanumeric': 0.17182907528826588, 'fraction_numerical': 0.0018087271082975356, 'mean_word_length': 5.010869565217392, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '594925', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1729, 'n_words': 233}