mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Move call method into existing Classifier class
This commit is contained in:
@@ -3,6 +3,25 @@ require 'linguist/tokenizer'
|
|||||||
module Linguist
|
module Linguist
|
||||||
# Language bayesian classifier.
|
# Language bayesian classifier.
|
||||||
class 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.
|
# Public: Train classifier that data is a certain language.
|
||||||
#
|
#
|
||||||
# db - Hash classifier database object
|
# db - Hash classifier database object
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ require 'linguist/file_blob'
|
|||||||
require 'linguist/blob_helper'
|
require 'linguist/blob_helper'
|
||||||
require 'linguist/strategy/filename'
|
require 'linguist/strategy/filename'
|
||||||
require 'linguist/strategy/shebang'
|
require 'linguist/strategy/shebang'
|
||||||
require 'linguist/strategy/classifier'
|
|
||||||
|
|
||||||
module Linguist
|
module Linguist
|
||||||
# Language names that are recognizable by GitHub. Defined languages
|
# Language names that are recognizable by GitHub. Defined languages
|
||||||
@@ -98,7 +97,7 @@ module Linguist
|
|||||||
Linguist::Strategy::Filename,
|
Linguist::Strategy::Filename,
|
||||||
Linguist::Strategy::Shebang,
|
Linguist::Strategy::Shebang,
|
||||||
Linguist::Heuristics,
|
Linguist::Heuristics,
|
||||||
Linguist::Strategy::Classifier
|
Linguist::Classifier
|
||||||
]
|
]
|
||||||
|
|
||||||
# Public: Detects the Language of the blob.
|
# Public: Detects the Language of the blob.
|
||||||
|
|||||||
@@ -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
|
|
||||||
Reference in New Issue
Block a user