public BookResult WriteCommentToFile(Book book, string newComment) { BookResult bookResult = fileOperation.WriteCommentToFile(book, newComment); return(bookResult); }
public void ReadTxtFile(string filePathName, Language language, BookCategory category, int skipLines, string connectionString, ref Dictionary <string, WordStatistics> wordsRepo) { var sw = new Stopwatch(); var fi = new FileInfo(filePathName); if (fi.Extension != ".txt") { return; } sw.Start(); var lines = File.ReadLines(filePathName); var cntdict = new Dictionary <string, Int64>(); Int64 wc = 0; Int64 tc = 0; Int64 linenum = 0; // 定义enumerable 避免可能的对IEnumerable的遍历,这是ReSharper提示的优化项。 var enumerable = lines as IList <string> ?? lines.ToList(); foreach (var line in enumerable) { ++linenum; if (linenum <= skipLines) // todo: this is temp for downloaded book { continue; } var cline = GetLetters(line, language); foreach (var letter in cline) { if (cntdict.ContainsKey(letter)) { cntdict[letter] += 1; } else { cntdict[letter] = 1; ++wc; } ++tc; } } if (wc < 50) { Console.WriteLine( $"{TotalBook}: Parse total words: {tc}, different words: {wc}, less than 50!!!!! {fi.Name.Replace(".txt", "")}"); return; } List <KeyValuePair <string, Int64> > myList = cntdict.ToList(); myList.Sort( (pair1, pair2) => pair2.Value.CompareTo(pair1.Value) ); var top1020 = string.Join("", myList.Skip(10).Take(10).Select(p => p.Key).ToArray()); bool findbook = true; try { var context = Program.GetBookContext(connectionString); // 添加跟踪信息 var booktrack = new BookTrack { Top1020 = top1020, OriginalPath = filePathName, BookInfo = string.Join('\n', enumerable.Skip(skipLines).Take(5)) }; context.Add(booktrack); context.SaveChanges(); var book = context.Books.SingleOrDefault(p => p.TopIndexWords == top1020); // Include(p => p.Language).Include(p => p.BookCategory). if (book != null) { Console.WriteLine($"book already exists: {fi.Name}, tracked in BookTrack and not calculate statistics."); return; } if (book == null) { findbook = false; book = new Book { LanguageId = language.LanguageId, BookName = fi.Name.Replace(".txt", ""), LastDateTime = DateTime.UtcNow, BookInfo = string.Join('\n', enumerable.Skip(skipLines).Take(5)) }; } book.PrevWordCount = book.LastWordCount; book.PrevTotalCount = book.LastTotalCount; book.LastWordCount = wc; book.LastTotalCount = tc; if (findbook) { context.Update(book); } else { book.TopIndexWords = top1020; // 2018年4月17日 EF Core Add function returns negative id,why? context.Add(book); } context.SaveChanges(); MapBookCategory map = new MapBookCategory { BookId = book.BookId, BookCategoryId = category.BookCategoryId }; context.Add(map); context.SaveChanges(); var br = new BookResult { Book = book, BookId = findbook ? book.BookId : 0, LanguageId = language.LanguageId, BookTrack = booktrack.BookTrackId, ResultDateTime = DateTime.UtcNow, WordCount = wc, TotalCount = tc, Top10 = string.Join("", myList.Take(10).Select(p => p.Key).ToArray()), Top1020 = top1020, Top50 = string.Join("", myList.Take(50).Select(p => p.Key).ToArray()) }; context.Add(br); context.SaveChanges(); var newwords = new List <WordStatistics>(); var wrs = new List <WordResult>(); foreach (var dic in cntdict) { wrs.Add(new WordResult { Book = book, BookId = book.BookId, WordCount = dic.Value, WordLetter = dic.Key }); if (!wordsRepo.ContainsKey(dic.Key)) { wordsRepo[dic.Key] = new WordStatistics { WordUnicode = dic.Key, TotalBook = 1, TotalWords = wc, TotalOccur = dic.Value, MaxRatio = (double)dic.Value / (double)wc, MaxWords = wc, MaxOccur = dic.Value, BookCategory = category, BookCategoryId = category.BookCategoryId, FirstBookId = book.BookId, WordLength = dic.Key.Length }; } else { var wordsta = wordsRepo[dic.Key]; wordsta.TotalBook += 1; wordsta.MaxOccur = Math.Max(wordsta.MaxOccur, dic.Value); wordsta.MaxWords = Math.Max(wordsta.MaxWords, wc); wordsta.MaxRatio = Math.Max(wordsta.MaxRatio, (double)dic.Value / (double)wc); wordsta.TotalOccur += dic.Value; wordsta.TotalWords += wc; //if (findbook) todo 这里的计算有误! //{ // wordsta.TotalOccur -= ?? not book.PrevWordCount; // wordsta.TotalWords -= ?? not book.PrevTotalCount; //} #if false #else wordsRepo[dic.Key] = wordsta; #endif } } #if false context.AddRange(wrs); context.UpdateRange(wordsrepo.Values); context.SaveChanges(); context.AddRange(newwords); context.SaveChanges(); #else #endif sw.Stop(); ++TotalBook; TotalWords += wc; TotalTime += sw.ElapsedMilliseconds; Console.WriteLine( $"{TotalBook}: Parse finished, total words: {tc}, different words: {wc}, average elapsed milliseconds: {(double) TotalTime / (double) TotalBook}, average words: {(double) TotalWords / (double) TotalBook}. {book.BookName}"); Int64 i = 0; foreach (var count in myList) { if (i++ > 10) { break; } Console.WriteLine($"{i}: {count.Key}: {count.Value}"); } } catch (Exception e) { // must create new context since old context has dirty data var context = Program.GetBookContext(connectionString); context?.Add(new BookException { Top1020 = top1020, ErrorMsg = e.ToString(), BookInfo = fi.FullName }); context?.SaveChanges(); Console.WriteLine($"{TotalBook}: Parse failed: {e}"); } }
public BookResult incrementLikeCount(Book book, User user) { BookResult bookResult = fileOperation.ReadTheBookFromFileChangeTheLikeCount(book, user); return(bookResult); }
public IActionResult Result(BookResult result) { return(View(result)); }