Sha256: b119f68ad5ad1f9e8396b5084b70f5969698e98643ac5334e0c74f2965f31424

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

module StanfordCoreNLP::Bridge

  def inject_get_method(klass)
    
    klass.class_eval do
      
      if RUBY_PLATFORM =~ /java/
        return unless method_defined?(:get)
        alias_method :get_without_casting, :get
      end

      # Dynamically defined on all proxied annotation classes.
      # Get an annotation using the annotation bridge.
      def get(annotation, anno_base = nil)

        unless RUBY_PLATFORM =~ /java/
          return unless java_methods.include?('get(Ljava.lang.Class;)')
        end
        
        anno_class = "#{StanfordCoreNLP.camel_case(annotation)}Annotation"
        if anno_base
          unless StanfordNLP::Config::Annotations[anno_base]
            raise "The path #{anno_base} doesn't exist."
          end
          anno_bases = [anno_base]
        else
          anno_bases = StanfordCoreNLP::Config::AnnotationsByName[anno_class]
          raise "The annotation #{anno_class} doesn't exist." unless anno_bases
        end
        if anno_bases.size > 1
          msg = "There are many different annotations bearing the name #{anno_class}." +
          "\nPlease specify one of the following base classes as second parameter to disambiguate: "
          msg << anno_bases.join(',')
          raise msg
        else
          base_class = anno_bases[0]
        end

        if RUBY_PLATFORM =~ /java/
          fqcn = "edu.stanford.#{base_class}"
          class_path = fqcn.split(".")
          class_name = class_path.pop
          path = StanfordCoreNLP.camel_case(class_path.join("."))
          jruby_class = "Java::#{path}::#{class_name}::#{anno_class}"
          get_without_casting(Object.module_eval(jruby_class))
        else
          url = "edu.stanford.#{base_class}$#{anno_class}"
          StanfordCoreNLP::AnnotationBridge.getAnnotation(self, url)
        end
        
      end

    end
    
  end

end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
stanford-core-nlp-3.5.0.alpha lib/stanford-core-nlp/bridge.rb
stanford-core-nlp-0.5.3 lib/stanford-core-nlp/bridge.rb
stanford-core-nlp-abstractor-0.5.3 lib/stanford-core-nlp/bridge.rb
stanford-core-nlp-0.5.1 lib/stanford-core-nlp/bridge.rb
stanford-core-nlp-0.5.0 lib/stanford-core-nlp/bridge.rb