Esempio n. 1
0
        public void ProcessTweet(DbTweet tw)
        {
            try
            {
                String[] words = tw.Text.Split(new char[] { ' ', '.', ',', '?', '!', '¿', '¡', ';', ':', '-', '\n', '(', ')', '"', '\'', '[', ']', '+', '|', '“', '”', '…', '*' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (String w in words)
                {
                    if (w.Contains('/'))
                    {
                        continue;                  // Ignoro urls
                    }
                    if (w.Length == 1)
                    {
                        continue;
                    }
                    if (w.Contains('@')) // Sólo guardo @alias de los topics
                    {
                        bool cont = true;
                        IEnumerable <String> topicUsers = from DbTopic t in db
                                                          select t.Alias[1];

                        foreach (String u in topicUsers)
                        {
                            if (w.ToLower() == u.ToLower())
                            {
                                cont = false;
                            }
                        }

                        if (cont)
                        {
                            continue;
                        }
                    }

                    /*
                     * IEnumerable<DbWord> res = from DbWord x in db
                     *                     where x.Name.Equals(w.ToLower())
                     *                     select x;*/

                    int wordValue = 0;
                    if (Core.words.ContainsKey(w.ToLower()))
                    {
                        wordValue = Core.words[w.ToLower()]; // En lugar de res.First, etc.
                    }
                    bool stopword = false;
                    if (wordValue == 2) // Caso stopword
                    {
                        stopword = true;
                        //Console.Out.WriteLine("Stopword: " + w);
                    }
                    else if (wordValue == 1)
                    {
                        tw.PosValue++;                      // Palabra positiva
                    }
                    else if (wordValue == -1)
                    {
                        tw.NegValue++;                       // Palabra negativa
                    }
                    if (!stopword && w != "")
                    {
                        // Se agrega el término
                        if (!tw.Terms.Contains(w.ToLower()))
                        {
                            tw.Terms.Add(w.ToLower());
                        }

                        if (TermFreq.ContainsKey(w.ToLower()))
                        {
                            TermFreq[w.ToLower()]++;
                        }
                        else
                        {
                            TermFreq[w.ToLower()] = 1;
                        }
                    }
                }

                db.Store(tw);
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("ProcessTweet: Error analizando tweet.");
                Console.Out.WriteLine(e.Message + "\n" + e.StackTrace);
            }

            cantTweets++;
        }
Esempio n. 2
0
        public void ListenTo(DbTopic t)
        {
            Action <ITweet> act = (arg) =>
            {
                if (cantTweets >= maxCantTweets)
                {
                    myStream.StopStream();

                    return;
                }

                else
                {
                    Console.Out.Write(t.Id + " ");
                }

                if (arg.Language != Tweetinvi.Core.Enum.Language.Spanish)
                {
                    //Console.Out.WriteLine("!!! No español: " + arg.Text);
                    return;
                }

                IEnumerable <DbTweet> existente = from DbTweet tw in db
                                                  where tw.Id == arg.Id
                                                  select tw;

                // Para modificar un tweet ya almacenado (ej. cuando ya mencionaba a otro tópico) en vez de crear otro distinto.
                DbTweet n;
                if (existente != null && existente.Count() == 0)
                {
                    n = new DbTweet(arg.Id, arg.Text, arg.CreatedBy.UserIdentifier.ScreenName, arg.CreatedAt, DateTime.Now, arg.RetweetCount);
                }
                else
                {
                    n = existente.First();
                }

                if (arg.Coordinates != null)
                {
                    Console.Out.WriteLine("> En: " + arg.Coordinates.Latitude + ", " + arg.Coordinates.Longitude);
                    n.Coord = new Tuple <float, float>((float)arg.Coordinates.Latitude, (float)arg.Coordinates.Longitude);
                }
                else if (arg.Place != null)
                {
                    Console.Out.WriteLine("> En: " + arg.Place.FullName);
                    n.Coord = new Tuple <float, float>((float)arg.Place.BoundingBox.Coordinates[0].Latitude, (float)arg.Place.BoundingBox.Coordinates[0].Longitude);
                    Console.Out.WriteLine("> O sea: " + n.Coord.Item1 + ", " + n.Coord.Item2);
                }

                Console.Out.Write(n.PosValue + "/" + n.NegValue + " ");


                // Evito topics duplicados si ya están en la base de datos.
                IEnumerable <DbTopic> res = from DbTopic x in db
                                            where x.Id == t.Id
                                            select x;
                DbTopic dbt;
                if (res == null || res.Count() == 0)
                {
                    dbt = t;
                }
                else
                {
                    dbt = (DbTopic)res.First();
                }

                // Saltear tweet en caso de que mencione una palabra prohibida para un tópico dado (e.g. "alejandro" en "sanz")
                bool ignore = false;
                foreach (String excep in dbt.Except)
                {
                    if (arg.Text.IndexOf(excep, 0, StringComparison.CurrentCultureIgnoreCase) != -1)
                    {
                        ignore = true;
                    }
                }

                if (ignore)
                {
                    Console.Out.WriteLine("\n\nIgnorando: " + arg.Text + "\n\n");
                }
                else
                {
                    if (!dbt.TweetsAbout.Contains(n.Id))
                    {
                        dbt.TweetsAbout.Add(n.Id);
                    }

                    n.About.Add(t.Id);

                    db.Store(n);
                    db.Store(dbt);

                    if (existente.Count() > 0)
                    {
                        TA.ProcessTweet(n);
                    }

                    cantTweets++;
                }
            };

            foreach (String a in t.Alias)
            {
                myStream.AddTrack(a, act);
            }
        }