Created
January 20, 2014 21:00
-
-
Save AlexMocioi/8529038 to your computer and use it in GitHub Desktop.
Funcția Map normalizează denumirile, scoate diacriticele, le face litere mari și sparge denumirile după spații, linii, puncte, virgule sau paranteze și emite apoi o listă de tokenuri!
Funcția List parcurge răspunsul cu include_docs și întoarce în format aaData (array-of-arrays-of-Data) taman bine formatate pentru DataTables !
Pentru că nu apelea…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%Map | |
fun({Doc}) -> | |
Filtre = [ {"ț","T"}, {"Ț","T"}, {"ș","S"}, {"Ș","S"}, {"ă","A"}, {"â","A"}, {"Ă","A"}, {"Â","A"}, {"Î","I"}, {"î","I"} ], | |
Nume = proplists:get_value(<<"Nume">>, Doc), | |
Cuvinte = string:tokens(binary_to_list(Nume)," (),-.?!'"), | |
lists:foreach(fun((Cuvant)) -> | |
Lung = string:len(Cuvant), | |
if | |
Lung>2 -> | |
Filtrat = lists:foldl(fun({Pattern, Replacement}, Subject) -> | |
re:replace(Subject, Pattern, Replacement, [global, {return, list}]) | |
end , string:to_upper(Cuvant), Filtre), | |
Emit(list_to_binary(Filtrat),1), | |
true; | |
true -> | |
true | |
end | |
end, Cuvinte) | |
end. | |
%%List | |
fun(Head, {Req}) -> | |
Send(<<"[">>), | |
%% asta am lăsat-o să vedeți cum se preia un eventual | |
%% parametru trimis în request, pentru operațiuni | |
%% suplimentare ce se pot executa în list (alte filtre) | |
%% | |
%% Send(proplists:get_value(<<"q">>, Req)), | |
%% | |
Fun = fun({Row}, Virgula) -> | |
Send(Virgula), | |
case proplists:get_value(<<"doc">>, Row) of | |
{Doc} -> | |
Send(<<"[\"">>), | |
Send(proplists:get_value(<<"id">>, Row)), | |
Send(<<"\",\"">>), | |
Send(proplists:get_value(<<"key">>, Row)), | |
Send(<<"\",\"">>), | |
Send(proplists:get_value(<<"Nume">>, Doc)), | |
Send(<<"\",\"">>), | |
Send(proplists:get_value(<<"Siruta">>, Doc)), | |
Send(<<"\",\"">>), | |
Send(proplists:get_value(<<"Județ">>, Doc)), | |
Send(<<"\"]">>), | |
{ok, <<",">>}; | |
_ -> | |
{ok, <<",">>} | |
end | |
end, | |
{ok, _} = FoldRows(Fun, <<"">>), | |
<<"]">> | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment