Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We'll add serialization logic and make our request to the Text Analytics API.
Parsing the Sentiment Score to a Double
If you're not located in the United States, you might need to call an overload of the double.TryParse
method that allows you to pass in the "en-US" culture info in order to successfully parse the string value.
double score;
if (double.TryParse(sentiment.Score, NumberStyles.Any,
CultureInfo.GetCultureInfo("en-US"), out score))
{
newsResult.SentimentScore = score;
}
For more information about how to get and set your current culture in .NET, see the MSDN documentation page for the CultureInfo.CurrentCulture
property.
Formatting Numbers
Standard Numeric Format Strings
Project Ideas
- Build an app that integrates with data from a public API.
- Check out this list of public APIs and find something you're interested in!
- Use other API services from Microsoft Cognitive Services
- Download CSV or JSON datasets from Data.gov and load it into your application
We need to serialize our
request data into JSON and
0:00
then send it to the text
analytics API URL.
0:03
I wanna to show you a quicker way to
use JSON.net to serialize objects
0:07
using the JSON convert class.
0:11
You might have seen it earlier
in the JSON.net documentation.
0:13
String request Json =
0:17
JsonConvert.SerializeObject and
0:20
will send it the sentimentRequest.
0:26
There's one small bug I found
with our sentiment request class.
0:34
Let's go fix it before
we build our request.
0:38
This JSON property needs to be documents.
0:41
Let's go check the JSON response in
the docs to make sure it matches.
0:44
There it is, okay,
let's get back to our method.
0:49
We need to convert our request
based on into a byte array so
0:56
we can pass it to the upload data method.
0:59
Remember when we talked about encoding?
1:02
It's a good practice to use UTFA for
sending data over the Web.
1:04
UTFA is the most compatible encoding.
1:08
Let's convert our string into
a byte array using UTFA.
1:11
To do that, we can use the Encoding
1:16
class by RequestBytes =
1:21
Encoding utf8.GetBytes,
1:26
and we'll pass it.
1:31
Our requestJson.
1:34
Then we can send it to the URL
in our upload data method.
1:37
But our URL is still the old one.
1:43
Let's delete that.
1:46
And we'll put a string placeholder and
paste in request bytes.
1:48
We need to go grab the URL
from the documentation and
1:53
paste it into the upload data method.
1:55
Here it is.
2:01
Copy and paste.
2:02
Now, we can use the json convert
class to deserialize the response.
2:08
But we need a string for that.
2:13
Json convert only deals with strings.
2:14
String sentiments.
2:19
equals encoding dot UTF8 dot Get String.
2:21
And we'll pass it our response.
2:29
And now we can use the deserialize
object method on Json convert.
2:31
Sentiment response equals
Json convert dot deserialize
2:36
And our type parameter
is sentiment response,
2:45
and then we need to pass it
the string of sentiments.
2:51
All right looking good.
2:56
Now we need to do something with
the sentiments from the news headlines.
2:58
We can add a property
to the NewsResult class
3:02
to store the sentiment scores
from our text analytics response.
3:05
Let's take a look at
the response JSON again.
3:09
The score field has decimals, so
which type do you think we should use?
3:14
How about a double?
3:18
It doesn't need to be super precise.
3:20
New search class and
3:25
NewsResult public double
3:28
SentimentScore get set.
3:33
Now let's go back up to
main In our program class.
3:38
Let's see where we can call this method.
3:44
How about right after we
call GetNewsForPlayer?
3:46
SentimentResponse sentimentResponse
3:51
= GetSentimentResponse And
3:58
we'll pass it our list of news results.
4:03
Then we can loop through
the sentiment responses.
4:07
So for each var sentiment in
4:11
sentimentResponse.Sentiments Cause
it's a list,
4:15
and then let's add another loop to
find the correct news result and
4:25
update the score,
foreach (var newsResult in newsResults).
4:30
Will add a check to see if the headline
matches the ID of the sentiment and
4:41
then we'll add the score
to the news result object.
4:46
So if the news result headline.
4:49
because if you remember we put
the Sentiment.id equal to the headline.
4:55
Then we'll add the score to the news
result object equals sentiment.score.
5:04
But we need to parse the string
from sentiment into a double
5:14
We can use good old Double.TryParse.
5:17
We'll need a double to hold the value.
5:20
If Double.TryParse, and
5:23
in we'll pass it sentiment.Score,
and out score.
5:29
And so if that's successful,
5:38
we'll assign newsresult.sentiment
score equals score.
5:41
We can get rid of this line
right here Okay, and since
5:49
we found a match we can break out of this
loop to continue to the next news result.
5:56
Now, let's add the sentiment score to
the text that we write to the console,
6:02
Sentiment Score and we'll pass it a 0.
6:07
Since it's at the beginning, and
then we'll need to change all these that
6:12
follow, One more
6:16
in the list here.
6:22
So we've got 1, 2, 3, 4, and then we'll
need to add the sentiment score On
6:27
the result object, result.SentimentScore,
6:32
and I like to double check the numbers,
we go one, two, three, four.
6:38
One, two, three, four, great.
6:42
Let's run it and
see what we get All right.
6:47
It looks like our sentiments are there,
6:53
but do you think we should
format them a little better?
6:55
The scores are supposed to
represent a percentage.
6:59
We can use a numeric format or
in our string.
7:02
format method.
7:04
The letter P.
7:05
will format the numeric
value into a percentage.
7:06
Stop debugging.
7:10
So a lot like our date formatter,
right here we just do a :P.
7:13
Now let's see what we get.
7:17
All right, great.
7:23
It's a percentage.
7:25
You can find more ways to format
numbers with the link in the notes.
7:27
It's a lot easier to read.
7:31
Let's see, this is two percent,
missing the injured Marco Verratti and
7:34
Claudio [LAUGH]
Struggled,
7:38
yeah, I guess that's a low sentiment.
7:43
Let's look at some other ones here,
Looking for low ones
7:45
These are all pretty high, 9%.
7:54
Looks like somebody had hand surgery.
7:57
Suffered the injury.
8:02
Yeah, that's pretty low.
8:04
All right, great Fantastic job.
8:05
You learned a lot in this course.
8:11
We learned about file IO and how to
read and write files to your computer.
8:13
Then we explored how to parse
text data into dates and numbers.
8:17
We use Json.net for
serialization and then send
8:21
it over the Web Now you can take what
you've learned and build some projects.
8:24
Go find some JSON data that interests
you and build an app around it or
8:29
better yet find an API and
use it to grab data from the web.
8:33
I've put some project ideas for
you in the notes.
8:37
Go forth and build things.
8:40
You need to sign up for Treehouse in order to download course files.
Sign up