Xamarin.Forms Text Analysis
Hello, In my previous article, I try to wrote about the face recognition and in this article, i will try to make Text Analytics app. Text Analytics can be used for the different purposes. In the application, I will try to make sentiment analytics, language analytics, important entities or people analytics and key phrases of the text.
Firstly, Login into Azure and create a Text Analytics Api, it is easy to do. After the creation of the Api, create Xamarin.Forms blank application. In the app, i create a page which name is TextAnalyticsPage and then i made the design of this page.
<ScrollView>
<StackLayout Margin="10">
<Editor Placeholder="Write Something :)"
HeightRequest="250"
TextColor="Black"
PlaceholderColor="Gray"
x:Name="myEditor"/>
<Button Text="Analyze"
TextColor="White"
BackgroundColor="Green"
Clicked="ButtonAnalyze_Clicked"/>
<ActivityIndicator Color="Green"
VerticalOptions="Center"
HorizontalOptions="Center"
IsVisible="False"
x:Name="myActivitIndicator"/>
<Label x:Name="labelSentiment"
Style="{StaticResource myLabelStyle}"/>
<Label x:Name="labelLanguage"
Style="{StaticResource myLabelStyle}"/>
<ListView ItemsSource="{Binding .}"
HasUnevenRows="True"
IsVisible="False"
x:Name="listEntityDetails">
<ListView.Header>
<Label Text="Entities ; "
Style="{StaticResource myLabelStyle}"
TextColor="Red"/>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0"
Orientation="Horizontal">
<Label Style="{StaticResource myLabelStyle}">
<Label.FormattedText>
<FormattedString>
<Span Text="Name : "/>
<Span Text="{Binding Name}"/>
</FormattedString>
</Label.FormattedText>
</Label>
<Label Style="{StaticResource myLabelStyle}">
<Label.FormattedText>
<FormattedString>
<Span Text="Type : "/>
<Span Text="{Binding Type}"/>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
<StackLayout Grid.Row="1"
Orientation="Horizontal">
<Label Style="{StaticResource myLabelStyle}">
<Label.FormattedText>
<FormattedString>
<Span Text="Sub-Type : "/>
<Span Text="{Binding SubType}"/>
</FormattedString>
</Label.FormattedText>
</Label>
<Label Style="{StaticResource myLabelStyle}">
<Label.FormattedText>
<FormattedString>
<Span Text="OffSet : "/>
<Span Text="{Binding OffSet}"/>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
<StackLayout Grid.Row="2"
Orientation="Horizontal">
<Label Style="{StaticResource myLabelStyle}">
<Label.FormattedText>
<FormattedString>
<Span Text="Length : "/>
<Span Text="{Binding Length}"/>
</FormattedString>
</Label.FormattedText>
</Label>
<Label Style="{StaticResource myLabelStyle}">
<Label.FormattedText>
<FormattedString>
<Span Text="Score : "/>
<Span Text="{Binding Score}"/>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView ItemsSource="{Binding .}"
HasUnevenRows="False"
IsVisible="False"
x:Name="listKeyPhrases">
<ListView.Header>
<StackLayout>
<Label Text="Key Phrases ; "
Style="{StaticResource myLabelStyle}"
TextColor="Red"/>
</StackLayout>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding KeyPhrase}"
Style="{StaticResource myLabelStyle}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ScrollView>

If you copy and paste the code, you will have the same design with me. After the design, we should make the analytics part. For the make analysis, I follow the Microsoft Documentation, you can reach by clicking here. We should add the Microsoft.Azure.CognitiveServices.Language.TextAnalytics nuget package to the our project. Now everything is good to code analysis but before that we should add the Subscription Key and End-Point.
private static readonly string subscriptionKey = "Key"; private static readonly string endpoint = "End-Point";
We should wrote the codes that which store the information and help us to connect with api;
//Storing credentials
class ApiKeyServiceClientCredentials : ServiceClientCredentials
{
private readonly string apiKey;
public ApiKeyServiceClientCredentials(string apiKey)
{
this.apiKey = apiKey;
}
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
request.Headers.Add("Ocp-Apim-Subscription-Key", this.apiKey);
return base.ProcessHttpRequestAsync(request, cancellationToken);
}
}
Codes for the Sentimental Analysis of the Text;
// Sentiment Analyzing of Text
public void SentimentAnalysisExample(TextAnalyticsClient client)
{
//Determining the language
var detectedLanguage = client.DetectLanguage(myEditor.Text);
string languageCode = detectedLanguage.DetectedLanguages[0].Iso6391Name;
var result = client.Sentiment(myEditor.Text, languageCode);
labelSentiment.Text = "Sentiment Score : " + result.Score;
}
Codes for the Detect Language of the Text;
// Language Analyze of Text
public void LanguageDetectionExample(TextAnalyticsClient client)
{
var result = client.DetectLanguage(myEditor.Text);
labelLanguage.Text = "Language : " + result.DetectedLanguages[0].Name;
}
Codes for the detect Entities and People in the Text;
// Entity Analyze of Text
public void EntityRecognitionExample(TextAnalyticsClient client)
{
// Otomatically Language setted to the English
var result = client.Entities(myEditor.Text);
List<Entity> entities = new List<Entity>();
if(result.Entities.Count != 0)
{
listEntityDetails.IsVisible = true;
foreach (var entity in result.Entities)
{
foreach (var match in entity.Matches)
{
entities.Add(new Entity
{
Name = entity.Name,
Length = match.Length.ToString(),
OffSet = match.Offset.ToString(),
Score = match.EntityTypeScore.ToString(),
SubType = entity.SubType,
Type = entity.Type
});
}
}
listEntityDetails.BindingContext = entities;
}
else
{
listEntityDetails.IsVisible = false;
}
}
Codes for the detect Key Phrases of the Text;
// Analyzing of the Key Phrase in the Text
public void KeyPhraseExtractionExample(TextAnalyticsClient client)
{
var result = client.KeyPhrases(myEditor.Text);
List<Phrase> phrases = new List<Phrase>();
if(result.KeyPhrases.Count != 0)
{
listKeyPhrases.IsVisible = true;
foreach (string keyphrase in result.KeyPhrases)
{
phrases.Add(new Phrase { KeyPhrase = keyphrase });
}
listKeyPhrases.BindingContext = phrases;
}
else
{
listKeyPhrases.IsVisible = false;
}
}
And the button event for the call all the functions;
private void ButtonAnalyze_Clicked(object sender, EventArgs e)
{
var credentials = new ApiKeyServiceClientCredentials(subscriptionKey);
TextAnalyticsClient client = new TextAnalyticsClient(credentials)
{
Endpoint = endpoint
};
myActivitIndicator.IsVisible = true;
myActivitIndicator.IsRunning = true;
SentimentAnalysisExample(client);
LanguageDetectionExample(client);
EntityRecognitionExample(client);
KeyPhraseExtractionExample(client);
myActivitIndicator.IsVisible = false;
myActivitIndicator.IsRunning = false;
}
After the wroting all the codes above, Text Analysis will work perfectly. For your question, you can reach me via e-mail or comments.
