Exemple #1
0
 /*
  * Legger til et eller flere stopp ord fra databasen
  * Sjekker om stoppordet eksisterer først
  */
 public bool addStopsWordsDB(List <string> stopWords)
 {
     using (var db = new dbEntities())
     {
         try
         {
             foreach (var newStopWord in stopWords)
             {
                 if (db.stopwords.Where(sw => sw.Word == newStopWord).FirstOrDefault() == null)
                 {
                     db.stopwords.Add(new stopwords {
                         Word = newStopWord
                     });
                 }
             }
             db.SaveChanges();
         }
         catch (Exception e)
         {
             Debug.WriteLine("An error has occured while adding: " + e.Message.ToString());
             return(false);
         }
         return(true);
     }
 }
Exemple #2
0
        /***********************
        * Saving/Adding/Removing from Database
        ***********************/

        /*
         * Lagrer nye - eller oppdaterer antall forekomster av et ord i tabellen 'word'
         */
        public bool saveWords(List <IGrouping <string, string> > groupedWords)
        {
            using (var db = new dbEntities())
            {
                try
                {
                    foreach (var word in groupedWords)
                    {
                        if (word.Key.Length > 1 && word.Key.Length < 30)
                        {
                            var foundWord = db.words.Where(w => word.Key == w.word).FirstOrDefault();
                            if (foundWord == null)
                            {
                                db.words.Add(new words {
                                    word = word.Key, count = word.Count()
                                });
                            }
                            else
                            {
                                foundWord.count += word.Count();
                            }
                        }
                    }
                    db.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                            ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return(false);
                }
                return(true);
            }
        }
Exemple #3
0
        /*
         * Lagrer ordskyen til en person i tabellen 'wordcloud'
         * Key er en id til et ord fra 'word' tabellen
         * MÅ IKKE MISTOLKES MED KEY I GROUPING (ER IKKE DET SAMME)
         *
         * cristinID | key | count
         * 10050     | 2   | 20
         * 10050     | 100 | 3
         */
        public bool saveWordCloud(List <IGrouping <string, string> > groupedWords, string cristinID, short totalTitles)
        {
            using (var db = new dbEntities())
            {
                try
                {
                    foreach (var word in groupedWords)
                    {
                        //int foundWordKey = db.words.Where(w => word.Key == w.word).Select(w => w.key).FirstOrDefault();

                        words  w1 = db.words.Where(w => w.word == word.Key).FirstOrDefault();
                        person p1 = db.person.Where(c => c.cristinID == cristinID).FirstOrDefault();

                        db.wordcloud.Add(new wordcloud {
                            person = p1, words = w1, count = (short)word.Count()
                        });
                    }

                    // db.titles.Add(new titles { cristinID = cristinID, titlesCount = totalTitles });
                    db.SaveChanges();
                }

                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                            ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return(false);
                }
                return(true);
            }
        }
Exemple #4
0
 /*
  * Sletter et eller flere stopp ord fra databasen
  * -    Sjekker om stoppordet eksisterer først
  */
 public bool removeStopsWordsDB(List <string> stopWords)
 {
     using (var db = new dbEntities())
     {
         try
         {
             foreach (var oldStopWord in stopWords)
             {
                 var containsStopWord = db.stopwords.Where(sw => sw.Word == oldStopWord).FirstOrDefault();
                 if (containsStopWord != null)
                 {
                     db.stopwords.Remove(containsStopWord);
                 }
             }
             db.SaveChanges();
         }
         catch (Exception e)
         {
             Debug.WriteLine("An error has occured while removing: " + e.Message.ToString());
             return(false);
         }
         return(true);
     }
 }