Skip to content

Commit

Permalink
extract and display matching text snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
beerriot committed Nov 3, 2010
1 parent 0d17ac3 commit b9be0e0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
47 changes: 42 additions & 5 deletions apps/wriaki/src/wiki_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,43 @@ render_404(RD, Ctx) ->

-define(SEARCH_FUN,
iolist_to_binary(
[<<"function(v) {\n">>,
<<"return [v.key];\n">>,
[<<"function(v, d) {\n">>,

%% always want the key in the result
<<"var result = {key: v.key};\n">>,

%% if the object was found, and we have word position info
<<"if (v.values && v.values[0] && d.p) {\n">>,

%% sort positions
<<"d.p.sort();\n">>,

%% create a list of start/end position pairs that attempt
%% to group "close" positions (within 5 words) together
<<"result.ranges=[{start: d.p[0], end: d.p[0]}];\n">>,
<<"for (var i = 1; i < d.p.length; i++) {\n">>,
<<"if (result.ranges[result.ranges.length-1].end-d.p[i] < 5)\n">>,
<<"result.ranges[result.ranges.length-1].end = d.p[i];\n">>,
<<"else\n">>,
<<"result.ranges[result.ranges.length] = {start: d.p[i], end: d.p[i]};\n">>,
<<"}\n">>,

%% extract the phrases from the text
<<"var text = JSON.parse(v.values[0].data).text;\n">>,
<<"var words = text.match(/[a-z0-9\u80-\uff]*/g).filter(function(s) { return s != \"\"; }).length\n">>,

<<"for (var i = 0; i < result.ranges.length; i++) {\n">>,
<<"var s = result.ranges[i].start < 5 ? 0 : result.ranges[i].start-5;\n">>,
<<"var e = result.ranges[i].end+5 > words ? words : result.ranges[i].end+5;\n">>,
%% regexp is basically "match START words, then grab everything
%% until WORDS-END words from the end"
<<"var match = (new RegExp(\"(?:[a-z0-9\u80-\uff]+[^a-z0-9\u80-\uff]+){\"+s+\"}(.*)[^a-z0-9\u80-\uff](?:[a-z0-9\u80-\uff]+[^a-z0-9\u80-\uff]+){\"+(words-e)+\"}\")).exec(text);\n">>,
<<"result.ranges[i] = match[1];\n">>,
<<"}\n">>,

<<"}\n">>, % end of if values & positions

<<"return [result];\n">>,
<<"}">>])).

search(Client, RawSearch) ->
Expand All @@ -269,9 +304,11 @@ search(Client, RawSearch) ->
[<<"article">>, iolist_to_binary(Search)]},
[{map, {jsanon, ?SEARCH_FUN}, <<>>, true}]),
case RawResults of
[{0, RawKeys}] ->
[ [{title, base64url:decode(R)}]
|| R <- RawKeys ];
[{0, Results}] ->
[ [{title, base64url:decode(
proplists:get_value(<<"key">>, R))},
{ranges, proplists:get_value(<<"ranges">>, R)}]
|| {struct, R} <- Results ];
_ ->
[]
end;
Expand Down
7 changes: 7 additions & 0 deletions apps/wriaki/templates/error_404.dtl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
<ul>
{% for result in results %}
<li><a href="/wiki/{{result.title|urlencode}}">{{result.title|escape}}</a></li>
{% if result.ranges %}
<ul>
{% for range in result.ranges %}
<li>&hellip;{{range|escape}}&hellip;</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</ul>
{% endif%}
Expand Down

0 comments on commit b9be0e0

Please sign in to comment.