Class: TexterraAPI

Inherits:
IsprasAPI show all
Includes:
TexterraKBM, TexterraNLP
Defined in:
lib/ispras-api/texterra_api.rb

Constant Summary

Constants included from TexterraNLPSpecs

TexterraNLPSpecs::NLP_SPECS

Constants included from TexterraKBMSpecs

TexterraKBMSpecs::KBM_SPECS

Constants inherited from IsprasAPI

IsprasAPI::ROOT_URL

Instance Method Summary collapse

Methods included from TexterraNLP

#disambiguation_annotate, #domain_detection_annotate, #domain_polarity_detection_annotate, #key_concepts_annotate, #language_detection_annotate, #lemmatization_annotate, #named_entities_annotate, #polarity_detection_annotate, #pos_tagging_annotate, #sentence_detection_annotate, #spelling_correction_annotate, #subjectivity_detection_annotate, #syntax_detection, #term_detection_annotate, #tokenization_annotate, #tweet_normalization

Methods included from TexterraKBM

#all_pairs_similarity, #get_attributes, #neighbours, #neighbours_size, #similar_over_filtered_neighbours, #similar_over_first_neighbours, #similarity_between_virtual_articles, #similarity_graph, #similarity_to_virtual_article, #term_commonness, #term_info_measure, #term_meanings, #term_presence

Methods inherited from IsprasAPI

#GET, #POST

Constructor Details

#initialize(key, name = nil, ver = nil) ⇒ TexterraAPI

Returns a new instance of TexterraAPI.



15
16
17
18
19
# File 'lib/ispras-api/texterra_api.rb', line 15

def initialize(key, name = nil, ver = nil)
  name = 'texterra' if name.nil? || name.empty?
  ver = 'v3.1' if ver.nil? || ver.empty?
  super(key, name, ver)
end

Instance Method Details

#custom_query(path, query, form = nil) ⇒ Object



77
78
79
# File 'lib/ispras-api/texterra_api.rb', line 77

def custom_query(path, query, form = nil)
  form.nil? ? GET(path, query) : POST(path, query, form)
end

#disambiguation(text) ⇒ Array

Detects the most appropriate meanings (concepts) for terms occurred in a given text

Parameters:

  • text (String)

    Text to process

Returns:

  • (Array)

    Texterra annotations



73
74
75
# File 'lib/ispras-api/texterra_api.rb', line 73

def disambiguation(text)
  disambiguation_annotate(text)
end

#domain_sentiment_analysis(text, domain = '') ⇒ Hash

Detects whether the given text has positive, negative, or no sentiment, with respect to domain. If domain isn’t provided, Domain detection is applied, this way method tries to achieve best results. If no domain is detected general domain algorithm is applied

Parameters:

  • text (String)

    Text to process

  • domain (String) (defaults to: '')

    domain to use. Can be empty

Returns:

  • (Hash)

    used :domain and detected :polarity



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ispras-api/texterra_api.rb', line 56

def domain_sentiment_analysis(text, domain = '')
  used_domain = 'general'
  sentiment = 'NEUTRAL'
  (domain_polarity_detection_annotate(text, domain) || []).each do |an|
    sentiment = an[:value] if an[:@class].include? 'SentimentPolarity'
    used_domain = an[:value] if an[:@class].include? 'DomainAnnotation'
  end
  {
    domain: used_domain,
    polarity: sentiment
  }
end

#key_concepts(text) ⇒ Array

Key concepts are the concepts providing short (conceptual) and informative text description. This service extracts a set of key concepts for a given text

Parameters:

  • text (String)

    Text to process

Returns:

  • (Array)

    Array of weighted key concepts



30
31
32
33
34
35
36
37
# File 'lib/ispras-api/texterra_api.rb', line 30

def key_concepts(text)
  key_concepts = key_concepts_annotate(text)[0][:value][:concepts_weights][:entry] || []
  key_concepts = [].push key_concepts unless key_concepts.is_a? Array
  key_concepts.map do |kc|
    kc[:concept][:weight] = kc[:double]
    kc[:concept]
  end
end

#sentiment_analysis(text) ⇒ Array

Detects whether the given text has positive, negative or no sentiment

Parameters:

  • text (String)

    Text to process

Returns:

  • (Array)

    Sentiment of the text



43
44
45
46
47
# File 'lib/ispras-api/texterra_api.rb', line 43

def sentiment_analysis(text)
  polarity_detection_annotate(text)[0][:value].to_s || 'NEUTRAL'
  rescue NoMethodError
    'NEUTRAL'
end