<commit_before>module Gramercy
  module Grammar
    class Parser

      attr_reader :text

      STRUCTURES = {
        question: [
          Structures::SimpleQuestionWithInterrogative,
          Structures::SimpleQuestion
        ],
        statement: [
          Structures::SimpleDeclarative
        ]
      }

      def initialize(text="")
        @text = text.downcase
      end

      def context
        Meta::Context.from(nouns).first
      end

      def contexts
        @contexts ||= Meta::Context.from(nouns)
      end

      def interrogative
        parser.interrogative
      end

      def sentence_type
        return :question if self.text =~ /\?$/
        :statement
      end

      def nouns
        ([subject.to_s.split.last] + [predicate.to_s.split.last]).compact
      end

      # FIXME Implement this a better way than initializing the parser class 2x!
      def parser
        @parser ||= begin
          STRUCTURES[sentence_type].detect do |structure|
            candidate = structure.new(split_text, position_of(verb))
            candidate.conforms? && candidate
          end.new(split_text, position_of(verb))
        end
      end<commit_msg>Detect positivity of sentences in parser.<commit_after>module Gramercy
  module Grammar
    class Parser

      attr_reader :text

      STRUCTURES = {
        question: [
          Structures::SimpleQuestionWithInterrogative,
          Structures::SimpleQuestion
        ],
        statement: [
          Structures::SimpleDeclarative
        ]
      }

      def initialize(text="")
        @text = text.downcase
      end

      def context
        Meta::Context.from(nouns).first
      end

      def contexts
        @contexts ||= Meta::Context.from(nouns)
      end

      def positivity
        roots = Gramercy::PartOfSpeech::Generic.query_as(:w).match('(r)-[IN_FORM]->(w)').where("w.base_form in #{split_text}").return('r').map(&:r)
        contexts.map do |context|
          roots.map{|root| root.positivity_in_context(context).to_i }
        end.flatten.sum
      end

      def interrogative
        parser.interrogative
      end

      def sentence_type
        return :question if self.text =~ /\?$/
        :statement
      end

      def nouns
        ([subject.to_s.split.last] + [predicate.to_s.split.last]).compact
      end

      # FIXME Implement this a better way than initializing the parser class 2x!
      def parser
        @parser ||= begin
          STRUCTURES[sentence_type].detect do |structure|
            candidate = structure.new(split_text, position_of(verb))
            candidate.conforms? && candidate
          end.new(split_text, position_of(verb))
        end
      end
{'fraction_non_alphanumeric': 0.09330855018587361, 'fraction_numerical': 0.0007434944237918215, 'mean_word_length': 1.9866814650388458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2988912', 'n_tokens_mistral': 793, 'n_tokens_neox': 753, 'n_words': 163}