<commit_before>module Lurn
  module Text
    class BernoulliVectorizer

      attr_accessor :tokenizer
      attr_accessor :vocabulary

      def initialize(options = {})
        @tokenizer = options[:tokenizer] || WordTokenizer.new
        @vocabulary = []

        options[:max_df] ||= 50
        options[:min_df] ||= 0
        @options = options
      end

      def fit(documents)
        @vocabulary = []
        tokenized_docs = tokenize_documents(documents)
        @vocabulary = tokenized_docs.flatten.uniq.sort
        reduce_features(tokenized_docs)
      end

      def to_h
        {
          tokenizer_options: @tokenizer.to_h,
          vocabulary: @vocabulary
        }
      end

      def transform(documents)
        documents.map do |document|
          tokens = @tokenizer.tokenize(document)
          @vocabulary.map do |word|
            tokens.include? word
          end
        end
      end

      private

      def reduce_features(tokenized_docs)
        doc_frequencies = Array.new(@vocabulary.length, 0)

        tokenized_docs.each do |tokens|<commit_msg>Handle ngram vocabularies in the bernoulli vectorizer<commit_after>module Lurn
  module Text
    class BernoulliVectorizer

      attr_accessor :tokenizer
      attr_accessor :vocabulary

      def initialize(options = {})
        @tokenizer = options[:tokenizer] || WordTokenizer.new
        @vocabulary = []

        options[:max_df] ||= 50
        options[:min_df] ||= 0
        @options = options
      end

      def fit(documents)
        @vocabulary = []
        tokenized_docs = tokenize_documents(documents)
        @vocabulary = tokenized_docs.flatten(1).uniq.sort
        reduce_features(tokenized_docs)
      end

      def to_h
        {
          tokenizer_options: @tokenizer.to_h,
          vocabulary: @vocabulary
        }
      end

      def transform(documents)
        documents.map do |document|
          tokens = @tokenizer.tokenize(document)
          @vocabulary.map do |word|
            tokens.include? word
          end
        end
      end

      private

      def reduce_features(tokenized_docs)
        doc_frequencies = Array.new(@vocabulary.length, 0)

        tokenized_docs.each do |tokens|
{'fraction_non_alphanumeric': 0.08028867839422643, 'fraction_numerical': 0.0040595399188092015, 'mean_word_length': 2.0508940852819806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7375312', 'n_tokens_mistral': 673, 'n_tokens_neox': 641, 'n_words': 131}