On 4/8/07, James Kim <[EMAIL PROTECTED]> wrote:
> In the O'reilly Ferret short cuts, I found very useful example for me.
> It explains how to make custom Tokenizer.
> But that book doesn't explain how to make custom Filter.
> (especially, how to implement the #text=() method)
>
> I'm a newbee and I don't understand how do I create my own custom
> Filter.
> Are there some good source code examples??
Hi James,
Thanks for buying the Ferret ShortCut. I'm assuming you're talking
about implementing a custom TokenFilter and not a custom Filter (which
filters search results and can be implemented itself in two different
ways). Here is an example of a custom TokenFilter which reverses
tokens (obviously just a toy example);
class MyReverseTokenFilter < TokenStream
def initialize(token_stream)
@token_stream = token_stream
end
def text=(text)
@token_stream.text = text
end
def next()
if token = @token_stream.next
token.text = token.text.reverse
end
token
end
end
Notice that I did;
token.text = token.text.reverse
And not;
token.text.reverse!
You can't change the string in place as the text and text= methods are
fetching and setting a string inside a C struct. Obviously the same
goes for sub!, downcase!, lstrip! etc.
Let me know if you need any more help with this.
Cheers,
Dave
--
Dave Balmain
http://www.davebalmain.com/
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk