class PhraseLookup
Public Class Methods
new(obj=nil, debug: false)
click to toggle source
# File lib/phrase_lookup.rb, line 14 def initialize(obj=nil, debug: false) @debug = debug @master = if obj then if obj.is_a? String then s = RXFHelper.read(obj).first if s.lstrip =~ /^---/ then # it's YAML Master.new( YAML.load(s)) elsif s =~ /: / # it's the contents of a log file m = Master.new m.create s m else # it's plain text m = Master.new m.create_from_txt s m end elsif obj.is_a? Array puts 'array detected' if @debug m = Master.new m.parse_array obj m else Master.new obj end end end
Public Instance Methods
lookup(s, limit: 10, search_tags: false)
click to toggle source
# File lib/phrase_lookup.rb, line 123 def lookup(s, limit: 10, search_tags: false) return [] if s.empty? h = @master.to_h a = h.keys a3 = if search_tags then a.grep /\].*(?:\b#{s}\b|\b#{s}).*\|.*(?:\b#{s}\b|\b#{s})/i else a1, a2 = [/^#{s}/i, /\b#{s}/i].map do |regex| a.select {|x| x.gsub(/\[[^\]]*\]/,'').gsub(/\([^\)]*\)/,'') =~ regex} end (a1 + a2) end return a3.sort_by {|word| -h[word]}.map {|x| x.sub(/ +\|.*$/,'')}\ .uniq.take(limit) end
Also aliased as: q
master()
click to toggle source
# File lib/phrase_lookup.rb, line 146 def master() @master end
save(filename='phrase_lookup.yaml')
click to toggle source
# File lib/phrase_lookup.rb, line 150 def save(filename='phrase_lookup.yaml') @master.save filename end