File: similarity.rb

package info (click to toggle)
ruby-rouge 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: ruby: 38,489; sed: 2,071; perl: 152; makefile: 8
file content (31 lines) | stat: -rw-r--r-- 851 bytes parent folder | download | duplicates (4)
1
2
3
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
module Similarity
  def self.test(lexer_class)
    # state_defintions is an InheritableHash, so we use `own_keys` to
    # exclude states inherited from superclasses
    state_names = Set.new(lexer_class.state_definitions.own_keys)

    candidates = Rouge::Lexer.all.select do |x|
      # we can only compare to RegexLexers which have state_definitions
      next false unless x < Rouge::RegexLexer

      # don't compare a lexer to itself or any subclasses
      next false if x <= lexer_class

      true
    end

    max_score = 1
    matches = []
    candidates.each do |candidate|
      score = (state_names & candidate.state_definitions.keys).size
      if score > max_score
        max_score = score
        matches = [candidate]
      elsif score == max_score
        matches << candidate
      end
    end

    [max_score, matches]
  end
end