Module: TraceVisualization::LexemeOverlapFilter

Defined in:
lib/trace_visualization/lexeme_overlap_filter.rb

Class Method Summary collapse

Class Method Details

.is_longest_token(token_positions, token_pos) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/trace_visualization/lexeme_overlap_filter.rb', line 51

def self.is_longest_token(token_positions, token_pos)
  result = true

  token_positions.each do |other_token_pos|
    if token_pos != other_token_pos && token_pos.token.length < other_token_pos.token.length
      result = false
      break
    end
  end

  result
end

.process(token_positions) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/trace_visualization/lexeme_overlap_filter.rb', line 4

def self.process(token_positions)
  token_positions.sort! { |a, b| a.pos <=> b.pos }
  left_bound = token_positions.inject(0) do |left_bound, token_pos| 
    [token_pos.pos + token_pos.token.length, left_bound].max 
  end

  idx, current, result = 0, [], []

  for pos in 0 .. left_bound
  
    i = 0
    while i < current.size
      token_pos = current[i]

      if token_pos.pos + token_pos.token.length == pos
        fl_delete_token = false
      
        if current.size == 1
          result << token_pos
          fl_delete_token = true
        else
          if is_longest_token(current, token_pos)
            result << token_pos
            current = []
          else
            fl_delete_token = true
          end
        end
  
        if fl_delete_token
          current.delete_at(i)
          i -= 1
        end      
      end

      i += 1
    end

    while idx < token_positions.size && token_positions[idx].pos == pos
      current << token_positions[idx] 
      idx += 1
    end
  end

  result
end