public bool RemoveDeleted(List <long> userIds) { bool result = true; int i = 1; var languagesQuery = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown); var languagesIds = Enum.GetValues(typeof(LanguageShortName)).Cast <LanguageShortName>() .Where(e => languagesQuery.GetByShortName(e) != null) .Select(e => languagesQuery.GetByShortName(e).Id).ToList(); foreach (long userId in userIds) { foreach (var languageId in languagesIds) { IUserKnowledgeQuery userKnowledgeQuery = new UserKnowledgeQuery(userId, languageId); if (!userKnowledgeQuery.RemoveDeleted()) { Console.WriteLine("Не удалось удалить знания пользователя {0} по языку {1}!", userId); result = false; } } if (i % 100 == 0) { Console.WriteLine("Удалены старые данные для {0} пользователей из {1}!", i, userIds.Count); } i++; } return(result); }
public void Create(string file) { var csvReader = new CsvReader(file); var languages = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown); Language from = languages.GetByShortName(_from); Language russian = languages.GetByShortName(LanguageShortName.Ru); //заголовок не учитывать string[] firstLine = csvReader.ReadLine(); var popularWordsQuery = new PopularWordsQuery(); do { string[] line = csvReader.ReadLine(); if (line == null) { break; } if (line.Length < 2) { continue; } SourceWithTranslation popularWord = popularWordsQuery.GetOrCreate(CreateWordForUser(line[0], from), CreateWordForUser(line[1], russian), _type); Console.WriteLine("{0}: {1}", popularWord != null ? "Сохранено" : "Не сохранено", line.Aggregate((e1, e2) => e1 + " -> " + e2)); } while (true); }
/// <summary> /// Возвращает перевод для слова /// </summary> /// <param name="sourceLanguage">язык, на котором введено слово</param> /// <param name="destinationLanguage">язык, на который нужно перевести слово</param> /// <param name="word">слово, которое нужно перевести</param> /// <returns>переведенное слово</returns> public ActionResult Translation(string sourceLanguage, string destinationLanguage, string word) { const string ACTION_NAME = "Index"; if (WebSettingsConfig.Instance.IsSectionForbidden(SectionId)) { return(RedirectToAction("Index", RouteConfig.MAIN_CONTROLLER_NAME)); } var languages = new LanguagesQuery(WebSettingsConfig.Instance.DefaultLanguageFrom, WebSettingsConfig.Instance.DefaultLanguageTo); LanguageShortName source = LanguagesQuery.ParseShortName(sourceLanguage); LanguageShortName destination = LanguagesQuery.ParseShortName(destinationLanguage); UserLanguages userLanguages = languages.GetLanguagesByShortNames(source, destination); if (UserLanguages.IsInvalid(userLanguages) || string.IsNullOrEmpty(word)) { return(RedirectToAction(ACTION_NAME, ControllerName)); } List <PronunciationForUser> translationWords = GetTranslationsByQuery(userLanguages, word); return(View(ACTION_NAME, new TranslatorModel(userLanguages, word, translationWords))); }
public ActionResult Translation(long userId, long?sourceId, long?translationId) { if (WebSettingsConfig.Instance.IsSectionForbidden(_sectionId)) { return(RedirectToAction("Index", RouteConfig.MAIN_CONTROLLER_NAME)); } if (!sourceId.HasValue || !translationId.HasValue) { return(RedirectToAction(_viewName)); } List <SourceWithTranslation> sourceWithTranslations = _query.GetExact(userId, sourceId.Value, translationId.Value); if (sourceWithTranslations == null || sourceWithTranslations.Count == 0) { return(RedirectToAction(_viewName)); } var languages = new LanguagesQuery(WebSettingsConfig.Instance.DefaultLanguageFrom, WebSettingsConfig.Instance.DefaultLanguageTo); SourceWithTranslation currentSentence = sourceWithTranslations.Single(e => e.IsCurrent); UserLanguages userLanguages = languages.GetLanguages(new List <long> { currentSentence.Source.LanguageId, currentSentence.Translation.LanguageId }); if (UserLanguages.IsInvalid(userLanguages)) { return(RedirectToAction(_viewName)); } return(View(_viewName, new ShuffleModel(userLanguages, sourceWithTranslations))); }
private void SetLanguages() { var languages = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown); Language english = languages.GetByShortName(LanguageShortName.En); Language russian = languages.GetByShortName(LanguageShortName.Ru); _englishLanguageId = english.Id; _russianLanguageId = russian.Id; }
private static UserLanguages GetUserLanguages() { var languagesQuery = new LanguagesQuery(WebSettingsConfig.Instance.DefaultLanguageFrom, WebSettingsConfig.Instance.DefaultLanguageTo); UserLanguages userLanguages = languagesQuery.GetLanguagesByShortNames(LanguageShortName.En, LanguageShortName.Ru); return(userLanguages); }
public void Create(string file) { var csvReader = new CsvReader(file); //var sentences = IoCModule.Create<SentencesQuery>(); var languages = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown); Language from = languages.GetByShortName(_from); Language russian = languages.GetByShortName(LanguageShortName.Ru); string[] firstLine = csvReader.ReadLine(); if (firstLine == null || EnumerableValidator.IsEmpty(firstLine)) { return; } string groupName = TextFormatter.FirstUpperCharAndTrim(firstLine[0]); if (string.IsNullOrEmpty(groupName) || groupName.Length < 2) { return; } var groupsQuery = new GroupsQuery(from.Id); string fileName = firstLine.Length > 1 && !string.IsNullOrWhiteSpace(firstLine[1]) ? firstLine[1].Trim() : groupName; string imageFileName = Path.Combine(@"C:\Projects\StudyLanguages\Источники для групп\Источники картинок\", fileName + ".jpg"); //создает или получает раннее созданную группу byte[] image = !string.IsNullOrEmpty(imageFileName) ? File.ReadAllBytes(imageFileName) : null; GroupForUser groupForUser = groupsQuery.GetOrCreate(GroupType, groupName, image); do { string[] line = csvReader.ReadLine(); if (line == null) { break; } if (line.Length < 2) { continue; } image = line.Length > 2 && !string.IsNullOrEmpty(line[2]) ? File.ReadAllBytes(line[2]) : null; int?rating = null; int rat; if (line.Length > 3 && int.TryParse(line[3], out rat)) { rating = rat; } bool isSuccess = Create(groupForUser, line, from, russian, image, rating); Console.WriteLine("{0}: {1}", isSuccess ? "Сохранено" : "Не сохранено", line.Aggregate((e1, e2) => e1 + " -> " + e2)); } while (true); }
public async Task Get() { var logger = testConfig.GetLoggerFactory().CreateLogger <LanguageGetHandler>(); LanguageGetHandler eventHandlers = new LanguageGetHandler(testConfig.GetDbContext(), logger); LanguagesQuery query = new LanguagesQuery(); IList <Language> res = await eventHandlers.Handle(query, new CancellationToken()); Assert.AreEqual(2, res.Count); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { List <long> parsedLanguagesIds = GetLanguagesIdsFromCookie(filterContext); var languages = new LanguagesQuery(WebSettingsConfig.Instance.DefaultLanguageFrom, WebSettingsConfig.Instance.DefaultLanguageTo); UserLanguages userLanguages = languages.GetLanguages(parsedLanguagesIds); filterContext.ActionParameters[_userLanguagesParamName] = userLanguages; filterContext.RouteData.Values[_userLanguagesParamName] = userLanguages; }
private void LoadLanguages() { if (DefaultUserLanguages != null) { return; } var languages = new LanguagesQuery(DefaultLanguageFrom, DefaultLanguageTo); DefaultUserLanguages = languages.GetLanguages(null); }
public TVSeriesCreator(LanguageShortName language, string outputPath, string origSeriesName, string transSeriesName) { _outputPath = outputPath; _origSeriesName = origSeriesName; _transSeriesName = transSeriesName; var languagesQuery = new LanguagesQuery(language, LanguageShortName.Ru); _languageId = languagesQuery.GetByShortName(language).Id; _seriesQuery = new TVSeriesQuery(_languageId); }
public static void Fill() { var languages = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown); Language language = languages.GetByShortName(LanguageShortName.En); long englishLanguageId = language.Id; IPronunciationQuery pronunciationQuery = new SentencesQuery(); pronunciationQuery.FillSpeak(englishLanguageId); pronunciationQuery = new WordsQuery(); pronunciationQuery.FillSpeak(englishLanguageId); }
public static void Fill(string groupName, string folder) { var languages = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown); Language english = languages.GetByShortName(LanguageShortName.En); Language russian = languages.GetByShortName(LanguageShortName.Ru); //первую строку пропускаем string imagePath = Path.Combine(folder, groupName); List <string> imageFiles = Directory.GetFiles(imagePath, "*.*") .Where(file => file.ToLower().EndsWith("jpg") || file.ToLower().EndsWith("jpeg")) .ToList(); new DbAdapter().ActionByContext(c => { int i = 1; foreach (string imageFile in imageFiles) { string englishWord = Path.GetFileNameWithoutExtension(imageFile); ImageConverter.ResizeSmallImages(imageFile); Image image = Image.FromFile(imageFile); var memoryStream = new MemoryStream(); image.Save(memoryStream, ImageFormat.Jpeg); byte[] imageBytes = memoryStream.ToArray(); IQueryable <WordTranslation> wordTranslations = (from wt in c.WordTranslation join w1 in c.Word on wt.WordId1 equals w1.Id join w2 in c.Word on wt.WordId2 equals w2.Id join gw in c.GroupWord on wt.Id equals gw.WordTranslationId join g in c.Group on gw.GroupId equals g.Id where g.Name == groupName && ((w1.Text == englishWord && w1.LanguageId == english.Id) || (w2.Text == englishWord && w2.LanguageId == english.Id)) select wt); List <WordTranslation> wordsTranslation = wordTranslations.ToList(); foreach (WordTranslation wordTranslation in wordsTranslation) { wordTranslation.Image = imageBytes; c.SaveChanges(); Console.WriteLine("{0}. {1} сохранено", i, englishWord); } i++; } ; }); }
private void LoadLanguages() { if (_userLanguages != null) { return; } var languagesQuery = new LanguagesQuery(LanguageShortName.Ru, LanguageShortName.En); _userLanguages = languagesQuery.GetLanguages(null); AddToShortName(languagesQuery, _userLanguages.From.Id); AddToShortName(languagesQuery, _userLanguages.To.Id); }
public bool RemoveWithoutData(List <long> userIds) { bool result = true; int i = 1; var languagesQuery = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown); List <long> languagesIds = Enum.GetValues(typeof(LanguageShortName)).Cast <LanguageShortName>().Where(e => languagesQuery.GetByShortName(e) != null).Select( e => languagesQuery.GetByShortName(e).Id).ToList(); foreach (long userId in userIds) { foreach (long languageId in languagesIds) { IUserRepetitionKnowledgeQuery repetitionQuery = new UserRepetitionKnowledgeQuery(userId, languageId, KnowledgeDataType. All); IUserRepetitionIntervalQuery userRepetitionIntervalQuery = new UserRepetitionIntervalQuery(userId, languageId, KnowledgeSourceType . Knowledge, repetitionQuery, RepetitionType .All); if (!userRepetitionIntervalQuery.RemoveWithoutData()) { Console.WriteLine( "Не удалось удалить данные о повторениях для пользователя {0} для языка {1}!", userId, languageId); result = false; } } if (i % 100 == 0) { Console.WriteLine("Удалены данные о повторениях для {0} несуществующих пользователей из {1}!", i, userIds.Count); } i++; } return(result); }
public void Fill() { var csvReader = new CsvReader(@"C:\Projects\StudyLanguages\1.csv"); string[] line; var sentences = new SentencesQuery(); var languages = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown); Language english = languages.GetByShortName(LanguageShortName.En); Language russian = languages.GetByShortName(LanguageShortName.Ru); do { line = csvReader.ReadLine(); if (line != null) { SourceWithTranslation sentenceWithTranslation = sentences.GetOrCreate(SentenceType.Separate, new PronunciationForUser(IdValidator.INVALID_ID, line[0], false, english.Id), new PronunciationForUser(IdValidator.INVALID_ID, line[1], false, russian.Id), null, null); Console.WriteLine("{0}: {1}", sentenceWithTranslation != null ? "Сохранено" : "Не сохранено", line.Aggregate((e1, e2) => e1 + " -> " + e2)); } } while (line != null); /* for (int i = 1; i <= 100000; i++) { * SentenceWithTranslation sentenceWithTranslation = * sentences.CreateSentencencesWithTranslation(english, "Test sentence number " + i, russian, * "Тестовое предложение № " + i); * if (sentenceWithTranslation == null) { * Console.WriteLine("Не удалось сохранить предложение {0}. Нажмите Enter...", i); * Console.ReadLine(); * } * * if (i % 10000 == 0) { * Console.WriteLine("Сохранено {0} предложений", i); * } * }*/ }
public void Fill(VideoForUser videoForUser) { long languageId = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown).GetByShortName( LanguageShortName.En).Id; string title = videoForUser.Title; string htmlCode = videoForUser.HtmlCode; byte[] image = GetImage(title, htmlCode); var videosQuery = new VideosQuery(languageId); VideoForUser result = videosQuery.GetOrCreate(VideoType.Clip, videoForUser, image, null); if (result != null) { Console.WriteLine("Видео \"{0}\" успешно добавлено", title); } else { Console.WriteLine("Не удалось добавить видео \"{0}\"!!!", title); } }
public void Create(string fileName, string pathToImagePattern) { var csvReader = new CsvReader(fileName); var languages = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown); Language from = languages.GetByShortName(_from); Language to = languages.GetByShortName(LanguageShortName.Ru); var representationsQuery = new RepresentationsQuery(from.Id); string[] line = csvReader.ReadLine(); if (line.Length < 1 || string.IsNullOrEmpty(line[0])) { Console.WriteLine("Некорректная первая строка в файле {0}!", fileName); return; } int imageIndex = 0; if (line.Length >= 2 && !string.IsNullOrEmpty(line[1])) { imageIndex = 1; } string imageFileName = string.Format(pathToImagePattern, line[imageIndex].Trim()); Image image = Image.FromFile(imageFileName); var memoryStream = new MemoryStream(); image.Save(memoryStream, ImageFormat.Jpeg); byte[] imageBytes = memoryStream.ToArray(); string visualDictionaryName = line[0]; byte?widthPercent = null; if (line.Length > 2) { byte w; if (byte.TryParse(line[2], out w) && w > 0 && w <= 100) { widthPercent = w; } } visualDictionaryName = char.ToUpper(visualDictionaryName[0]) + visualDictionaryName.Substring(1); var representationForUser = new RepresentationForUser(IdValidator.INVALID_ID, visualDictionaryName, imageBytes, new Size(image.Size.Width, image.Size.Height), widthPercent); bool hasErrors = false; do { line = csvReader.ReadLine(); if (line != null) { if (line.Length < 6) { hasErrors = true; break; } PronunciationForUser englishWord = CreateWordForUser(line[0], from); PronunciationForUser russianWord = CreateWordForUser(line[1], to); Point leftTopPoint = CreatePoint(line[2], line[3]); Point rightBottomPoint = CreatePoint(line[4], line[5]); if (englishWord == null || russianWord == null || leftTopPoint == null || rightBottomPoint == null) { hasErrors = true; break; } var representationArea = new RepresentationAreaForUser(IdValidator.INVALID_ID, leftTopPoint, rightBottomPoint) { Source = russianWord, Translation = englishWord }; representationForUser.AddArea(representationArea); } } while (line != null); if (hasErrors) { Console.WriteLine("В файле {0} возникли ошибки! Файл не будет сохранен", fileName); return; } RepresentationForUser savedRepresentation = representationsQuery.GetOrCreate(representationForUser); Console.WriteLine("Визуальный словарь {0} {1}", representationForUser.Title, savedRepresentation != null ? "сохранен" : "НЕ сохранен!"); }
public byte[] Generate(LanguageShortName from, LanguageShortName to, HashSet <long> visualDictionariesIds) { var languagesQuery = new LanguagesQuery(from, to); UserLanguages userLanguages = languagesQuery.GetLanguagesByShortNames(from, to); var representationsQuery = new RepresentationsQuery(userLanguages.From.Id); List <RepresentationForUser> allRepresentations = representationsQuery.GetBought(userLanguages, visualDictionariesIds); PdfGenerator commonPdfGenerator = allRepresentations.Count > 1 ? new PdfGenerator(_fontPath, _domain, "Все визуальные словари") : null; var zipKey = GetZipKey(allRepresentations, @from, to); var result = _cache.Get(zipKey); if (result != null) { return(result); } var painter = new Painter(); var partImageCreator = new PartImageCreator(painter); foreach (RepresentationForUser representation in allRepresentations) { var tableData = new TableData(3, true); tableData.AddHeader(string.Empty, "Слово", "Перевод"); string title = representation.Title; /*if (pictureName != "Лицо" /*&& pictureName != "Человек"#1#) { * continue; * }*/ var byteStream = new MemoryStream(representation.Image); Image image = Image.FromStream(byteStream); var wordsWriter = new WordsWriter(painter, image.Width, image.Height); foreach (RepresentationAreaForUser area in representation.Areas) { Point leftCorner = area.LeftUpperCorner; Point rightCorner = area.RightBottomCorner; string partKey = representation.Id + "_part_" + area.Id + "_" + representation.SortInfo.LastModified.Ticks + "_" + from + "_" + to + ".jpeg"; byte[] partImageBytes = _cache.Get(partKey); if (partImageBytes == null) { PartImageData partImageData = partImageCreator.CutImage(leftCorner, rightCorner, image); partImageBytes = ImageToBytes(partImageData.Bitmap); WriteToCache(partKey, partImageBytes); } tableData.AddRow(new[] { TableDataCell.CreateImage(partImageBytes), TableDataCell.CreateText(area.Source.Text), TableDataCell.CreateText(area.Translation.Text) }); wordsWriter.AddRectangle(leftCorner, rightCorner, area); } string fullKey = representation.Id + "_" + representation.SortInfo.LastModified.Ticks + "_" + from + "_" + to + ".jpeg"; byte[] imageResult = _cache.Get(fullKey); if (imageResult == null) { Image imageWithSign = wordsWriter.GetImageWithSigns(image); imageResult = ImageToBytes(imageWithSign); WriteToCache(fullKey, imageResult); } var pdfFileName = title + ".pdf"; string pdfKey = representation.Id + "_" + representation.SortInfo.LastModified.Ticks + "_" + from + "_" + to + "_" + pdfFileName; byte[] pdfContent = _cache.Get(pdfKey); if (pdfContent == null) { var pdfGenerator = new PdfGenerator(_fontPath, _domain, string.Format( "Визуальный словарь на тему {0}", title)); WriteDataToPdf(pdfGenerator, title, imageResult, tableData); pdfContent = pdfGenerator.GetAsBytes(); WriteToCache(pdfKey, pdfContent); } _zipCompressor.AddFileToArchive(pdfFileName, pdfContent); if (commonPdfGenerator != null) { WriteDataToPdf(commonPdfGenerator, title, imageResult, tableData); commonPdfGenerator.NewPage(); } } if (commonPdfGenerator != null) { WritePdfToArchive(commonPdfGenerator, "Всё в одном файле.pdf"); } result = _zipCompressor.GetArchive(); WriteToCache(zipKey, result); return(result); }
public void Process() { long languageId = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown).GetByShortName( _shortName).Id; const int MAX_DOMAIN_TO_PROCESS = 10; List <Tuple <string, List <string> > > linksByDomains = _linksHelper.Analyze(); foreach (var tuple in linksByDomains.Take(MAX_DOMAIN_TO_PROCESS)) { IVideoDataGetter videoDataGetter = GetVideoDataGetterByDomain(tuple.Item1); if (videoDataGetter == null) { continue; } int failCount = 0; int successCount = 0; List <string> links = tuple.Item2; foreach (string link in links) { string encodedLink = FileHelper.EncodeFileName(link); IVideoData videoData = GetVideoDataFromCache(videoDataGetter, encodedLink); if (videoData == null) { //для этой ссылки данных нет в кэше - получить их от Getter'а videoData = videoDataGetter.GetVideoData(link); } if (videoData == null) { Console.WriteLine("Для ссылки {0} НЕ удалось получить данные!", link); failCount++; continue; } SaveVideoDataToCache(videoDataGetter, encodedLink, videoData); if (videoDataGetter.IsInvalid(videoData, _shortName)) { Console.WriteLine("Для ссылки {0} данные некорректны!", link); failCount++; continue; } var videosQuery = new VideosQuery(languageId); var videoForUser = new VideoForUser(videoData.Title, videoData.HtmlCode); //TODO: поиск дубликатов видео //TODO: сохранять дополнительную информацию в БД VideoForUser result = videosQuery.GetOrCreate(VideoType.Movie, videoForUser, videoData.ThumnailImage, videoData.Rating); if (result != null) { successCount++; } else { Console.WriteLine("Не удалось добавить видео \"{0}\"!!!", videoData.Title); } } Console.WriteLine("Обработан домен {0} из {1} ссылок успешно сохранены {2}, не удалось сохранить {3}", tuple.Item1, links.Count, successCount, failCount); } }
public async Task <IList <Language> > Handle(LanguagesQuery query, CancellationToken cancellationToken) { IList <Language> result = await db.Languages.ToListAsync(); return(result); }
public void Fill(LanguageShortName languageShortName, string fileName) { long languageId = new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown).GetByShortName(languageShortName).Id; var crossReferencesQuery = new CrossReferencesQuery(languageId); bool hasErrors = false; var ids = new HashSet <long>(); var csvReader = new CsvReader(fileName); int i = 0; do { i++; string[] line = csvReader.ReadLine(); if (line == null) { break; } if (line.Length != 4) { ShowError( "Некорректное кол-во полей в строке {0}: Ожидаем 4, а полей {1}. Для продолжения нажмите Enter...", i, line.Length); continue; } string source = GetTrimmed(line[0]); CrossReferenceType sourceType = GetCrossType(line[1]); string destination = GetTrimmed(line[2]); CrossReferenceType destinationType = GetCrossType(line[3]); if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(destination) || sourceType == CrossReferenceType.Unknown || destinationType == CrossReferenceType.Unknown) { ShowError("Некорректные данные в строке {0}: {1}. Для продолжения нажмите Enter...", i, string.Join(";", line)); continue; } long id = crossReferencesQuery.Add(source, sourceType, destination, destinationType); if (IdValidator.IsValid(id)) { ids.Add(id); Console.WriteLine("Обработана строка под номером {0}", i); } else { hasErrors = true; ShowError( "Не удалось сохранить данные из строки {0}. Источник {1}, тип источника {2}, приемник {3}, тип источника {4}. Для продолжения нажмите Enter...", i, source, sourceType, destination, destinationType); } } while (true); if (!hasErrors) { crossReferencesQuery.RemoveAllExceptIds(ids); } Console.WriteLine("С ссылками все"); Console.ReadLine(); }