Class: Linguist::Classifier
- Inherits:
-
Object
- Object
- Linguist::Classifier
- Defined in:
- lib/linguist/classifier.rb
Overview
Language bayesian classifier.
Class Method Summary collapse
-
.classify(db, tokens, languages = nil) ⇒ Object
Public: Guess language of data.
-
.train!(db, language, data) ⇒ Object
Public: Train classifier that data is a certain language.
Instance Method Summary collapse
-
#classify(tokens, languages) ⇒ Object
Internal: Guess language of data.
-
#initialize(db = {}) ⇒ Classifier
constructor
Internal: Initialize a Classifier.
-
#language_probability(language) ⇒ Object
Internal: Probably of a language occurring - P©.
-
#token_probability(token, language) ⇒ Object
Internal: Probably of token in language occurring - P(F | C).
-
#tokens_probability(tokens, language) ⇒ Object
Internal: Probably of set of tokens in a language occurring - P(D | C).
Constructor Details
#initialize(db = {}) ⇒ Classifier
Internal: Initialize a Classifier.
60 61 62 63 64 65 66 |
# File 'lib/linguist/classifier.rb', line 60 def initialize(db = {}) @tokens_total = db['tokens_total'] @languages_total = db['languages_total'] @tokens = db['tokens'] @language_tokens = db['language_tokens'] @languages = db['languages'] end |
Class Method Details
.classify(db, tokens, languages = nil) ⇒ Object
Public: Guess language of data.
db - Hash of classifier tokens database. data - Array of tokens or String data to analyze. languages - Array of language name Strings to restrict to.
Examples
Classifier.classify(db, "def hello; end")
# => [ 'Ruby', 0.90], ['Python', 0.2], ... ]
Returns sorted Array of result pairs. Each pair contains the String language name and a Float score.
54 55 56 57 |
# File 'lib/linguist/classifier.rb', line 54 def self.classify(db, tokens, languages = nil) languages ||= db['languages'].keys new(db).classify(tokens, languages) end |
.train!(db, language, data) ⇒ Object
Public: Train classifier that data is a certain language.
db - Hash classifier database object language - String language of data data - String contents of file
Examples
Classifier.train(db, 'Ruby', "def hello; end")
Returns nothing.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/linguist/classifier.rb', line 17 def self.train!(db, language, data) tokens = Tokenizer.tokenize(data) db['tokens_total'] ||= 0 db['languages_total'] ||= 0 db['tokens'] ||= {} db['language_tokens'] ||= {} db['languages'] ||= {} tokens.each do |token| db['tokens'][language] ||= {} db['tokens'][language][token] ||= 0 db['tokens'][language][token] += 1 db['language_tokens'][language] ||= 0 db['language_tokens'][language] += 1 db['tokens_total'] += 1 end db['languages'][language] ||= 0 db['languages'][language] += 1 db['languages_total'] += 1 nil end |
Instance Method Details
#classify(tokens, languages) ⇒ Object
Internal: Guess language of data
data - Array of tokens or String data to analyze. languages - Array of language name Strings to restrict to.
Returns sorted Array of result pairs. Each pair contains the String language name and a Float score.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/linguist/classifier.rb', line 75 def classify(tokens, languages) return [] if tokens.nil? tokens = Tokenizer.tokenize(tokens) if tokens.is_a?(String) scores = {} languages.each do |language| scores[language] = tokens_probability(tokens, language) + language_probability(language) end scores.sort { |a, b| b[1] <=> a[1] }.map { |score| [score[0], score[1]] } end |
#language_probability(language) ⇒ Object
Internal: Probably of a language occurring - P©
language - Language to check.
Returns Float between 0.0 and 1.0.
119 120 121 |
# File 'lib/linguist/classifier.rb', line 119 def language_probability(language) Math.log(@languages[language].to_f / @languages_total.to_f) end |
#token_probability(token, language) ⇒ Object
Internal: Probably of token in language occurring - P(F | C)
token - String token. language - Language to check.
Returns Float between 0.0 and 1.0.
106 107 108 109 110 111 112 |
# File 'lib/linguist/classifier.rb', line 106 def token_probability(token, language) if @tokens[language][token].to_f == 0.0 1 / @tokens_total.to_f else @tokens[language][token].to_f / @language_tokens[language].to_f end end |
#tokens_probability(tokens, language) ⇒ Object
Internal: Probably of set of tokens in a language occurring - P(D | C)
tokens - Array of String tokens. language - Language to check.
Returns Float between 0.0 and 1.0.
94 95 96 97 98 |
# File 'lib/linguist/classifier.rb', line 94 def tokens_probability(tokens, language) tokens.inject(0.0) do |sum, token| sum += Math.log(token_probability(token, language)) end end |