Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a rake similarity[lexer_name] and Similarity tester #1206

Merged
merged 3 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions spec/support/similarity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
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
26 changes: 26 additions & 0 deletions tasks/similarity.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def test_similarity(lexer_class)
score, matches = Similarity.test(lexer_class)

if score == 1
puts "[none]"
else
puts "[#{score}] #{matches.map(&:tag).join(', ')}"
end
end

desc "tests the similarity with existing lexers"
task :similarity, [:language] do |t, args|
require 'rouge'
require "#{File.dirname(__dir__)}/spec/support/similarity.rb"

language = args.language

if language
test_similarity Rouge::Lexer.find(language)
else
Rouge::Lexer.all.each do |lexer_class|
print "#{lexer_class.tag}: "
test_similarity lexer_class if lexer_class < Rouge::RegexLexer
end
end
end