Esempio n. 1
0
        /// <summary>
        /// Threads
        /// </summary>
        private static void Threads_VisaoLogica()
        {
            List <string> Vocabulario_Arquivo = new List <string>();
            string        ArquivoAtual;

            lock (ReadOnlyObject)   //Bloqueia acesso a pilha Documentos
            {
                //Le e retira o documento da pilha
                ArquivoAtual = Documentos.Peek();
                Documentos.Dequeue();
            }

            try
            {
                StreamReader sr = new StreamReader(ArquivoAtual, Encoding.UTF8); //Novo leitor

                while (!sr.EndOfStream)
                {
                    string[] splitAux = { " ", "<", ">", "/key", "/think" };
                    string[] Linha    = TratamentoDeTexto.TransformacaoLexica(sr.ReadLine()).Split(splitAux, StringSplitOptions.RemoveEmptyEntries); //Divide as palavras

                    foreach (string Palavra in Linha)                                                                                                //Para cada palavra
                    {
                        if (!Vocabulario_Arquivo.Contains(Palavra) && Palavra != "" && Palavra != null)                                              //Verifica se ja existe na lista
                        {
                            Vocabulario_Arquivo.Add(Palavra);                                                                                        //Adiciona na lista
                        }
                    }
                }

                sr.Close();                 //Fecha arquivo

                Vocabulario_Arquivo.Sort(); //Ordena vocabulario

                string[] aux = ArquivoAtual.Split('\\');

                string caminho = Configuracoes.Diretorio_VisaoLogica() + "\\" + aux[aux.Count() - 1];


                //Escreve arquivo de visao logica do arquivo
                File.WriteAllLines(caminho, Vocabulario_Arquivo, Encoding.UTF8);

                ModuloFuncionando = false;
            }
            catch (Exception ex)
            {
                RegistroDeExcessoes.Incluir(new Excessao("Erro ao tentar ler, escrever e processar arquivo", ex.ToString()));
            }

            return;
        }
Esempio n. 2
0
        /// <summary>
        /// Frequencia do termo no documento
        /// </summary>
        private static int Frequencia(int IndiceVoc, int IndiceDoc)
        {
            int cont = 0;

            try
            {
                if (VetorDocumentosBM25 == null)
                {
                    //Vetor de documentos e linhas destes
                    VetorDocumentosBM25 = new string[Arquivos_Info.Count()][];

                    string[] splitAux = { " ", "<", ">", "/key", "/think" };

                    ListaLogs.Add("Carregando arquivos");
                    foreach (FileInfo Arquivo in Arquivos_Info.OrderBy(p => p.Name))    //Para cada arquivo
                    {
                        Porcentagem = (cont * 100 / Arquivos_Info.Count()).ToString();
                        string[] aux = TratamentoDeTexto.TransformacaoLexica(File.ReadAllText(Arquivo.FullName)).Split(splitAux, StringSplitOptions.RemoveEmptyEntries);
                        VetorDocumentosBM25[cont++] = aux;  //Carrega todas as linhas
                    }
                }

                string Termo = Vocabulario[IndiceVoc];

                cont = 0;

                foreach (string item in VetorDocumentosBM25[IndiceDoc])
                {
                    if (item == Termo)
                    {
                        cont++;
                    }
                }
            }
            catch (Exception ex)
            {
                RegistroDeExcessoes.Incluir(new Excessao("Erro ao calcular frequencia do termo", ex.ToString()));

                ModuloFuncionando = false;
            }

            if (cont > 9)
            {
                return(9);
            }
            else
            {
                return(cont);
            }
        }
Esempio n. 3
0
        private static void ConstruirMatrizBM()
        {
            try
            {
                Diretorio_Documentos = new DirectoryInfo(Configuracoes.Diretorio_Documentos());             //Informacoes do diretorio
                Arquivos_Info        = Diretorio_Documentos.GetFiles("*.txt", SearchOption.AllDirectories); //Informacoes dos arquivos

                Vocabulario.AddRange(File.ReadAllLines(Configuracoes.Caminho_Vocabulario()));
                int tamanhovocabulario = Vocabulario.Count;

                BMMatriz = new string[tamanhovocabulario];

                int Contador = 0;
                int Aux      = 0;
                Contador = 0;
                int NumMaximoThreads = 10;


                DirectoryInfo D  = new DirectoryInfo(Configuracoes.Diretorio_ColecaoBM25());
                FileInfo[]    AD = D.GetFiles("*.txt", SearchOption.AllDirectories);

                IndiceBM25 = AD.Count();
                //IndiceBM25 = 2200; tamanhovocabulario = 2201;
                Contador = IndiceBM25;


                if (IndiceBM25 != tamanhovocabulario)
                {
                    Thread[] NovaThread = new Thread[tamanhovocabulario];

                    //Vetor de documentos e linhas destes
                    VetorDocumentosBM25 = new string[Arquivos_Info.Count()][];

                    string[] splitAux = { " ", "<", ">", "/key", "/think" };


                    int cont = 0;
                    ListaLogs.Add("Carregando arquivos");
                    foreach (FileInfo Arquivo in Arquivos_Info.OrderBy(p => p.Name))    //Para cada arquivo
                    {
                        Porcentagem = (cont * 100 / Arquivos_Info.Count()).ToString();
                        string[] aux = TratamentoDeTexto.TransformacaoLexica(File.ReadAllText(Arquivo.FullName)).Split(splitAux, StringSplitOptions.RemoveEmptyEntries);
                        VetorDocumentosBM25[cont++] = aux;  //Carrega todas as linhas
                    }

                    ListaLogs.Add("Iniciando threads");

                    for (int i = IndiceBM25; i < tamanhovocabulario; i++)
                    {
                        Porcentagem = (i * 100 / tamanhovocabulario).ToString() + "%";

                        NovaThread[i]          = new Thread(Threads_ConstruirMatrizBM); //Thread para o arquivo
                        NovaThread[i].Priority = ThreadPriority.Highest;

                        NovaThread[i].Start();       //Inicia thread

                        Aux++;                       //Incrementa contador

                        if (Aux >= NumMaximoThreads) //Controla quantidade de threads lancadas
                        {
                            ListaLogs.Add("Aguardando threads terminarem");
                            for (int j = Contador; j <= i; j++)  //Aguarda cada thread acabar
                            {
                                NovaThread[j].Join();
                            }

                            ListaLogs.Add("Escrevendo buffer");
                            for (int j = Contador; j <= i; j++)  //Aguarda cada thread acabar
                            {
                                File.WriteAllText(Configuracoes.Diretorio_ColecaoBM25() + "\\" + j + ".txt", BMMatriz[j], Encoding.UTF8);
                                BMMatriz[j] = null;
                            }

                            Aux       = 0;
                            Contador += NumMaximoThreads;    //Controle de ultima thread que terminou o processamento

                            ListaLogs.Add("Iniciando threads");
                        }
                    }

                    Porcentagem = "100%";

                    ListaLogs.Add("Aguardando threads terminarem");
                    for (int j = Contador; j < tamanhovocabulario; j++)    //Aguarda as threads restantes acabarem
                    {
                        NovaThread[j].Join();
                    }

                    ListaLogs.Add("Escrevendo buffer");
                    for (int j = Contador; j < tamanhovocabulario; j++)  //Aguarda cada thread acabar
                    {
                        File.WriteAllText(Configuracoes.Diretorio_ColecaoBM25() + "\\" + j + ".txt", BMMatriz[j], Encoding.UTF8);
                        BMMatriz[j] = null;
                    }
                }

                VetorDocumentosBM25 = null;

                ListaLogs.Add("Agrupando vetores BM25");

                try
                {
                    for (int i = 0; i < tamanhovocabulario; i++)
                    {
                        Porcentagem = (i * 100 / tamanhovocabulario).ToString() + "%";

                        BMMatriz[i] = File.ReadAllText(Configuracoes.Diretorio_ColecaoBM25() + "\\" + i + ".txt", Encoding.UTF8);    //Adiciona na matriz
                    }

                    Porcentagem = "100%";
                }
                catch (Exception ex)
                {
                    ListaLogs.Add("Memória insuficiente");

                    BMMatriz = null;

                    RegistroDeExcessoes.Incluir(new Excessao("Erro ao agrupar vetores BM25", ex.ToString()));

                    ModuloFuncionando = false;

                    return;
                }

                try
                {
                    ListaLogs.Add("Escrevendo agrupamento de vetores de BM25");
                    //Escreve todas as linhas
                    File.WriteAllLines(Configuracoes.Caminho_VetoresBM25(), BMMatriz, Encoding.UTF8);
                }
                catch (Exception ex)
                {
                    RegistroDeExcessoes.Incluir(new Excessao("Erro ao escrever vetores BM25", ex.ToString()));

                    ModuloFuncionando = false;
                }
            }
            catch (Exception ex)
            {
                RegistroDeExcessoes.Incluir(new Excessao("Erro ao escrever vetores BM25", ex.ToString()));

                ModuloFuncionando = false;
            }

            return;
        }