Permalink
Browse files

Update Ruby/Python examples with labels example

  • Loading branch information...
1 parent 5bb8b7f commit 1793eb301cb5be0b4872f08dacc2c0c8d06598a9 @palexander palexander committed Mar 5, 2014
Showing with 18 additions and 8 deletions.
  1. +9 −4 python/annotate_text.py
  2. +9 −4 ruby/annotate_text.rb
View
@@ -11,9 +11,9 @@ def get_json(url):
opener.addheaders = [('Authorization', 'apikey token=' + API_KEY)]
return json.loads(opener.open(url).read())
-def print_annotations(annotations):
+def print_annotations(annotations, get_class=True):
for result in annotations:
- class_details = get_json(result["annotatedClass"]["links"]["self"])
+ class_details = get_json(result["annotatedClass"]["links"]["self"]) if get_class else result["annotatedClass"]
print "Class details"
print "\tid: " + class_details["@id"]
print "\tprefLabel: " + class_details["prefLabel"]
@@ -38,13 +38,18 @@ def print_annotations(annotations):
print "\n\n"
-# Annotate using the provided text
text_to_annotate = "Melanoma is a malignant tumor of melanocytes which are found predominantly in skin but also in the bowel and the eye."
+
+# Annotate using the provided text
annotations = get_json(REST_URL + "/annotator?text=" + urllib2.quote(text_to_annotate))
# Print out annotation details
print_annotations(annotations)
# Annotate with hierarchy information
annotations = get_json(REST_URL + "/annotator?max_level=3&text=" + urllib2.quote(text_to_annotate))
-print_annotations(annotations)
+print_annotations(annotations)
+
+# Annotate with prefLabel, synonym, definition returned
+annotations = get_json(REST_URL + "/annotator?include=prefLabel,synonym,definition&text=" + urllib2.quote(text_to_annotate))
+print_annotations(annotations, False)
View
@@ -9,9 +9,9 @@ def get_json(url)
JSON.parse(open(url, "Authorization" => "apikey token=#{API_KEY}").read)
end
-def puts_annotations(annotations)
+def puts_annotations(annotations, get_class = true)
annotations.each do |result|
- class_details = get_json(result["annotatedClass"]["links"]["self"])
+ class_details = get_class ? get_json(result["annotatedClass"]["links"]["self"]) : result["annotatedClass"]
puts "Class details"
puts "\tid: " + class_details["@id"]
puts "\tprefLabel: " + class_details["prefLabel"]
@@ -41,13 +41,18 @@ def puts_annotations(annotations)
end
end
-# Annotate using the provided text
text_to_annotate = "Melanoma is a malignant tumor of melanocytes which are found predominantly in skin but also in the bowel and the eye."
+
+# Annotate using the provided text
annotations = get_json(REST_URL + "/annotator?text=" + CGI.escape(text_to_annotate))
-# puts out annotation details
+# Prints out annotation details
puts_annotations(annotations)
# Annotate with hierarchy information
annotations = get_json(REST_URL + "/annotator?max_level=3&text=" + CGI.escape(text_to_annotate))
puts_annotations(annotations)
+
+# Annotate with prefLabel, synonym, definition returned
+annotations = get_json(REST_URL + "/annotator?include=prefLabel,synonym,definition&text=" + CGI.escape(text_to_annotate))
+puts_annotations(annotations)

0 comments on commit 1793eb3

Please sign in to comment.