Class: SynonymScrapper::Nltk

Inherits:
Object
  • Object
show all
Defined in:
lib/synonym_scrapper/nltk.rb

Overview

Connector and requester of python’s NLTK

Instance Method Summary collapse

Instance Method Details

#synonyms(word, options = {}) ⇒ Object

Obtain synonyms of a word from the NLTK.

Makes a call to a python script and parses its results.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/synonym_scrapper/nltk.rb', line 15

def synonyms(word, options = {})

	begin
		nltk_response = `python3 #{__dir__}/nltk_parser.py "#{word}"`
		related_words = JSON.parse(nltk_response)["relations"][word]

		synonyms = Array.new
		related_words.each do |synonym|
			synonyms.push({
				word: synonym["word"],
				score: synonym["score"]
			})
		end
		return synonyms
	rescue => e
		puts e
		return []
	end
end