text_worker.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. class TextWorker
  2. @@wl = WhatLanguage.new(:all)
  3. @@tgr = EngTagger.new
  4. def self.post(url, path, body={})
  5. uri = URI.parse(url)
  6. http = Net::HTTP.new(uri.host, uri.port)
  7. request = Net::HTTP::Post.new(path)
  8. request.add_field('Content-Type', 'application/json')
  9. request.body = body.to_json
  10. response = http.request(request)
  11. response.body
  12. end
  13. def self.analyze_text(text)
  14. if text
  15. hash = {}
  16. tagged = @@tgr.add_tags(text)
  17. hash[:word_list] = @@tgr.get_words(text)
  18. hash[:nouns] = @@tgr.get_nouns(tagged)
  19. hash[:proper_nouns] = @@tgr.get_proper_nouns(tagged)
  20. hash[:past_tense_verbs] = @@tgr.get_past_tense_verbs(tagged)
  21. hash[:adjectives] = @@tgr.get_adjectives(tagged)
  22. hash[:noun_phrases] = @@tgr.get_noun_phrases(tagged)
  23. hash[:language] = @@wl.language(text)
  24. hash[:languages_ranked] = @@wl.process_text(text)
  25. hash[:profanity] = SadPanda.polarity (text)
  26. hash[:emotion] = SadPanda.emotion (text)
  27. hash[:reading_level] = Odyssey.coleman_liau (text)
  28. return hash
  29. else
  30. return false
  31. end
  32. end
  33. def self.analyze_entities(text)
  34. if text
  35. entities = @@ner.perform(text)
  36. if entities
  37. return entities
  38. else
  39. return false
  40. end
  41. end
  42. end
  43. def self.analyze_name(first_name, last_name)
  44. if first_name and last_name
  45. hash = {}
  46. hash[:gender] = Guess.gender(first_name.to_s.humanize)
  47. hash[:ethnicity] = $races[last_name.to_s.upcase]
  48. hash[:name] = [first_name, last_name].join(" ")
  49. return hash
  50. else
  51. return false
  52. end
  53. end
  54. end