Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teste Blog Minuto Seguro - Victor Santana #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
994 changes: 994 additions & 0 deletions Test_Minuto_Seguro/.vs/config/applicationhost.config

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions Test_Minuto_Seguro/Test_Minuto_Seguro.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.852
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test_Minuto_Seguro", "Test_Minuto_Seguro\Test_Minuto_Seguro.csproj", "{3A21B4C1-D3DA-4F19-AA91-1BFFFE885208}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3A21B4C1-D3DA-4F19-AA91-1BFFFE885208}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A21B4C1-D3DA-4F19-AA91-1BFFFE885208}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A21B4C1-D3DA-4F19-AA91-1BFFFE885208}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A21B4C1-D3DA-4F19-AA91-1BFFFE885208}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {44038B9C-A9B2-4A2A-803F-955129F244AD}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Test_Minuto_Seguro.Services;

namespace Test_Minuto_Seguro.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BlogMinutoSeguroController : ControllerBase
{
[HttpGet]
[ProducesResponseType(typeof(Exception), (int)HttpStatusCode.InternalServerError)]
public IActionResult Get()
{
try
{
var xml = ReadXml.GetObjectByXml();
var format = new FormatDataXml();
return Ok(format.GetWordsHighestRelevanceByPostAndTotal(xml));
}
catch (Exception e)
{
return StatusCode(500, new Exception(e.Message));
}
}
}
}
13 changes: 13 additions & 0 deletions Test_Minuto_Seguro/Test_Minuto_Seguro/Interfaces/IFormatDataXml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Test_Minuto_Seguro.Model;

namespace Test_Minuto_Seguro.Interfaces
{
public interface IFormatDataXml
{
BlogMinutoXml GetWordsHighestRelevanceByPostAndTotal(BlogMinutoXml xml);
}
}
72 changes: 72 additions & 0 deletions Test_Minuto_Seguro/Test_Minuto_Seguro/Model/BlogMinutoXml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

namespace Test_Minuto_Seguro.Model
{
[XmlRoot("rss")]
public class BlogMinutoXml
{
[XmlElement(ElementName = "channel")]
public Channel Channel { get; set; }

public BlogMinutoXml()
{
Channel = new Channel();
}

private void ConvertListStringDateToDateTime()
{
Channel.LastBuildDateFormatted = Convert.ToDateTime(Channel.LastBuildDate);
Channel.Item.ConvertAll(x => x.PubDateFormatted = Convert.ToDateTime(x.PubDate));
}

public void OrderByLastListFeeds()
{
ConvertListStringDateToDateTime();
var c = new Channel
{
Item = Channel.Item.OrderByDescending(x => x.PubDateFormatted.Value).ToList()
};
Channel.Item = c.Item;
}
}

public class Channel
{
[XmlElement(ElementName = "title")]
public string Title { get; set; }

[XmlElement(Namespace = "http://www.w3.org/2005/Atom", ElementName = "link")]
public string AtomLink { get; set; }

[XmlElement(ElementName = "link")]
public string Link { get; set; }

[XmlElement(ElementName = "description")]
public string Description { get; set; }

[XmlElement(ElementName = "lastBuildDate")]
public string LastBuildDate { get; set; }

public DateTime? LastBuildDateFormatted { get; set; }

[XmlElement(ElementName = "language")]
public string Language { get; set; }

[XmlElement(Namespace = "http://purl.org/rss/1.0/modules/syndication/", ElementName = "updatePeriod")]
public string SyUpdatePeriod { get; set; }

[XmlElement(Namespace = "http://purl.org/rss/1.0/modules/syndication/", ElementName = "updateFrequency")]
public int SyUpdateFrequency { get; set; }

[XmlElement(ElementName = "generator")]
public string Generator { get; set; }

[XmlElement(ElementName = "item")]
public List<ItemXml> Item = new List<ItemXml>();

}

}
54 changes: 54 additions & 0 deletions Test_Minuto_Seguro/Test_Minuto_Seguro/Model/ItemXml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Test_Minuto_Seguro.Model
{
[XmlRoot("item")]
public class ItemXml
{
[XmlElement(ElementName = "title")]
public string Title { get; set; }

[XmlElement(ElementName = "link")]
public string Link { get; set; }

[XmlElement(ElementName = "comments")]
public string Comments { get; set; }

[XmlElement(ElementName = "pubDate")]
public string PubDate { get; set; }

public DateTime? PubDateFormatted { get; set; }

[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/", ElementName = "creator")]
public string DcCreator { get; set; }

[XmlElement(ElementName = "category")]
public string Category { get; set; }

[XmlElement(ElementName = "guid")]
public string Guid { get; set; }

[XmlElement(ElementName = "description")]
public string Description { get; set; }

[XmlElement(Namespace = "http://purl.org/rss/1.0/modules/content/", ElementName = "encoded")]
public string ContentEncoded { get; set; }

[XmlElement(Namespace = "http://purl.org/rss/1.0/modules/slash/", ElementName = "comments")]
public string SlashComments { get; set; }

[XmlElement(Namespace = "http://wellformedweb.org/CommentAPI/", ElementName = "commentRss")]
public string WfwCommentRss { get; set; }

[XmlIgnore]
public int TotalWord { get; set; }

[XmlIgnore]
public List<Words> CountMostRelevantWords = new List<Words>();

}
}
14 changes: 14 additions & 0 deletions Test_Minuto_Seguro/Test_Minuto_Seguro/Model/Words.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Test_Minuto_Seguro.Model
{
public class Words
{
public string Word { get; set; }

public int Count { get; set; }
}
}
24 changes: 24 additions & 0 deletions Test_Minuto_Seguro/Test_Minuto_Seguro/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Test_Minuto_Seguro
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:58158",
"sslPort": 44371
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/BlogMinutoSeguro",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Test_Minuto_Seguro": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/BlogMinutoSeguro",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
85 changes: 85 additions & 0 deletions Test_Minuto_Seguro/Test_Minuto_Seguro/Services/FormatDataXml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Test_Minuto_Seguro.Interfaces;
using Test_Minuto_Seguro.Model;

namespace Test_Minuto_Seguro.Services
{
public class FormatDataXml : IFormatDataXml
{
private readonly List<string> Prep = new List<string>();

public FormatDataXml()
{
var newPrep = "afora|como|conforme|durante|exceto|feito|fora|mediante|menos|salvo|segundo|senao|tirante|visto|seus|minhas|essa|essas|sob|duns|dumas|uma|" +
"ante|apos|que|contra|desde|entre|para|perante|sobre|tras|abaixo|acerca|acima|alem|antes|inves|lado|par|apesar|suas|meus|esse|uns|ate|das|" +
"atras|atraves|acordo|debaixo|cima|dentro|depois|diante|frente|lugar|graças|perto|causa|entre|umas|minha|esses|com|por|sem|aos|dos|";

Prep = newPrep.Split("|").ToList();
}

public BlogMinutoXml GetWordsHighestRelevanceByPostAndTotal(BlogMinutoXml xml)
{
string text;
var listWords = new List<string>();
var listItemXml = new List<ItemXml>();
var itemXml = new ItemXml();
foreach (var item in xml.Channel.Item.Take(10))
{
text = FilterHtmlChars(item.ContentEncoded);
text = FilterSpaces(text);
text = FilterAccentuation(text);
text = FilterSpecialCharsAndNumbers(text);

listWords = text.Split(" ").ToList();
listWords = FilterArticlesAndPrep(listWords);

itemXml = item;
itemXml.TotalWord = listWords.Count();
itemXml.CountMostRelevantWords = FilterTopTenWords(listWords);
listItemXml.Add(itemXml);
}
xml.Channel.Item = listItemXml;
return xml;
}

private string FilterHtmlChars(string text)
{
Regex regex = new Regex(@"<[^>]*>");
return regex.Replace(text, "").ToLower();
}

private string FilterSpaces(string text)
{
Regex regex = new Regex(@"(\s){1,}");
return regex.Replace(text, " ").ToLower();
}

private string FilterSpecialCharsAndNumbers(string text)
{
Regex regex = new Regex(@"[^a-zç\s]+?");
return regex.Replace(text, String.Empty).Trim();
}
private string FilterAccentuation(string text) => new string(text
.Normalize(NormalizationForm.FormD)
.Where(x => char.GetUnicodeCategory(x) != UnicodeCategory.NonSpacingMark)
.ToArray());

private List<string> FilterArticlesAndPrep(List<string> words) => words.Where(x => x.Length > 2 && !Prep.Contains(x)).ToList();

private List<Words> FilterTopTenWords(List<string> words)
{
var listWord = (from p in words.OrderBy(x => x)
group p by p into g
select new Words { Word = g.Key, Count = g.Count() }
).OrderByDescending(x => x.Count).Take(10).ToList();

return listWord;
}

}
}
24 changes: 24 additions & 0 deletions Test_Minuto_Seguro/Test_Minuto_Seguro/Services/ReadXml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using Test_Minuto_Seguro.Model;

namespace Test_Minuto_Seguro.Services
{
public static class ReadXml
{
public static BlogMinutoXml GetObjectByXml()
{
XmlSerializer serializer = new XmlSerializer(typeof(BlogMinutoXml));
var reader = new StreamReader("feed.xml");
var result = (BlogMinutoXml)serializer.Deserialize(reader);
result.OrderByLastListFeeds();
return result;
}
}
}
Loading