Skip to content

Commit

Permalink
snippets parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Nov 23, 2008
1 parent 08e22ac commit 7f52a26
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/search_do/backends/hyper_estraier/estraier_pure_extention.rb
Expand Up @@ -3,9 +3,10 @@
module SearchDo::Backends
module HyperEstraier::EstraierPureExtention
def self.included(base)
base.const_get("Node").send(:include, Node)
base.const_get("Condition").send(:include, Condition)
base.const_get("NodeResult").send(:include, NodeResult)
[Node,Condition,NodeResult,ResultDocument].each do |klas|
basic_class_name = klas.to_s.split('::').last
base.const_get(basic_class_name).send(:include, klas)
end
end

module Node
Expand Down Expand Up @@ -40,6 +41,18 @@ def docs; map{|e| e } ; end

def first_doc; get_doc(0); end
end

module ResultDocument
#wacky snippet string parsed into a nice array of lines
#where the found word (here: a search for bob) is marked to be highlighted
#[["hallo my name is ",false],["bob",true],[" butcher",false]]
def snippets
snip = @snippet.sub(/^\.+/,"\n")#first ... can be understood as \n
snip.split("\n").reject{|x|x==''}.map do |part|
part.include?("\t") ? [part.split("\t")[0],true] : [part,false]
end
end
end
end
end

Expand Down
26 changes: 26 additions & 0 deletions spec/backends/result_document_spec.rb
@@ -0,0 +1,26 @@
require File.expand_path("../spec_helper", File.dirname(__FILE__))
require 'search_do/backends/hyper_estraier/estraier_pure_extention'


describe EstraierPure::ResultDocument do
describe :snippet do
before do
#result of a search for "i am so blue"
snip = "...i\ti\n \nam\tam\n \nso\tso\n \nblue\tblue\n test\ni\ti\nng makes me happy\n\n"
@res = EstraierPure::ResultDocument.new('',{},snip,'')
end

it "returns a snippets" do
@res.snippets.should_not be_nil
end

it "transform the snippet to a array of lines" do
@res.snippets.size.should == 10
end

it "highlights the found words" do
# i ' ' am ' ' so ' ' blue ' test' i ng makes...
@res.snippets.map{|x|x[1]}.should == [true,false,true,false,true,false,true, false, true,false]
end
end
end

0 comments on commit 7f52a26

Please sign in to comment.