Author Topic: Check connotation of language with machine learning API!  (Read 4581 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Check connotation of language with machine learning API!
« on: July 22, 2020, 09:29:34 am »
Use this demo here to type a line of text and send it to the sentim API machine learning AI to see if a phrase is negative, neutral, or positive in connotation. Many thanks to Luke for his assistance in understanding cURL strings.
This demo is very limited. The output from the AI actually contains more data than "negative", "positive", or "neutral". It even contains how negative or how positive something is. You could pass back that value and add more IF statements or whatever that could detect whether something is very nice or very mean.

Here are some screenshots:
 
sentimpositive.png
 
sentimnegative.png


And the source code:
Code: QB64: [Select]
  1.  
  2. DIM result AS STRING
  3.  
  4. LINE INPUT "Please input what text you would like the API to check: ", text
  5.  
  6. result = CheckConnotation(text)
  7. IF result = "negative" THEN
  8.     PRINT "That's not a nice thing to say."
  9. ELSEIF result = "positive" THEN
  10.     PRINT "Positivity is key."
  11. ELSEIF result = "neutral" THEN
  12.     PRINT "Meh."
  13.     PRINT "Well, that seems to not work. Check your connection or print the string that is being SHELLed to make sure the syntax isn't awry."
  14.  
  15.  
  16. FUNCTION CheckConnotation$ (Text AS STRING)
  17.     DIM sentim AS STRING
  18.     SHELL _HIDE "curl -XPOST -H " + CHR$(34) + "Accept: application/json" + CHR$(34) + " -H " + CHR$(34) + "Content-type: application/json" + CHR$(34) + " -d " + CHR$(34) + "{\" + CHR$(34) + "text\" + CHR$(34) + ": \" + CHR$(34) + Text$ + "\" + CHR$(34) + "}" + CHR$(34) + " " + CHR$(34) + "https://sentim-api.herokuapp.com/api/v1/" + CHR$(34) + " -o sentimapirequest"
  19.  
  20.     IF _FILEEXISTS("sentimapirequest") THEN
  21.         C = FREEFILE
  22.         OPEN "sentimapirequest" FOR BINARY AS #C
  23.         IF LOF(C) <> 0 THEN
  24.             sentim = SPACE$(LOF(C))
  25.             GET #C, , sentim
  26.             CLOSE #C
  27.             sentim = MID$(sentim, INSTR(sentim, "type" + CHR$(34) + ":") + 7)
  28.             sentim = LEFT$(sentim, INSTR(sentim, CHR$(34)) - 1)
  29.             CheckConnotation = sentim
  30.             KILL "sentimapirequest"
  31.             EXIT FUNCTION
  32.         ELSE
  33.             CLOSE #C
  34.             CheckConnotation = ""
  35.             KILL "sentimapirequest"
  36.             EXIT FUNCTION
  37.         END IF
  38.     ELSE
  39.         CheckConnotation = ""
  40.         EXIT FUNCTION
  41.     END IF
« Last Edit: July 22, 2020, 09:49:58 am by SpriggsySpriggs »
Shuwatch!

Offline loudar

  • Newbie
  • Posts: 73
  • improve it bit by bit.
    • View Profile
Re: Check connotation of language with machine learning API!
« Reply #1 on: July 22, 2020, 02:48:41 pm »
Got this. What's wrong with it?

 
Anmerkung 2020-07-22 204817.png
Check out what I do besides coding: http://loudar.myportfolio.com/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Check connotation of language with machine learning API!
« Reply #2 on: July 22, 2020, 03:17:08 pm »
Worked for me, a little mod:
Code: QB64: [Select]
  1.  
  2. DIM result AS STRING
  3.  
  4.     LINE INPUT "Please input what text you would like the API to check: ", text
  5.     IF text = "" THEN EXIT DO
  6.     result = CheckConnotation(text)
  7.     IF result = "negative" THEN
  8.         PRINT "That's not a nice thing to say."
  9.     ELSEIF result = "positive" THEN
  10.         PRINT "Positivity is key."
  11.     ELSEIF result = "neutral" THEN
  12.         PRINT "Meh."
  13.     ELSE
  14.         PRINT "Well, that seems to not work. Check your connection or print the string that is being SHELLed to make sure the syntax isn't awry."
  15.     END IF
  16.     PRINT: PRINT " 5 sec delay..."
  17.     _DELAY 5
  18.  
  19. FUNCTION CheckConnotation$ (Text AS STRING)
  20.     DIM sentim AS STRING
  21.     SHELL _HIDE "curl -XPOST -H " + CHR$(34) + "Accept: application/json" + CHR$(34) + " -H " + CHR$(34) + "Content-type: application/json" + CHR$(34) + " -d " + CHR$(34) + "{\" + CHR$(34) + "text\" + CHR$(34) + ": \" + CHR$(34) + Text$ + "\" + CHR$(34) + "}" + CHR$(34) + " " + CHR$(34) + "https://sentim-api.herokuapp.com/api/v1/" + CHR$(34) + " -o sentimapirequest"
  22.  
  23.     IF _FILEEXISTS("sentimapirequest") THEN
  24.         C = FREEFILE
  25.         OPEN "sentimapirequest" FOR BINARY AS #C
  26.         IF LOF(C) <> 0 THEN
  27.             sentim = SPACE$(LOF(C))
  28.             GET #C, , sentim
  29.             CLOSE #C
  30.             sentim = MID$(sentim, INSTR(sentim, "type" + CHR$(34) + ":") + 7)
  31.             sentim = LEFT$(sentim, INSTR(sentim, CHR$(34)) - 1)
  32.             CheckConnotation = sentim
  33.             KILL "sentimapirequest"
  34.             EXIT FUNCTION
  35.         ELSE
  36.             CLOSE #C
  37.             CheckConnotation = ""
  38.             KILL "sentimapirequest"
  39.             EXIT FUNCTION
  40.         END IF
  41.     ELSE
  42.         CheckConnotation = ""
  43.         EXIT FUNCTION
  44.     END IF
  45.  
  46.  

"meh"  LOL
nice or not.PNG
* nice or not.PNG (Filesize: 24.8 KB, Dimensions: 688x512, Views: 213)

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Check connotation of language with machine learning API!
« Reply #3 on: July 22, 2020, 04:59:05 pm »
Got this. What's wrong with it?
@loudar I'm not taking into account things like apostrophes or question marks. I'm new to JSON formats still and command prompt is still weird about characters and delimiters. You might have to escape the character. Nope, that's not it because bplus didn't change my function at all and was able to use both apostrophes and question marks. Maybe it's a connection issue? Try visiting the site in your browser and see if you can reach it. https://sentim-api.herokuapp.com
« Last Edit: July 22, 2020, 05:06:22 pm by SpriggsySpriggs »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Check connotation of language with machine learning API!
« Reply #4 on: July 22, 2020, 05:07:38 pm »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Check connotation of language with machine learning API!
« Reply #5 on: July 22, 2020, 09:37:38 pm »
Perhaps this weekend I'll look into using the "polarity" value in the result JSON that shows just how positive or negative a statement is. Can make for more flexibility.
Shuwatch!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Check connotation of language with machine learning API!
« Reply #6 on: July 22, 2020, 11:39:53 pm »
Nice... @SpriggsySpriggs
Quote
Please input what text you would like the API to check: you should kill yourself
Meh.
How this can be neutral? ROFL
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Check connotation of language with machine learning API!
« Reply #7 on: July 23, 2020, 04:28:18 am »
Nice...How this can be neutral? ROFL

A lot of these type programs are first generation apps.  All they can do is look at the surface of what is written, and attempt to rate things based solely on the words used.  Much like profanity filters, they lack the capacity to understand user context.

Two friends talking:

Friend One: "Man, I can't believe I failed my class!  I thought the exam was tomorrow, not today.  I completely missed it."   
Friend Two (sarcastically): "You're such a genius!"

Those statements would be neutral for friend one, but positive for friend two.  Normally, calling someone a genius is positive reinforcement, but in this case it's sarcastic.  These programs react to words used, not the context of which the words are used.  They're cute to play with, and maybe someday they'll inspire someone to go the extra mile and sort out the nuances of subtle context, but as they are now, I wouldn't depend on them for any sort of diagnosis for my mental state or anything.  (And I have seen some people who attempt to do that with journals which grade their writings.  "You are 87% positive in your daily writings...")
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Check connotation of language with machine learning API!
« Reply #8 on: July 23, 2020, 08:03:51 am »
Nice... @SpriggsySpriggsHow this can be neutral? ROFL
@Ashish That's because I'm only grabbing the final result from the JSON file. There is more information like polarity that shows just how much it sways either way. I just chose neutral, negative, or positive as the final answer.
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Check connotation of language with machine learning API!
« Reply #9 on: July 23, 2020, 08:13:44 am »
A lot of these type programs are first generation apps.  All they can do is look at the surface of what is written, and attempt to rate things based solely on the words used.  Much like profanity filters, they lack the capacity to understand user context.
@SMcNeill ,
This particular API is supposedly machine learning so as time goes on I reckon it will become more accurate in determining meaning of words. Similar to Cleverbot in how it uses user input and responses to generate more fluid dialog and as time goes on it has more natural responses.

This one is probably not used nearly as much as the webpage for it is rather sparse in both documentation as well as design quality. The profanity filter only recognized words and filtered. The more text you send to this API in a string the better the result. Short sentences like "I hate you" or "I love you" are cut and dry whereas a paragraph requires more scanning to get more context. The website example shows using a paragraph as the passed string and returning back a value. It can analyze many sentences at once.
Shuwatch!

Offline loudar

  • Newbie
  • Posts: 73
  • improve it bit by bit.
    • View Profile
Re: Check connotation of language with machine learning API!
« Reply #10 on: July 23, 2020, 12:16:00 pm »
@loudar I'm not taking into account things like apostrophes or question marks. I'm new to JSON formats still and command prompt is still weird about characters and delimiters. You might have to escape the character. Nope, that's not it because bplus didn't change my function at all and was able to use both apostrophes and question marks. Maybe it's a connection issue? Try visiting the site in your browser and see if you can reach it. https://sentim-api.herokuapp.com

Site works, but very slow.
Check out what I do besides coding: http://loudar.myportfolio.com/

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Check connotation of language with machine learning API!
« Reply #11 on: July 23, 2020, 12:55:42 pm »
@loudar Then it is just your connection. It's probably just timing out.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Check connotation of language with machine learning API!
« Reply #12 on: July 23, 2020, 04:56:33 pm »
Quote
This one is probably not used nearly as much as the webpage for it is rather sparse in both documentation as well as design quality. The profanity filter only recognized words and filtered. The more text you send to this API in a string the better the result. Short sentences like "I hate you" or "I love you" are cut and dry whereas a paragraph requires more scanning to get more context. The website example shows using a paragraph as the passed string and returning back a value. It can analyze many sentences at once.

Can't do that without feedback and it's not getting that from us.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Check connotation of language with machine learning API!
« Reply #13 on: July 23, 2020, 06:12:33 pm »
@bplus Oh well. I'm not really going to be using this in any real world applications. It's mostly to show usage of CURL and APIs. The only APIs I plan on using elsewhere are the Weather API and all the data validation APIs.
Shuwatch!