mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Sample returns simple hash objects
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
require 'linguist/classifier'
|
||||
require 'linguist/language'
|
||||
|
||||
module Linguist
|
||||
# Model for accessing classifier training data.
|
||||
class Sample
|
||||
module Sample
|
||||
# Samples live in test/ for now, we'll eventually move them out
|
||||
PATH = File.expand_path("../../../samples", __FILE__)
|
||||
|
||||
# Public: Iterate over each Sample.
|
||||
# Public: Iterate over each sample.
|
||||
#
|
||||
# &block - Yields Sample to block
|
||||
#
|
||||
@@ -20,14 +17,10 @@ module Linguist
|
||||
# Possibly reconsider this later
|
||||
next if category == 'text' || category == 'binary'
|
||||
|
||||
# Map directory name to a Language alias
|
||||
language = Linguist::Language.find_by_alias(category)
|
||||
raise "No language for #{category.inspect}" unless language
|
||||
|
||||
dirname = File.join(PATH, category)
|
||||
Dir.entries(dirname).each do |filename|
|
||||
next if filename == '.' || filename == '..'
|
||||
yield new(File.join(dirname, filename), language)
|
||||
yield({ :path => File.join(dirname, filename), :language => category })
|
||||
end
|
||||
end
|
||||
|
||||
@@ -38,37 +31,16 @@ module Linguist
|
||||
#
|
||||
# Returns trained Classifier.
|
||||
def self.classifier
|
||||
require 'linguist/classifier'
|
||||
require 'linguist/language'
|
||||
|
||||
classifier = Classifier.new
|
||||
each { |sample| classifier.train(sample.language.name, sample.data) }
|
||||
each { |sample|
|
||||
language = Language.find_by_alias(sample[:language])
|
||||
data = File.read(sample[:path])
|
||||
classifier.train(language.name, data)
|
||||
}
|
||||
classifier.gc
|
||||
end
|
||||
|
||||
# Internal: Initialize Sample.
|
||||
#
|
||||
# Samples should be initialized by Sample.each.
|
||||
#
|
||||
# path - String full path to file.
|
||||
# language - Language of sample.
|
||||
def initialize(path, language)
|
||||
@path = path
|
||||
@language = language
|
||||
end
|
||||
|
||||
# Public: Get full path to file.
|
||||
#
|
||||
# Returns String.
|
||||
attr_reader :path
|
||||
|
||||
# Public: Get sample language.
|
||||
#
|
||||
# Returns Language.
|
||||
attr_reader :language
|
||||
|
||||
# Public: Read file contents.
|
||||
#
|
||||
# Returns String.
|
||||
def data
|
||||
File.read(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user