From 01de40faaaa990394063925f51bec8472753be87 Mon Sep 17 00:00:00 2001 From: Colin Seymour Date: Mon, 13 Feb 2017 18:22:54 +0000 Subject: [PATCH] Return early in Classifier.classify if no languages supplied (#3471) * Return early if no languages supplied There's no need to tokenise the data when attempting to classify without a limited language scope as no action will be performed when it comes to scoring anyway. * Add test for empty languages array --- lib/linguist/classifier.rb | 2 +- test/test_classifier.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/linguist/classifier.rb b/lib/linguist/classifier.rb index 89a0df2f..4d549cc9 100644 --- a/lib/linguist/classifier.rb +++ b/lib/linguist/classifier.rb @@ -95,7 +95,7 @@ module Linguist # Returns sorted Array of result pairs. Each pair contains the # String language name and a Float score. def classify(tokens, languages) - return [] if tokens.nil? + return [] if tokens.nil? || languages.empty? tokens = Tokenizer.tokenize(tokens) if tokens.is_a?(String) scores = {} diff --git a/test/test_classifier.rb b/test/test_classifier.rb index 2ae2f45e..818bee29 100644 --- a/test/test_classifier.rb +++ b/test/test_classifier.rb @@ -53,4 +53,8 @@ class TestClassifier < Minitest::Test assert_equal language.name, results.first[0], "#{sample[:path]}\n#{results.inspect}" end end + + def test_classify_empty_languages + assert_equal [], Classifier.classify(Samples.cache, fixture("Ruby/foo.rb"), []) + end end