Class: WLAPI::API
Overview
This class represents an interface to the linguistic web services provided by the University of Leipzig.
See the project ‘Wortschatz Leipzig’ for more details.
Constant Summary collapse
- ENDPOINT =
SOAP Services Endpoint.
'http://wortschatz.uni-leipzig.de/axis/services'- SERVICES =
The list of accessible services, the MARSService is excluded due to its internal authorization.
[ :Baseform, :Cooccurrences, :CooccurrencesAll, :ExperimentalSynonyms, :Frequencies, :Kookurrenzschnitt, :Kreuzwortraetsel, :LeftCollocationFinder, :LeftNeighbours, :NGrams, :NGramReferences, :RightCollocationFinder, :RightNeighbours, :Sachgebiet, :Sentences, :Similarity, :Synonyms, :Thesaurus, :Wordforms ]
Instance Method Summary collapse
-
#baseform(word) ⇒ Array
Gets the baseform (whatever it is :), not lemma).
- #check_params(*args) ⇒ Object private
-
#cooccurrences(word, sign, limit = 10) ⇒ Object
Returns statistically significant co-occurrences of the input word.
-
#cooccurrences_all(word, sign, limit = 10) ⇒ Object
Returns statistically significant co-occurrences of the input word.
-
#crossword(word, word_length, limit = 10) ⇒ Object
Attempts to find suitable words given a pattern as word parameter, a word length and the number of words to find at max (limit), e.g.
-
#domain(word) ⇒ Object
Returns categories for a given input word as an array: api.domain(“Michael”) => [“Vorname”, “Nachname”, “Männername”] – Is it a good name? all names are in English, but here..
-
#experimental_synonyms(word, limit = 10) ⇒ Object
This service delivers an experimental synonyms request for internal tests.
-
#frequencies(word) ⇒ Array
Returns the frequency and frequency class of the input word.
-
#get_answer(doc, mod = '') ⇒ Object
private
This method extracts valuable data from the XML structure of the soap response.
-
#initialize(login = 'anonymous', pass = 'anonymous') ⇒ API
constructor
At the creation point clients for all services are being instantiated.
-
#intersection(word1, word2, limit = 10) ⇒ Object
Returns the intersection of the co-occurrences of the two given words.
-
#left_collocation_finder(word, pos, limit = 10) ⇒ Object
Attempts to find linguistic collocations that occur to the left of the given input word.
-
#left_neighbours(word, limit = 10) ⇒ Object
For a given input word, returns statistically significant left neighbours (words co-occurring immediately to the left of the input word).
- #msg(arg, meth, cls) ⇒ Object private
- #ngram_references(pattern, limit = 10) ⇒ Object
- #ngrams(pattern, limit = 10) ⇒ Object
-
#query(cl, *args) ⇒ Object
private
Main query method, it invokes the soap engine.
-
#right_collocation_finder(word, pos, limit = 10) ⇒ Object
Attempts to find linguistic collocations that occur to the right of the given input word.
-
#right_neighbours(word, limit = 10) ⇒ Object
For a given input word, returns statistically significant right neighbours (words co-occurring immediately to the right of the input word).
-
#sentences(word, limit = 10) ⇒ Object
Returns sample sentences containing the input word.
-
#similarity(word, limit = 10) ⇒ Object
Returns automatically computed contextually similar words of the input word.
-
#synonyms(word, limit = 10) ⇒ Array
This method searches for synonyms.
-
#thesaurus(word, limit = 10) ⇒ Array
As the Synonyms service returns synonyms of the given input word.
-
#wordforms(word, limit = 10) ⇒ Array
Returns all other word forms of the same lemma for a given word form.
Constructor Details
#initialize(login = 'anonymous', pass = 'anonymous') ⇒ API
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wlapi/api.rb', line 44 def initialize(login = 'anonymous', pass = 'anonymous') # This hash contains the URLs to the single services. services = {} SERVICES.each { |service| services[service] = "#{ENDPOINT}/#{service}"} # cl short for client. # Dynamically create all the clients and set access credentials. # It can be a very bad idea to instantiate all the clients at once, # we should investigate the typical user behaviour. # If only one service is used in the separate session => rewrite the class! services.each do |key, val| cl_name = "@cl_#{key}" = {:wsdl => val + '?wsdl', :namespaces => {'xmlns:dat' => 'http://datatypes.webservice.wortschatz.uni_leipzig.de', 'xmlns:urn' => val}, :basic_auth => ['anonymous', 'anonymous'], :log => $DEBUG } client = Savon.client() eval("#{cl_name} = client") end # Savon creates very verbose logs, switching off. HTTPI.log = false unless $DEBUG end |
Instance Method Details
#baseform(word) ⇒ Array
Gets the baseform (whatever it is :), not lemma). Returns the lemmatized (base) form of the input word and the POS tag in an array:
api.baseform("Auto") => ["Auto", "N"]
105 106 107 108 109 110 111 112 |
# File 'lib/wlapi/api.rb', line 105 def baseform(word) check_params(word) arg1 = ['Wort', word] answer = query(@cl_Baseform, arg1) get_answer(answer) end |
#check_params(*args) ⇒ Object (private)
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/wlapi/api.rb', line 434 def check_params(*args) m = caller(1).first.match(/^.+`(.*)'$/)[1] num_of_args = self.method(m.to_sym).arity = [] # Arity can be negativ => .abs. case num_of_args.abs when 1 << msg(args[0], m, 'String') unless args[0].is_a?(String) when 2 << msg(args[0], m, 'String') unless args[0].is_a?(String) << msg(args[1], m, 'Numeric') unless args[1].is_a?(Fixnum) when 3 << msg(args[0], m, 'String') unless args[0].is_a?(String) unless args[1].is_a?(String) || args[1].is_a?(Fixnum) << msg(args[1], m, 'String or Numeric') end << msg(args[2], m, 'Numeric') unless args[2].is_a?(Fixnum) end if .any? fail WLAPI::UserError, .join("\n") end end |
#cooccurrences(word, sign, limit = 10) ⇒ Object
Returns statistically significant co-occurrences of the input word.
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/wlapi/api.rb', line 312 def cooccurrences(word, sign, limit = 10) check_params(word, sign, limit) arg1 = ['Wort', word] arg2 = ['Mindestsignifikanz', sign] arg3 = ['Limit', limit] answer = query(@cl_Cooccurrences, arg1, arg2, arg3) get_answer(answer) end |
#cooccurrences_all(word, sign, limit = 10) ⇒ Object
Returns statistically significant co-occurrences of the input word. However, it searches in the unrestricted version of the co-occurrences table as in the Cooccurrences services, which means significantly longer wait times.
327 328 329 330 331 332 333 334 335 336 |
# File 'lib/wlapi/api.rb', line 327 def cooccurrences_all(word, sign, limit = 10) check_params(word, sign, limit) arg1 = ['Wort', word] arg2 = ['Mindestsignifikanz', sign] arg3 = ['Limit', limit] answer = query(@cl_CooccurrencesAll, arg1, arg2, arg3) get_answer(answer) end |
#crossword(word, word_length, limit = 10) ⇒ Object
Attempts to find suitable words given a pattern as word parameter, a word length and the number of words to find at max (limit), e.g. API#crossword('%uto', 4) would return find 24 results and return them as an array: [Auto, Auto, ...]:
api.crossword('%uto') => ["Auto", "Auto", ...]
SQL like syntax is used for pattern (% for an arbitrary string, _ for a single character).
Note: Umlaute will count as one character
– Let’s keep all public method names in English: kreuzwortraetsel => crossword.
369 370 371 372 373 374 375 376 377 378 |
# File 'lib/wlapi/api.rb', line 369 def crossword(word, word_length, limit = 10) check_params(word, word_length, limit) arg1 = ['Wort', word ] arg2 = ['Wortlaenge', word_length] arg3 = ['Limit', limit] answer = query(@cl_Kreuzwortraetsel, arg1, arg2, arg3) get_answer(answer) end |
#domain(word) ⇒ Object
Returns categories for a given input word as an array:
api.domain("Michael") => ["Vorname", "Nachname", "Männername"]
– Is it a good name? all names are in English, but here.. let’s call it domain, not sachgebiet
119 120 121 122 123 124 125 126 |
# File 'lib/wlapi/api.rb', line 119 def domain(word) check_params(word) arg1 = ['Wort', word] answer = query(@cl_Sachgebiet, arg1) get_answer(answer) end |
#experimental_synonyms(word, limit = 10) ⇒ Object
This service delivers an experimental synonyms request for internal tests. – don’t know, if we have to include this service…
244 245 246 247 248 249 250 251 252 |
# File 'lib/wlapi/api.rb', line 244 def experimental_synonyms(word, limit = 10) check_params(word, limit) arg1 = ['Wort', word] arg2 = ['Limit', limit] answer = query(@cl_ExperimentalSynonyms, arg1, arg2) get_answer(answer) end |
#frequencies(word) ⇒ Array
Returns the frequency and frequency class of the input word. Frequency class is computed in relation to the most frequent word in the corpus. The higher the class, the rarer the word:
api.frequencies("Autos") => ["40614", "9"]
91 92 93 94 95 96 97 98 |
# File 'lib/wlapi/api.rb', line 91 def frequencies(word) check_params(word) arg1 = ['Wort', word] answer = query(@cl_Frequencies, arg1) get_answer(answer) end |
#get_answer(doc, mod = '') ⇒ Object (private)
This method extracts valuable data from the XML structure of the soap response. It returns an array with extracted xml text nodes or nil, if the service provided no answer.
421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/wlapi/api.rb', line 421 def get_answer(doc, mod='') result = [] # The path seems to be weird, because the namespaces change incrementally # in the output, so I don't want to treat it here. # A modifier needed because synonyms service provides duplicate values. XPath.each(doc, "//result/*/*#{mod}") do |el| warn(el.text) if $DEBUG result << el.text end result.any? ? result : nil end |
#intersection(word1, word2, limit = 10) ⇒ Object
Returns the intersection of the co-occurrences of the two given words. The result set is ordered according to the sum of the significances in descending order. Note that due to the join involved, this make take some time. – let’s call it intersection, not kookurrenzschnitt is being used INTERN, we need additional credentials
345 346 347 348 349 350 351 352 353 354 |
# File 'lib/wlapi/api.rb', line 345 def intersection(word1, word2, limit = 10) check_params(word1, word2, limit) arg1 = ['Wort 1', word1] arg2 = ['Wort 2', word2] arg3 = ['Limit', limit] answer = query(@cl_Kookurrenzschnitt, arg1, arg2, arg3) get_answer(answer) end |
#left_collocation_finder(word, pos, limit = 10) ⇒ Object
Attempts to find linguistic collocations that occur to the left of the given input word. The parameter ‘Wortart’ accepts four values ‘A, V, N, S’ which stand for adjective, verb, noun and stopword respectively. The parameter restricts the type of words found. It returns an array:
api.left_collocation_finder("Stuhl", "A", 10) =>
["apostolisch", "A", "Stuhl", ...]
300 301 302 303 304 305 306 307 308 309 |
# File 'lib/wlapi/api.rb', line 300 def left_collocation_finder(word, pos, limit = 10) check_params(word, pos, limit) arg1 = ['Wort', word] arg2 = ['Wortart', pos] arg3 = ['Limit', limit] answer = query(@cl_LeftCollocationFinder, arg1, arg2, arg3) get_answer(answer) end |
#left_neighbours(word, limit = 10) ⇒ Object
ok, but results should be filtered
For a given input word, returns statistically significant left neighbours (words co-occurring immediately to the left of the input word).
api.left_neighbours("Auto") => ["geparktes", "Auto", "561", ...]
–
198 199 200 201 202 203 204 205 206 |
# File 'lib/wlapi/api.rb', line 198 def left_neighbours(word, limit = 10) check_params(word, limit) arg1 = ['Wort', word] arg2 = ['Limit', limit] answer = query(@cl_LeftNeighbours, arg1, arg2) get_answer(answer) end |
#msg(arg, meth, cls) ⇒ Object (private)
459 460 461 462 |
# File 'lib/wlapi/api.rb', line 459 def msg(arg, meth, cls) "Argument <#{arg}> for the method <#{meth}> should be a <#{cls}>, "\ "not <#{arg.class}>!" end |
#ngram_references(pattern, limit = 10) ⇒ Object
263 264 265 266 267 268 |
# File 'lib/wlapi/api.rb', line 263 def ngram_references(pattern, limit = 10) arg1 = ['Pattern', pattern] arg2 = ['Limit', limit] answer = query(@cl_NGramReferences, arg1, arg2) # raise(NotImplementedError, 'This method will be implemented in the next release.') end |
#ngrams(pattern, limit = 10) ⇒ Object
255 256 257 258 259 260 |
# File 'lib/wlapi/api.rb', line 255 def ngrams(pattern, limit = 10) arg1 = ['Pattern', pattern] arg2 = ['Limit', limit] answer = query(@cl_NGrams, arg1, arg2) # raise(NotImplementedError, 'This method will be implemented in the next release.') end |
#query(cl, *args) ⇒ Object (private)
Main query method, it invokes the soap engine. It combines all the data to one SOAP request and gets the answer. <args> contains an array [[key1, value1], [key2, value2], [key3, value3]] with keys and values for the soap query.
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/wlapi/api.rb', line 386 def query(cl, *args) # WSDL is disabled since calling the server for wsdl can last too long. v = [] body = { 'urn:objRequestParameters' => { 'urn:corpus' => 'de', 'urn:parameters' => { 'urn:dataVectors' => v } } } # _args_ is an Array of arrays with keys and values # Setting the first argument (usually 'Wort'). # Setting the second argument (usually 'Limit'). # Setting the third argument (no common value). args.each do |key, val| v << {'dat:dataRow' => [key, val]} end begin resp = cl.call(:execute, {:message => body}) rescue => e raise(WLAPI::ExternalError, e) end doc = Document.new(resp.to_xml) warn(doc) if $DEBUG doc end |
#right_collocation_finder(word, pos, limit = 10) ⇒ Object
Attempts to find linguistic collocations that occur to the right of the given input word. The parameter ‘Wortart’ accepts four values ‘A, V, N, S’ which stand for adjective, verb, noun and stopword respectively. The parameter restricts the type of words found. It returns an array:
api.right_collocation_finder("Auto", "V", 10) =>
["Auto", "abfackeln", "V", ...]
281 282 283 284 285 286 287 288 289 290 |
# File 'lib/wlapi/api.rb', line 281 def right_collocation_finder(word, pos, limit = 10) check_params(word, pos, limit) arg1 = ['Wort', word] arg2 = ['Wortart', pos] arg3 = ['Limit', limit] answer = query(@cl_RightCollocationFinder, arg1, arg2, arg3) get_answer(answer) end |
#right_neighbours(word, limit = 10) ⇒ Object
ok, but results should be filtered
For a given input word, returns statistically significant right neighbours (words co-occurring immediately to the right of the input word).
api.right_neighbours("Auto") => ["Auto", "erfaßt", "575", ...]
–
213 214 215 216 217 218 219 220 221 |
# File 'lib/wlapi/api.rb', line 213 def right_neighbours(word, limit = 10) check_params(word, limit) arg1 = ['Wort', word] arg2 = ['Limit', limit] answer = query(@cl_RightNeighbours, arg1, arg2) get_answer(answer) end |
#sentences(word, limit = 10) ⇒ Object
ok, but results should be filtered
Returns sample sentences containing the input word. The return value is an array:
api.sentences("Auto") => ["40808144", "Zweitens der freche,
frische Klang der Hupe
und drittens die hinreißend gestylten 16-Zoll-Felgen,
die es leider nur für dieses Auto gibt.", ...]
–
183 184 185 186 187 188 189 190 191 |
# File 'lib/wlapi/api.rb', line 183 def sentences(word, limit = 10) check_params(word, limit) arg1 = ['Wort', word] arg2 = ['Limit', limit] answer = query(@cl_Sentences, arg1, arg2) get_answer(answer) end |
#similarity(word, limit = 10) ⇒ Object
Returns automatically computed contextually similar words of the input word. Such similar words may be antonyms, hyperonyms, synonyms, cohyponyms or other. Note that due to the huge amount of data any query to this services may take a long time.
api.similarity("Auto") => ["Auto", "Wagen", "26", ...]
231 232 233 234 235 236 237 238 239 |
# File 'lib/wlapi/api.rb', line 231 def similarity(word, limit = 10) check_params(word, limit) arg1 = ['Wort', word] arg2 = ['Limit', limit] answer = query(@cl_Similarity, arg1, arg2) get_answer(answer) end |
#synonyms(word, limit = 10) ⇒ Array
This method searches for synonyms. Returns synonyms of the input word. In other words, this is a thesaurus.
api.synonyms("Auto") => ["Kraftwagen", "Automobil", "Benzinkutsche", ...]
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/wlapi/api.rb', line 164 def synonyms(word, limit = 10) check_params(word, limit) arg1 = ['Wort', word] arg2 = ['Limit', limit] answer = query(@cl_Synonyms, arg1, arg2) # Synonym service provides multiple values, so we take only odd. get_answer(answer, '[position() mod 2 = 1 ]') end |
#thesaurus(word, limit = 10) ⇒ Array
As the Synonyms service returns synonyms of the given input word. However, this first builds a lemma of the input word and thus returns more synonyms:
api.thesaurus("Auto") => ["Auto", "Bahn", "Wagen", "Zug", "Schiff", ...]
150 151 152 153 154 155 156 157 158 |
# File 'lib/wlapi/api.rb', line 150 def thesaurus(word, limit = 10) check_params(word, limit) arg1 = ['Wort', word] arg2 = ['Limit', limit] answer = query(@cl_Thesaurus, arg1, arg2) get_answer(answer) end |
#wordforms(word, limit = 10) ⇒ Array
Returns all other word forms of the same lemma for a given word form.
api.wordforms("Auto") => ["Auto", "Autos"]
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/wlapi/api.rb', line 134 def wordforms(word, limit = 10) check_params(word, limit) # note, it is the only service which requires 'Word', not 'Wort' arg1 = ['Word', word] arg2 = ['Limit', limit] answer = query(@cl_Wordforms, arg1, arg2) get_answer(answer) end |