Class: Kleene::NaiveOnlineRegex
- Inherits:
-
Object
- Object
- Kleene::NaiveOnlineRegex
- Defined in:
- lib/kleene/naive_online_regex.rb
Instance Method Summary collapse
- #drop_matches_that_have_rolled_off(number_of_chars_at_front_of_buffer_that_rolled_off) ⇒ Object
-
#ingest(input, _debug = false) ⇒ Object
#ingest(input) is the online-style matching interface.
-
#initialize(regexen, window_size = 100) ⇒ NaiveOnlineRegex
constructor
A new instance of NaiveOnlineRegex.
-
#matches ⇒ Object
Hash(Regexp, Set(OnlineMatch)).
-
#matches_for(regex) ⇒ Object
Set(OnlineMatch) | Nil.
- #reset ⇒ Object
- #resize_buffer! ⇒ Object
Constructor Details
#initialize(regexen, window_size = 100) ⇒ NaiveOnlineRegex
Returns a new instance of NaiveOnlineRegex.
6 7 8 9 10 11 |
# File 'lib/kleene/naive_online_regex.rb', line 6 def initialize(regexen, window_size = 100) @regexen = regexen @window_size = window_size reset end |
Instance Method Details
#drop_matches_that_have_rolled_off(number_of_chars_at_front_of_buffer_that_rolled_off) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kleene/naive_online_regex.rb', line 51 def drop_matches_that_have_rolled_off(number_of_chars_at_front_of_buffer_that_rolled_off) @matches_per_regex.transform_values! do |match_set| new_set = Set.new match_set.each do |online_match| online_match_clone = online_match.clone online_match_clone.decrement_offsets(number_of_chars_at_front_of_buffer_that_rolled_off) new_set << online_match_clone if online_match_clone.offsets.first > 0 end new_set end end |
#ingest(input, _debug = false) ⇒ Object
#ingest(input) is the online-style matching interface
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/kleene/naive_online_regex.rb', line 19 def ingest(input, _debug = false) # : Set(OnlineMatch) @buffer << input new_online_matches = Set.new @regexen.each do |regex| existing_matches_for_regex = (@matches_per_regex[regex] ||= Set.new) scan_matches = @buffer.scan_matches(regex) scan_online_matches = scan_matches.map {|match_data| OnlineMatch.new(regex, match_data) }.to_set new_matches = scan_online_matches - existing_matches_for_regex # new_matches : Set(OnlineMatch) existing_matches_for_regex.merge(new_matches) new_online_matches.merge(new_matches) end resize_buffer! new_online_matches end |
#matches ⇒ Object
Hash(Regexp, Set(OnlineMatch))
34 35 36 |
# File 'lib/kleene/naive_online_regex.rb', line 34 def matches # Hash(Regexp, Set(OnlineMatch)) @matches_per_regex end |
#matches_for(regex) ⇒ Object
Set(OnlineMatch) | Nil
38 39 40 |
# File 'lib/kleene/naive_online_regex.rb', line 38 def matches_for(regex) # Set(OnlineMatch) | Nil @matches_per_regex[regex] end |
#reset ⇒ Object
13 14 15 16 |
# File 'lib/kleene/naive_online_regex.rb', line 13 def reset @buffer = '' @matches_per_regex = {} # Hash(Regexp, Set(OnlineMatch)) end |
#resize_buffer! ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/kleene/naive_online_regex.rb', line 42 def resize_buffer! return unless @buffer.size > @window_size number_of_chars_at_front_of_buffer_that_should_roll_off = @buffer.size - @window_size @buffer = @buffer[-@window_size..-1] drop_matches_that_have_rolled_off(number_of_chars_at_front_of_buffer_that_should_roll_off) end |