Commit 4667fe35 authored by alexlag's avatar alexlag
Browse files

new representation entrypoint. cleanup

parent 5afb48f4
require_relative 'ispras-api/texterra_api'
require_relative './ispras-api/texterra_api'
require 'httparty'
require 'nori'
require_relative 'api_error'
require_relative './api_error'
class IsprasAPI
include HTTParty
......
......@@ -3,43 +3,29 @@ require_relative './kbm_specs'
module TexterraKBM
include TexterraKBMSpecs
# Determines if Knowledge base contains the specified term
#
# @param term [String] term
# @return [Hash] with :presence field
def term_presence(term)
preset_kbm :termPresence, term
end
# Returns information measure for the given term. Information measure denotes,
# how often given term is used as link caption among all its occurences
#
# @param term [String] term
# @result [Hash] with :infomeasure field
def term_info_measure(term)
preset_kbm :termInfoMeasure, term
end
# Return concepts resource from the Knowledge base corresponding
# to the found meanings of the given term
#
# @param term [String] term
# @result [Hash] with :elements field
def term_meanings(term)
preset_kbm :termMeanings, term
end
# If concept isn't provided, returns concepts with their commonness,
# corresponding to the found meanings of the given term.
# Commonness denotes, how often the given term is associated with the given concept.
# With concept(format is {id}:{kbname}) returns commonness of given concept for the given term.
#
# @param term [String] term
# @param concept [String] concept as {id}:{kbname}
# @result [Hash] with :elements field
def term_commonness(term, concept = '')
concept = "id=#{concept}" unless concept.empty?
preset_kbm :termCommonness, [term, concept]
# Determines if Knowledge base contains the specified terms and computes features of the specified types for them.
#
# @param [String] text Text with term-candidates
# @param [Array] term_candidates Term-candidates start and end
# @param [Hash] params Specifies types of features required to compute
#
# @result [Array] Texterra annotations
def representation_terms(text, term_candidates, params={featureType: ['commonness', 'info-measure']})
path = KBM_SPECS[:representationTerms][:path]
options = {
headers: {
'Content-Type' => 'application/json'
},
query: params,
body: {
text: text,
annotations: {
'term-candidate' => term_candidates
}
}.to_json
}
response = self.class.post "/#{path}", options
response.code == 200 ? response.parsed_response : check_error(response)
end
# Return neighbour concepts for the given concepts(list or single concept, each concept is {id}:{kbname}).
......@@ -97,7 +83,7 @@ module TexterraKBM
preset_kbm :allPairsSimilarity, ["#{wrap_concepts(first_concepts)}linkWeight=#{linkWeight}", wrap_concepts(second_concepts)]
end
# Compute similarity from each concept from the first list to all concepts(list or single concept, each concept is {id}:{kbname}) from the second list as a whole.
# Compute similarity from each concept from the first list to all concepts(list or single concept, each concept is {id}:{kbname}) from the second list as a whole.
# Links of second list concepts(each concept is {id}:{kbname}) are collected together, thus forming a "virtual" article, similarity to which is computed.
#
# @param [Array<String>] concepts Array of concepts as {id}:{kbname}
......@@ -107,7 +93,7 @@ module TexterraKBM
preset_kbm :similarityToVirtualArticle, ["#{wrap_concepts(concepts)}linkWeight=#{linkWeight}", wrap_concepts(virtual_aricle)]
end
# Compute similarity between two sets of concepts(list or single concept, each concept is {id}:{kbname}) as between "virtual" articles from these sets.
# Compute similarity between two sets of concepts(list or single concept, each concept is {id}:{kbname}) as between "virtual" articles from these sets.
# The links of each virtual article are composed of links of the collection of concepts.
#
# @param [Array<String>] first_virtual_aricle Array of concepts as {id}:{kbname}
......
module TexterraKBMSpecs
# Path and parameters for preset KBM queries
KBM_SPECS = {
termPresence: {
path: 'representation/%s/contained',
params: {}
},
termInfoMeasure: {
path: 'representation/%s/infomeasure',
params: {}
},
termMeanings: {
path: 'representation/%s/meanings',
params: {}
},
termCommonness: {
path: 'representation/%s/commonness/%s',
representationTerms: {
path: 'representation/terms',
params: {}
},
neighbours: {
......
......@@ -32,7 +32,7 @@ module TexterraNLPSpecs
posTagging: {
path: 'nlp/pos',
params: {
class: 'ru.ispras.texterra.core.nlp.datamodel.pos.POSToken',
class: 'pos-token',
filtering: 'KEEPING'
}
},
......@@ -60,7 +60,7 @@ module TexterraNLPSpecs
disambiguation: {
path: 'nlp/disambiguation',
params: {
class: 'dmb-phrase',
class: 'disambiguated-phrase',
filtering: 'KEEPING'
}
......@@ -68,7 +68,7 @@ module TexterraNLPSpecs
keyConcepts: {
path: 'nlp/keyconcepts',
params: {
class: 'keyconcepts-sc',
class: 'keyconcepts',
filtering: 'KEEPING'
}
......@@ -84,7 +84,7 @@ module TexterraNLPSpecs
subjectivityDetection: {
path: 'nlp/subjectivity',
params: {
class: 'sentiment-subjectivity',
class: 'subjectivity',
filtering: 'KEEPING'
}
......@@ -92,7 +92,7 @@ module TexterraNLPSpecs
polarityDetection: {
path: 'nlp/polarity',
params: {
class: 'sentiment-polarity',
class: 'polarity',
filtering: 'KEEPING'
}
......@@ -108,7 +108,7 @@ module TexterraNLPSpecs
domainPolarityDetection: {
path: 'nlp/domainpolarity%s',
params: {
class: %w(domain sentiment-polarity),
class: %w(domain polarity),
filtering: 'KEEPING'
}
......
require 'json'
require_relative './ispras_api'
require_relative './texterra/nlp'
require_relative './texterra/kbm'
......@@ -10,7 +11,16 @@ class TexterraAPI < IsprasAPI
disable_rails_query_string_format
nori = Nori.new(parser: :rexml,
convert_tags_to: ->(tag) { tag.snakecase.to_sym })
parser proc { |data| nori.parse data }
parser(
proc do |data|
result = nori.parse(data)
begin
result = JSON.parse(data, symbolize_names: true) if result.empty?
rescue
end
result
end
)
def initialize(key, name = nil, ver = nil)
name = 'texterra' if name.nil? || name.empty?
......
module Version
MAJOR = 0
MINOR = 1
PATCH = 6
PATCH = 7
PRE = nil
YEAR = '2016'
MONTH = '08'
DAY = '26'
MONTH = '09'
DAY = '09'
def self.to_s
[MAJOR, MINOR, PATCH, PRE].compact.join('.')
......
......@@ -101,23 +101,17 @@ class TestTexterraAPI < Minitest::Test
assert_instance_of Array, @texterra.subjectivity_detection_annotate(@ru_tweet)
end
def test_term_presence
res = @texterra.term_presence('Anarchism')
def test_representation_terms
term_candidates = [
{ start: 0, end: 5 },
{ start: 6, end: 11 }
]
res = @texterra.representation_terms(@en_text, term_candidates, featureType: ['commonness', 'info-measure'])
assert_instance_of Hash, res
assert_equal true, res[:presence]
end
def test_term_info_measure
assert_instance_of Hash, @texterra.term_info_measure('Anarchism')
end
def test_term_meanings
assert_instance_of Hash, @texterra.term_meanings('android')
end
def test_term_commonness
assert_instance_of Hash, @texterra.term_commonness('android')
assert_instance_of Hash, @texterra.term_commonness('android', '713:enwiki')
assert_equal res[:text], @en_text
assert_instance_of Hash, res[:annotations]
assert_instance_of Array, res[:annotations][:commonness]
assert_instance_of Array, res[:annotations][:'info-measure']
end
def test_neighbours
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment