示例#1
0
文件: Token.cs 项目: pedroccrl/Tcc
        public static List <Token> Tokenize(string texto)
        {
            var list = new List <Token>();

            texto = StringAcento.RemoverAcento(texto.ToLower());
            var  original = texto;
            bool temPrep  = false;

            for (int i = 0; i < original.Length; i++)
            {
                foreach (var prep in preposicoes)
                {
                    var preposicao = $" {prep} ";
                    int index      = texto.IndexOf(preposicao);
                    if (index != -1)
                    {
                        var token = new Token
                        {
                            Inicio = index,
                            Fim    = index + prep.Length,
                            Tipo   = TipoToken.Preposicao
                        };
                        list.Add(token);
                        temPrep = true;
                        texto   = texto.Substring(index + prep.Length);
                        i       = index + prep.Length;
                    }
                }
                if (!temPrep)
                {
                    break;
                }
            }
            return(list);
        }
示例#2
0
        static EntidadeRelacionada ReconhecerEntidadeRelacionada(Entidade entidade, string texto)
        {
            texto = StringAcento.RemoverAcento(texto.ToLower());
            var original = texto;

            if (entidade.Tipo == TipoEntidade.Local)
            {
                var sub = texto.Substring(entidade.Fim);
                if (entidade.Nome == "bairro" || entidade.Nome == "ruas" || entidade.Nome == "orla" || entidade.Nome == "praia")
                {
                    foreach (var bairro in Bairros)
                    {
                        var bairro_nome = StringAcento.RemoverAcento(bairro.Nome.ToLower());
                        int index       = sub.IndexOf(bairro_nome);
                        if (index != -1)
                        {
                        }
                    }
                }
                else if (entidade.Nome == "rua")
                {
                }
            }

            return(null);
        }
示例#3
0
        public static List <Entidade> ReconhecerEntidades(string texto)
        {
            var list = new List <Entidade>();

            texto = StringAcento.RemoverAcento(texto.ToLower());
            var  original = texto;
            bool tem      = false;

            // procura local no texto
            for (int i = 0; i < original.Length; i++)
            {
                foreach (var lcl in locais)
                {
                    var local = $" {StringAcento.RemoverAcento(lcl)} ";
                    int index = texto.IndexOf(local);
                    if (index != -1)
                    {
                        index++;
                        var entidade = new Entidade
                        {
                            Inicio = i + index,
                            Fim    = i + index + lcl.Length,
                            Tipo   = TipoEntidade.Local,
                            Nome   = lcl
                        };
                        list.Add(entidade);
                        tem   = true;
                        texto = texto.Substring(index + lcl.Length);
                        i     = index + lcl.Length;
                    }
                }
                if (!tem)
                {
                    break;
                }
            }
            // procura onde a frase que fala do local termina
            for (int i = 0; i < list.Count; i++)
            {
                var ent      = list[i];
                var ent_prox = default(Entidade);
                if (i + 1 < list.Count)
                {
                    ent_prox = list[i + 1];
                }

                var sub = original.Substring(ent.Fim);
                if (ent_prox != null)
                {
                    sub = sub.Substring(0, ent_prox.Inicio - ent.Fim);
                }
                for (int j = 0; j < sub.Length; j++)
                {
                    var c = sub[j];
                    switch (c)
                    {
                    case '.':
                    case '?':
                    case '!':
                    case ':':
                        ent.FimFrase = ent.Fim + j;
                        j            = sub.Length;
                        break;

                    default:
                        break;
                    }
                }
            }


            return(list);
        }
示例#4
0
文件: Correios.cs 项目: pedroccrl/Tcc
        public static async Task <Bairro> GetLogradouroPorBairroAsync(Cidade cidade, string bairro, IStatus status = null)
        {
            var b = new Bairro {
                Nome = bairro, Logradouros = new List <Logradouro>()
            };
            await Task.Run(async() =>
            {
                var http = new HttpClient();

                http = new HttpClient(new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                    AllowAutoRedirect      = true,
                });

                http.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063");
                http.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
                http.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
                http.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
                http.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "pt-BR,pt;q=0.5");
                var bairroEncoded = StringAcento.RemoverAcento(bairro.ToLower().Trim());
                //var bairroEncoded = Uri.EscapeUriString(bairro.Trim());
                for (int i = 1; i < 1000; i += 50)
                {
                    var forms = new List <KeyValuePair <string, string> >();
                    forms.Add(new KeyValuePair <string, string>("Bairro", bairroEncoded));
                    forms.Add(new KeyValuePair <string, string>("Localidade", cidade.Nome));
                    forms.Add(new KeyValuePair <string, string>("UF", cidade.UF.ToUpper()));
                    forms.Add(new KeyValuePair <string, string>("qtdrow", "50"));
                    forms.Add(new KeyValuePair <string, string>("pagini", $"{i}"));
                    forms.Add(new KeyValuePair <string, string>("pagfim", $"{i + 50}"));

                    status?.Escrever($"Requisitando {bairro.Trim()}, Pagina: {i}");
                    var resposta = await http.PostAsync("http://www.buscacep.correios.com.br/sistemas/buscacep/resultadoBuscaLogBairro.cfm", new FormUrlEncodedContent(forms));


                    var resultado = await resposta.Content.ReadAsStringAsync();
                    status?.Escrever($"Resposta tamanho {resultado.Length}");
                    HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(resultado);


                    int r     = 0;
                    var nodes = doc.DocumentNode.Descendants("table").ToList();
                    var c     = nodes.Count();
                    if (nodes == null)
                    {
                        break;
                    }

                    try
                    {
                        var div = doc.DocumentNode.Descendants("div")?.Where(e => e.Attributes["class"].Value == "ctrlcontent").First()?.Descendants("p")?.First()?.InnerText;
                        if (div == "BAIRRO/LOGRADOURO NAO ENCONTRADO.")
                        {
                            break;
                        }
                        status?.Escrever(div);
                    }
                    catch (Exception)
                    {
                        break;
                    }



                    foreach (HtmlNode table in nodes)
                    {
                        Debug.WriteLine("Found: " + table.Id);
                        status?.Escrever($"Tabela encontrada");
                        foreach (HtmlNode row in table.Descendants("tr").ToList())
                        {
                            if (r++ == 0)
                            {
                                continue;
                            }
                            var logra = new Logradouro();
                            var linha = row.Descendants("td").ToList();
                            //if (linha.Count != 4)
                            //{
                            //    i = 2000;
                            //    break;
                            //}
                            //status?.Escrever($"{linha.Count} colunas encontradas");
                            try
                            {
                                logra.Nome = WebUtility.HtmlDecode(linha[0].InnerText);
                                logra.Cep  = WebUtility.HtmlDecode(linha[3].InnerText);
                                b.Logradouros.Add(logra);
                                status?.Escrever($"Logradouro: {logra.Nome} encontrado");
                            }
                            catch (Exception e)
                            {
                            }
                        }
                    }
                    status?.Escrever($"Aguardando 10 segundos");
                    await Task.Delay(1000 * 10);
                }
            });

            return(b);
        }