From bf4baff3632943e96ac6475b5703e8a777ae75f6 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 27 Nov 2014 11:29:38 -0500 Subject: [PATCH] Move call method into existing Classifier class --- lib/linguist/classifier.rb | 19 +++++++++++++++++++ lib/linguist/language.rb | 3 +-- lib/linguist/strategy/classifier.rb | 13 ------------- 3 files changed, 20 insertions(+), 15 deletions(-) delete mode 100644 lib/linguist/strategy/classifier.rb diff --git a/lib/linguist/classifier.rb b/lib/linguist/classifier.rb index 5370bdd8..0515577b 100644 --- a/lib/linguist/classifier.rb +++ b/lib/linguist/classifier.rb @@ -3,6 +3,25 @@ require 'linguist/tokenizer' module Linguist # Language bayesian classifier. class Classifier + # Public: Use the classifier to detect language of the blob. + # + # blob - An object that quacks like a blob. + # possible_languages - Array of + # + # Examples + # + # Classifier.call(FileBlob.new("path/to/file"), [ + # Language["Ruby"], Language["Python"] + # ]) + # + # Returns an Array of possible lanuages, most probable first. + def self.call(blob, possible_languages) + language_names = possible_languages.map(&:name) + classify(Samples.cache, blob.data, language_names).map do |name, _| + Language[name] # Return the actual Language objects + end + end + # Public: Train classifier that data is a certain language. # # db - Hash classifier database object diff --git a/lib/linguist/language.rb b/lib/linguist/language.rb index 40570356..07972019 100644 --- a/lib/linguist/language.rb +++ b/lib/linguist/language.rb @@ -12,7 +12,6 @@ require 'linguist/file_blob' require 'linguist/blob_helper' require 'linguist/strategy/filename' require 'linguist/strategy/shebang' -require 'linguist/strategy/classifier' module Linguist # Language names that are recognizable by GitHub. Defined languages @@ -98,7 +97,7 @@ module Linguist Linguist::Strategy::Filename, Linguist::Strategy::Shebang, Linguist::Heuristics, - Linguist::Strategy::Classifier + Linguist::Classifier ] # Public: Detects the Language of the blob. diff --git a/lib/linguist/strategy/classifier.rb b/lib/linguist/strategy/classifier.rb deleted file mode 100644 index 86a516b4..00000000 --- a/lib/linguist/strategy/classifier.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Linguist - module Strategy - # Detect language using the bayesian classifier - class Classifier - def self.call(blob, languages) - Linguist::Classifier.classify(Samples.cache, blob.data, languages.map(&:name)).map do |name, _| - # Return the actual Language object based of the string language name (i.e., first element of `#classify`) - Language[name] - end - end - end - end -end