<?xml version="1.0" encoding="UTF-8"?>
<wiki>
  <body>&lt;p&gt;The ResultFilter acts like an &amp;#8220;interface&amp;#8221; that will allow you to &amp;#8230;well, filter the results. A special method, called valid? will be invoked on each result (if such a filter is given to the KeywordTree).&lt;/p&gt;
&lt;p&gt;Although defined in the extension, this class can be translated to Ruby code:&lt;/p&gt;
&lt;pre&gt;
module AhoCorasick
  class ResultFilter

    # result =&amp;gt; current result, Hash
    # remain =&amp;gt; what's left to be search, String
    def valid?(result, remain)
    end

  end
end
&lt;/pre&gt;
&lt;p&gt;So, filtering the results can be done then by extending this class and overwriting the &lt;code&gt;valid?&lt;/code&gt; implementation.&lt;/p&gt;
&lt;p&gt;At your convenience, the current &lt;code&gt;result&lt;/code&gt; as well as the &lt;code&gt;remain&lt;/code&gt; &amp;#8212;ing string to be searched are passed as arguments to &lt;code&gt;valid?&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;
class FooNotAllowed &amp;lt; AhoCorasick::ResultFilter

  def valid?(result, remain)
    result[:value] != &quot;foo&quot;
  end

end
&lt;/pre&gt;
&lt;p&gt;To attach a filter to the KeywordTree, use &lt;code&gt;filter=&lt;/code&gt; method with your filter instance as argument.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;p&gt;Assuming a valid AhoCorasick::KeywordTree &lt;code&gt;tree&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;
&amp;gt;&amp;gt; tree.filter= FooNotAllowed.new
=&amp;gt; #&amp;lt;FooNotAllowed:0x12d8a4c&amp;gt;
&amp;gt;&amp;gt; tree.add_string &quot;foo&quot;
=&amp;gt; 1
&amp;gt;&amp;gt; tree.add_string &quot;bar&quot;
=&amp;gt; 2
&amp;gt;&amp;gt; tree.add_string &quot;baz&quot;
=&amp;gt; 3
&amp;gt;&amp;gt; results= tree.find_all(&quot;foo is not bar&quot;)
=&amp;gt; [{:ends_at=&amp;gt;14, :starts_at=&amp;gt;11, :value=&amp;gt;&quot;bar&quot;, :id=&amp;gt;2}]
&lt;/pre&gt;</body>
  <created-at type="datetime">2009-11-26T22:18:49-08:00</created-at>
  <id type="integer">90636</id>
  <permalink>resultfilter</permalink>
  <repository-id type="integer">56167</repository-id>
  <title>ResultFilter</title>
  <updated-at type="datetime">2008-12-04T12:52:59-08:00</updated-at>
  <user-id type="integer">350</user-id>
</wiki>
