Sha256: dcbb549cbc780ada815a21c062b23de317c7ffdeb0c7e14cd74ba9873a804e63

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module StanfordCoreNLP

  # Modify the Rjb JavaProxy class to add our own methods to every Java object.
  Rjb::Rjb_JavaProxy.class_eval do

    # Dynamically defined on all proxied Java objects.
    # Shorthand for to_string defined by Java classes.
    def to_s; to_string; end

    # Dynamically defined on all proxied Java iterators.
    # Provide Ruby-style iterators to wrap Java iterators.
    def each
      if !java_methods.include?('iterator()')
        raise 'This object cannot be iterated.'
      else
        i = self.iterator
        while i.has_next; yield i.next; end
      end
    end

    # Dynamically defined on all proxied annotation classes.
    # Get an annotation using the annotation bridge.
    def get(annotation)
      if !java_methods.include?('get(Ljava.lang.Class;)')
        raise 'No annotation can be retrieved on this object.'
      else
        base_class = (annotation.to_s.split('_')[0] == 'coref') ?
        'edu.stanford.nlp.dcoref.CorefCoreAnnotations$' :
        'edu.stanford.nlp.ling.CoreAnnotations$'
        anno_class = annotation.to_s.gsub(/^[a-z]|_[a-z]/) { |a| a.upcase }.gsub('_', '')
        url = "#{base_class}#{anno_class}Annotation"
        AnnotationBridge.getAnnotation(self, url)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stanford-core-nlp-0.1.1 lib/stanford-core-nlp/java-wrapper.rb