예제 #1
0
        public void GravarDados(IEnumerable <ITweet> tweetsPar)
        {
            var tweets = tweetsPar.Distinct().OrderBy(x => x.Id);

            foreach (var item in tweets)
            {
                StrFileIndice         sfi = new StrFileIndice();
                BinarySearchAlgorithm bsa = new BinarySearchAlgorithm();
                if (!bsa.BinarySearchById(item.Id, strNameFileIndice, ref sfi))
                {
                    StrFile strFile = new StrFile();
                    strFile.Id = item.Id;
                    if (item.FullText.Length > 500)
                    {
                        strFile.Mensagem = item.FullText.Substring(0, 500);
                    }
                    else
                    {
                        strFile.Mensagem = item.FullText;
                    }
                    strFile.Data    = item.CreatedAt.ToString("dd/MM/yyyy");
                    strFile.Usuario = item.CreatedBy.Name;
                    strFile.Pais    = item.Place != null && item.Place.Name != null ? item.Place.Name : "";

                    StringBuilder strHash = new StringBuilder();
                    if (item.Hashtags != null && item.Hashtags.Count > 0)
                    {
                        foreach (var hash in item.Hashtags)
                        {
                            strHash.Append(hash);
                        }
                    }
                    if (strHash.ToString().Length > 200)
                    {
                        strFile.HashTags = strHash.ToString().Substring(0, 200);
                    }
                    else
                    {
                        strFile.HashTags = strHash.ToString();
                    }
                    strFile.Elo     = 0;
                    strFile.NewLine = '\n';
                    FileWrite fWrite   = new FileWrite();
                    long      position = fWrite.WriteStructure(strFile, strNameFile);

                    StrFileIndice strFileIndice = new StrFileIndice();
                    strFileIndice.Id      = item.Id;
                    strFileIndice.Posicao = position;
                    strFileIndice.NewLine = '\n';
                    fWrite.WriteStructureIndex(strFileIndice, strNameFileIndice);
                }
            }
        }
예제 #2
0
        private void GenerateAllIndiceHashtags()
        {
            Console.Clear();

            try
            {
                Console.WriteLine("Aguarde. Gerando índice.");

                List <FileStream> listaStream = new List <FileStream>();
                listaStream.Add(new FileStream(strNameFile, FileMode.Open));
                listaStream.Add(new FileStream(strNameIndiceHashtags, FileMode.Open));

                listaStream[0].Seek(0, SeekOrigin.Begin);
                while (listaStream[0].Position < listaStream[0].Length)
                {
                    if (listaStream[0].Position > 0)
                    {
                        listaStream[0].Position += 1;
                    }

                    BinarySearchAlgorithm bsa = new BinarySearchAlgorithm();
                    StrFile  oReturn          = bsa.GetFileValue <StrFile>(listaStream[0]);
                    string[] hashtagsarray    = oReturn.HashTags.Split("#");
                    foreach (string hashtag in hashtagsarray)
                    {
                        if (hashtag.Trim() != "" && (hashtag.ToLower().Contains("rock") || hashtag.ToLower().Contains("metal") || hashtag.ToLower().Contains("punk") || hashtag.ToLower().Contains("hard")))
                        {
                            FileWrite fw         = new FileWrite();
                            bool      achou      = false;
                            string    hashtagAux = "#" + hashtag.Trim();
                            listaStream[1].Seek(0, SeekOrigin.Begin);
                            while (listaStream[1].Position < listaStream[1].Length)
                            {
                                if (listaStream[1].Position > 0)
                                {
                                    listaStream[1].Position += 1;
                                }

                                StrFileIndiceHashtags oReturnInd = bsa.GetFileValue <StrFileIndiceHashtags>(listaStream[1]);
                                if (oReturnInd.Hashtag.Trim() == hashtagAux.Trim())
                                {
                                    achou = true;
                                    for (int i = 1; i < 1000; i++)
                                    {
                                        if (oReturnInd.Ids[i] == 0)
                                        {
                                            oReturnInd.Ids[i] = oReturn.Id;
                                            break;
                                        }
                                    }
                                    long tamanhoBytes = Marshal.SizeOf(typeof(StrFileIndiceHashtags));
                                    if (listaStream[1].Position > 0)
                                    {
                                        listaStream[1].Position -= tamanhoBytes;
                                    }
                                    byte[] buf = fw.GetBytes(oReturnInd);
                                    listaStream[1].Write(buf);
                                }
                            }

                            if (!achou)
                            {
                                StrFileIndiceHashtags sfih = new StrFileIndiceHashtags();
                                sfih.Hashtag = hashtagAux;
                                sfih.Ids     = new long[1000];
                                sfih.Ids[0]  = oReturn.Id;
                                sfih.NewLine = '\n';
                                long position = 0;
                                if (listaStream[1].Length > 0)
                                {
                                    position = listaStream[1].Length + 1;
                                    listaStream[1].Position = position;
                                }
                                byte[] buf = fw.GetBytes(sfih);
                                listaStream[1].Write(buf);
                            }
                        }
                    }
                }

                listaStream[0].Close();
                listaStream[1].Close();
                listaStream[0].Dispose();
                listaStream[1].Dispose();

                Console.WriteLine("Pressione uma tecla para continuar.");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }