Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Last active June 18, 2019 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/6615985a85d5adaf0cbbd2a157e87236 to your computer and use it in GitHub Desktop.
Save icebeam7/6615985a85d5adaf0cbbd2a157e87236 to your computer and use it in GitHub Desktop.
Taller UTN
docker run --rm -it -p 5000:5000 --memory 2g --cpus 1 \
mcr.microsoft.com/azure-cognitive-services/sentiment \
Eula=accept \
Billing= https://brazilsouth.api.cognitive.microsoft.com/text/analytics/v2.0\
ApiKey=a5ee0d2aa4e94804aa08f2279668cb70
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AnalizadorTexto.Paginas.PaginaAnalizador">
<ContentPage.Content>
<StackLayout BackgroundColor="White" Padding="10">
<Label Text="Texto" TextColor="Blue" FontSize="Large"/>
<Entry x:Name="MessageEntry" TextColor="White" Placeholder="Texto..."
PlaceholderColor="LightGray" BackgroundColor="Black" FontSize="Medium"/>
<Button x:Name="AnalyzeButton" Text="Analizar" TextColor="White"
BackgroundColor="#0072BD" FontSize="Medium" Clicked="AnalyzeButton_Clicked"/>
<ActivityIndicator x:Name="indicator" Color="Green" VerticalOptions="Center" HorizontalOptions="Center"/>
<Label Text="Score" FontSize="Large" TextColor="Gray"/>
<Label x:Name="ScoreLabel" Text="0.00" TextColor="Black"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using AnalizadorTexto.Servicios;
namespace AnalizadorTexto.Paginas
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PaginaAnalizador : ContentPage
{
public PaginaAnalizador()
{
InitializeComponent();
}
private async void AnalyzeButton_Clicked(object sender, EventArgs e)
{
Loading(true);
var sentiment = await ServicioText.AnalyzeText(MessageEntry.Text);
if (sentiment != null)
{
ScoreLabel.Text = sentiment.Score.ToString("N4");
}
Loading(false);
}
void Loading(bool mostrar)
{
indicator.IsEnabled = mostrar;
indicator.IsRunning = mostrar;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace AnalizadorTexto.Modelos
{
public class DocumentRequest
{
public string Id { get; set; }
public string Text { get; set; }
public string Language { get; set; }
}
}
--------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace AnalizadorTexto.Modelos
{
public class InnerError
{
public string RequestId { get; set; }
}
public class Error
{
public string Code { get; set; }
public InnerError InnerError { get; set; }
public string Message { get; set; }
}
public class Document
{
public string Id { get; set; }
public float Score { get; set; }
}
public class DocumentResponse
{
public Document[] Documents { get; set; }
public Error[] Errors { get; set; }
}
}
-------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using AnalizadorTexto.Modelos;
using Newtonsoft.Json;
namespace AnalizadorTexto.Servicios
{
public static class ServicioText
{
private static readonly HttpClient client =
CreateHttpClient();
public async static Task<Document> AnalyzeText(string message)
{
var documents = PrepareRequest(message);
var content = new StringContent(documents);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.PostAsync("text/analytics/v2.0/sentiment", content);
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObjet<DocumentResponse>(json);
return result.documents.FirstOrDefault();
}
return default(Document);
}
private static HttpClient CreateHttpClient()
{
var client = new HttpClient();
client.BaseAddress = new Uri("http://192.168.99.100:5000/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
private static string PrepareRequest(string message)
{
var wrapper = new
{
documents = new List<DocumentRequest>()
{
new DocumentRequest()
{
Id = "1",
Language = "en",
Text = message
}
}
};
return JsonConvert.SerializeObject(wrapper);
}
}
}
-------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment