[MUSIC PLAYING]
PAUL RUIZ: Hey, all.
I'm Paul Ruiz, a developer advocate with Google.
And this is the ML on Android with media pipe series
where you'll be introduced to a variety of machine learning
concepts and their application on Android
with a media pipe framework.
In this video, you're going to learn some high level concepts
behind machine learning with natural language processing,
or in normal terms, working with text, along with how
to implement those concepts in your Android apps with media
pipe tasks.
In particular, this video will focus on text classification
and text similarity.
While there's a few different, somewhat complex ways
machine learning can work with text for classification,
a simple version of what happens is that words and letters
are represented by numbers through a process called
tokenization.
Then those tokens are processed to associate them
with the likelihood of whatever is trying to be classified.
This could be organizing documents based on their topic,
classifying spam, or determining if a movie
review is positive or negative.
Honestly, going into detail about text classification
could take an entire video series in and of itself.
So fortunately, we've already put one together.
You can find the link to the natural language processing 0
to hero series playlist down in this video's description.
That said, we're in luck when it comes
to applying text classification on Android
as it's fairly straightforward.
First, you will need to import the media pipe tasks
text dependency.
Then you can create your text classifier object,
which is done in a very similar way to vision or audio tasks.
For this example, you can use an off the shelf
model called Mobile BERT to classify text sentiment as
positive or negative.
Once your text classifier is created,
you only need to call the classify function on it
with the text that you want to classify to get your results.
Here you can see our example again,
where you can type in a sentence on Android,
click on the classify button, and then get
the model's confidence for the text being
positive or negative.
Kind of deceptively easy, right?
Where I personally think things get more interesting
is when you want to find out how similar two blocks of text are.
This is called text embedding and is often used
as a part of creating a text searcher,
though we would love to hear any interesting use
cases that you may have for this tool in the comments below.
Text embedding involves tokenizing text and then
processing each token into a single vector embedding
per comparable text block.
Once you have those vectors, you can
use a technique called cosine similarities
to take the cosine of the angle between those vectors
to determine similarity.
Let's take a look at a very simple version of this
to maybe make it a bit more clear.
Let's say you have the sentences "Hello world" and "hey world."
Each word in those sentences is turned into its own vector.
Then each word in each sentence is added together
to create a sentence vector.
You can then find the angle between those two sentence
vectors, which in this case should be about 40 degrees.
And then take the cosine of that angle
to get a value representing the similarity.
While the value is 0.77 for this example,
it can range anywhere from -1 to 1,
with 1 being the exact same sentence and -1 representing
two sentences that are completely different.
You can start using this in your Android apps
in the same way as the other tasks
by first creating your options and then
the media pipe task object, which in this case
is the text embedder.
This example is also using a type
of model trained specifically for text embedding.
After you've created the text embedder,
you can start using it.
You will first need to turn each block of text
into an embedding.
And then you can call the cosine similarity function
to compare them and get a returned value.
And that's it.
If you try this out in our text embedding sample app,
you can see that you get a cosine similarity of 0.77.
If we take the arccosine of that,
we get 0.7 radians or about 40 degrees
in between the vectors of "hey world" and "hello world."
Again, deceptively easy, but that's
what makes media pipe task great for machine learning.
You don't need to worry about every step happening
in the background to perform a task,
but you can get a ton done without a lot of code.
I've included links in the description of this video
to both of these open source media pipe sample
apps, the TensorFlow forums where
you can ask questions and talk to other ML developers,
the Google Developers Discord server where
you can find a media pipe channel for live conversations
with our team, and the official media pipe documentation.
Thanks all for watching and keep an eye out for even more ML
on Android with media pipe videos.
[MUSIC PLAYING]