All threads / How to use the Sentimental gem

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to use the Sentimental gem

Sascha M. asked in Gems / Libraries

Hi there,

I want to use the Sentimental gem to automatically check if an article is positive negative or neutral. This gem here: https://github.com/7compass/sentimental works fine in the Rails console. But I really have no clue how to use it in my scaffolded articles model - or inside the views. Do you have any ideas?

Hey Sascha! I forgot to write you back over email, but I'm planning on doing an episode on Thursday on this for you. :D

Are you interested in the simple "positive" or "negative" response or the percentage scores?

Hi Chris,

how awesome is that?! I'm very excited! Actually I'm interested in both the postive/negative and percentage scores.

Cheers,
Sascha

Really really good episode! This is EXACTLY what I needed :P

How would you automate the app, so that you have a search field inside your rails views so that you don't have to go into the console each time?

Whoo! :)

So if you want to analyze user input, you can just setup your models like normal with your controllers. The callback will automatically analyze it.

For example, you mentioned articles. If you just have your normal scaffold or controller, all you'll have to do is add the callback to your model and the attributes and they will get automatically analyzed anytime the text is changed. Then you can just access the sentiment and score attributes like you would with any other attribute. That's it!

Ok, got it. Is there an easy way to do the following:

Let's say I want to get the sentiment of Game tweets and I have a Game model in which are many Games entered, e.g. Hitman.

Is it possible to look up the Tweets as soon someone views the page and handover the title "Hitman" to the search query automatically? Also: How can I make sure that Tweets are not getting added twice .first_or_initialize? Thanks in advance!

That could be slow, or go over the Twitter API limits, so you'd want to do that in a background job for sure. In that case, I would just load the local copy of Tweets for that game.

When you do a search and save a Tweet, you can save the keyword you searched and link it to the game, and you can also save the tweet_id so that you can use that on the first_or_initialize or however you'd like to do that.

Just FYI:

I did the redundancy check like this:

client.search(query).each do |tweet|
 find_or_create_by(body: tweet.text, tweet_id: tweet.id)
end
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,629+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.