Class: Rblearn::CountVectorizer
- Inherits:
-
Object
- Object
- Rblearn::CountVectorizer
- Defined in:
- lib/rblearn/CountVectorizer.rb
Instance Attribute Summary collapse
-
#doc_matrix ⇒ Object
TODO: consider the access controll about all variables.
-
#feature_names ⇒ Object
TODO: consider the access controll about all variables.
-
#token2index ⇒ Object
TODO: consider the access controll about all variables.
Instance Method Summary collapse
-
#fit_transform(features) ⇒ Object
- features: Each documents’ feature
-
Array<String> -> NArray::Int64.
-
#initialize(tokenizer, lowercase = true, max_features = 0.8) ⇒ CountVectorizer
constructor
- tokenizer: lambda function
- string -> Array<string> lowcase: whether if words are lowercases
- bool stop_words: list of stop words
- Array<string> max_features: limitation of feature size
-
Float in [0, 1] TODO: by max_features, zero vectors are sometimes created.
Constructor Details
#initialize(tokenizer, lowercase = true, max_features = 0.8) ⇒ CountVectorizer
- tokenizer: lambda function
-
string -> Array<string>
- lowcase: whether if words are lowercases
-
bool
- stop_words: list of stop words
-
Array<string>
- max_features: limitation of feature size
-
Float in [0, 1]
TODO: by max_features, zero vectors are sometimes created.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rblearn/CountVectorizer.rb', line 14 def initialize(tokenizer, lowercase=true, max_features=0.8) @tokenizer = tokenizer @lowercase = lowercase stop_words = Stopwords::STOP_WORDS stop_words.map! {|token| token.stem} stop_words.map! {|token| token.downcase} if @lowercase @stopwords = stop_words @max_feature = max_features end |
Instance Attribute Details
#doc_matrix ⇒ Object
TODO: consider the access controll about all variables
7 8 9 |
# File 'lib/rblearn/CountVectorizer.rb', line 7 def doc_matrix @doc_matrix end |
#feature_names ⇒ Object
TODO: consider the access controll about all variables
7 8 9 |
# File 'lib/rblearn/CountVectorizer.rb', line 7 def feature_names @feature_names end |
#token2index ⇒ Object
TODO: consider the access controll about all variables
7 8 9 |
# File 'lib/rblearn/CountVectorizer.rb', line 7 def token2index @token2index end |
Instance Method Details
#fit_transform(features) ⇒ Object
- features: Each documents’ feature
-
Array<String> -> NArray::Int64
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rblearn/CountVectorizer.rb', line 26 def fit_transform(features) all_vocaburaries = [] word_frequency = Hash.new{|hash, key| hash[key] = 0} features.each do |feature| @tokenizer.call(feature).each do |token| token.downcase! if @lowercase all_vocaburaries << token word_frequency[token] += 1 end end all_vocaburaries.uniq! word_frequency = word_frequency.sort{|(_, value1), (_, value2)| value2 <=> value1} feature_names = (0...(word_frequency.size * @max_feature).to_i).map{|i| word_frequency[i][0]} token2index = {} feature_names.each_with_index do |token, i| token2index[token] = i end doc_matrix = Numo::Int32.zeros([features.size, feature_names.size]) features.each_with_index do |feature, doc_id| tokens = [] @tokenizer.call(feature).each do |token| token.downcase! if @lowercase tokens << token unless @stopwords.include?(token) end # BoW representation counter = Hash.new{|hash, key| hash[key] = 0} tokens.each do |token| counter[token] += 1 end counter.each do |token, freq| doc_matrix[doc_id, token2index[token]] = freq if token2index[token] end end @doc_matrix = doc_matrix @feature_names = feature_names @token2index = token2index return @doc_matrix end |