Sha256: 8b8c8d577efc44d37ef7503a393507b75b0b17669107eb890583c59e8621b884

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

module Opener
  class PolarityTagger
    class LexiconMap

      attr_reader :resource
      attr_reader :negators
      attr_reader :intensifiers
      attr_reader :with_polarity

      POS_ORDER = 'NRVGAO'
      UNKNOWN   = Hashie::Mash.new polarity: 'unknown'

      def initialize lang:, lexicons:
        @lang          = lang
        @lexicons      = lexicons

        @negators      = {}
        @intensifiers  = {}
        @with_polarity = {}
        map lexicons
      end

      DEFAULT_POS = 'O'

      POS_SHORT_MAP = {
        adj:         'G',
        adv:         'A',
        noun:        'N',
        propernoun:  'N',
        other:       'O',
        prep:        'P',
        verb:        'V',
        nil =>       DEFAULT_POS,
        multi_word_expression: 'O',
      }

      def by_negator lemma
        @negators[lemma]
      end

      def by_intensifier lemma
        @intensifiers[lemma]
      end

      def by_polarity lemma, short_pos
        return [@with_polarity[lemma+short_pos] || UNKNOWN, short_pos] if short_pos

        POS_ORDER.chars.each do |short_pos|
          if l = @with_polarity[lemma+short_pos]
            puts "Found polarify #{l.polarity} for #{lemma} with PoS #{short_pos}"
            return [l, short_pos]
          end
        end

        [UNKNOWN, 'unknown']
      end

      protected

      def map lexicons
        lexicons.each do |l|
          next if l.lemma.nil?

          case l.type
          when 'polarityShifter' then @negators[l.lemma]     = l
          when 'intensifier'     then @intensifiers[l.lemma] = l
          else
            if l.polarity
              short_pos = POS_SHORT_MAP[l.pos&.to_sym] || DEFAULT_POS
              @with_polarity[l.lemma+short_pos] = l
            end
          end
        end

        puts "#{@lang}: loaded #{@negators.size} negators"
        puts "#{@lang}: loaded #{@intensifiers.size} intensifiers"
        puts "#{@lang}: loaded #{@with_polarity.size} elements with polarity"
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
opener-polarity-tagger-3.1.3 lib/opener/polarity_tagger/lexicon_map.rb
opener-polarity-tagger-3.1.2 lib/opener/polarity_tagger/lexicon_map.rb
opener-polarity-tagger-3.1.1 lib/opener/polarity_tagger/lexicon_map.rb
opener-polarity-tagger-3.1.0 lib/opener/polarity_tagger/lexicon_map.rb