private Task <PostLearningRepetitionResponseModel> HandleCorrectAnswer(WordStats stats, Guid userId)
        {
            stats.RevisionFactor    += 1;
            stats.NextRevisionTicks *= 2;
            stats.NextRevisionTime   = DateTime.Now.AddTicks(stats.NextRevisionTicks);
            stats.UpdatedTime        = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, DateTime.UtcNow.Hour, 0, 0);
            var userCourseStats = RetrieveUserCourseStats(stats, userId);

            if (userCourseStats == null)
            {
                _unitOfWork.UserCourseStats
                .Add(new UserCourseStats
                {
                    Date     = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, 0, 0, 0),
                    CourseId = stats.Word.CourseId,
                    UserId   = userId,
                    NumberOfCorrectResponses = 1
                });
            }
            else
            {
                userCourseStats.NumberOfCorrectResponses += 1;
                _unitOfWork.UserCourseStats
                .Update(userCourseStats);
            }
            _unitOfWork.Complete();

            return(Task.FromResult(new PostLearningRepetitionResponseModel
            {
                IsCorrectAnswer = true
            }));
        }
Beispiel #2
0
        private void omegle_experiment(string omegle_suffix)
        {
            if (omegle_suffix == "")
            {
                //TODO TODO TODO THIS is super ugly - just for back compatibility - remove asap
                omegle_suffix = "2";
            }

            var useTfIdf       = GET("noTfIdf") == null;
            var experimentFile = GET("experimentFile");

            var experimentRoot  = Configuration.OmegleExperimentsRootPath + omegle_suffix;
            var experimentFiles = Directory.EnumerateFiles(experimentRoot, "*.omegle_log").Select(f => Path.GetFileName(f)).ToArray();

            if (!experimentFiles.Contains(experimentFile))
            {
                experimentFile = experimentFiles.First();
            }

            var lines      = File.ReadAllLines(Path.Combine(experimentRoot, experimentFile));
            var question   = lines.First();
            var utterances = lines.Skip(1).Where(u => u.Trim() != "").ToArray();

            WordStats stats;

            if (useTfIdf)
            {
                var allFileUtterances = new List <IEnumerable <string> >();
                foreach (var file in experimentFiles)
                {
                    allFileUtterances.Add(File.ReadAllLines(Path.Combine(experimentRoot, file)));
                }

                stats = new WordStats(utterances, allFileUtterances);
            }
            else
            {
                stats = new WordStats(utterances);
            }

            if (omegle_suffix == "2")
            {
                //TODO TODO TODO THIS is super ugly - just for back compatibility - remove asap
                omegle_suffix = "";
            }

            SetParam("omegle_suffixes", _omegle_suffixes);
            SetParam("current_omegle_suffix", omegle_suffix);
            SetParam("experiment_files", experimentFiles);
            SetParam("current_experiment_file", experimentFile);
            SetParam("utterance_count", utterances.Length);
            SetParam("word_stats", stats);
            SetParam("question", question);

            Layout("layout.haml");
            Render("omegle.haml");
        }
        private UserCourseStats RetrieveUserCourseStats(WordStats stats, Guid userId)
        {
            var today = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, 0, 0, 0);

            return(_unitOfWork.UserCourseStats
                   .Find(ucs =>
                         ucs.CourseId == stats.Word.CourseId &&
                         ucs.UserId == userId &&
                         ucs.Date == today)
                   .FirstOrDefault());
        }
        /// <summary>
        /// Asynchronous web call to submit the word statistics to the server.
        /// </summary>
        /// <param name="stats">word statistics</param>
        /// <returns>HttpResponseMessage</returns>
        public static async Task <HttpResponseMessage> SubmitStatistics(string authToken, Document doc)
        {
            using (var client = ServiceClient.GetClient())
            {
                //package submitevent and authtoken within a SubmissionRequest
                var request = new WordStats(authToken, doc);

                var response = await client.PostAsXmlAsync("api/word/post", request);

                return(response);
            }
        }
        private void UpdateStats(WordStats stats, int sentenceIndex)
        {
            stats.Occurences++;
            var indexToStore = sentenceIndex + _configProvider.SentenceNumberStartsFrom;

            // uncomment next line of code to count multiple appearances in a single sentence as one
            // this will affect following Unit Tests:
            // * TestMultipleOccurencesInOneSentence (should pass if the line is commented, or fail otherwise)
            // * TestMultipleOccurencesInOneSentence_MergedIntoOne (should pass if the line is UNcommented, or fail otherwise)

            //if (!stats.SentenceNumbers.Contains(indexToStore))
            stats.SentenceNumbers.Add(indexToStore);
        }
Beispiel #6
0
        public HttpResponseMessage Post(WordStats request)
        {
            /*
             * var auth = new Authentication();
             * if (!auth.IsValidKey(request.AuthToken))
             *  return new HttpResponseMessage { StatusCode = HttpStatusCode.Forbidden };
             */

            int result = 1;

            return(new HttpResponseMessage
            {
                StatusCode = result > 0 ? HttpStatusCode.OK : HttpStatusCode.InternalServerError,
                Content = new StringContent(result.ToString())
            });
        }
Beispiel #7
0
        private Task <GetLearningRepetitionResponseModel> RepeatLearnedWord(WordStats wordStats)
        {
            var(question, pronunciation, responses, audio, repetitionType) = _repetitionManager
                                                                             .CreateRepetitionData(wordStats);

            var response = new GetLearningRepetitionResponseModel
            {
                WordStatsId    = wordStats.Id,
                WordId         = wordStats.WordId,
                ResponseType   = ResponseType.PracticeResponse,
                Question       = question,
                Responses      = responses,
                RepetitionType = repetitionType,
                Pronunciation  = pronunciation,
                Audio          = audio
            };

            return(Task.FromResult(response));
        }
        private Task <PostLearningRepetitionResponseModel> HandleIncorrectAnswer(Word word, WordStats stats, Guid userId)
        {
            stats.RevisionFactor    = 0;
            stats.NextRevisionTicks = _thirtySecondsInTicks;
            stats.NextRevisionTime  = DateTime.Now.AddTicks(stats.NextRevisionTicks);
            stats.UpdatedTime       = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, DateTime.UtcNow.Hour, 0, 0);
            var userCourseStats = RetrieveUserCourseStats(stats, userId);

            if (userCourseStats == null)
            {
                _unitOfWork.UserCourseStats
                .Add(new UserCourseStats
                {
                    Date     = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, 0, 0, 0),
                    CourseId = word.CourseId,
                    UserId   = userId,
                    NumberOfIncorrectResponses = 1
                });
            }
            else
            {
                userCourseStats.NumberOfIncorrectResponses += 1;
                _unitOfWork.UserCourseStats
                .Update(userCourseStats);
            }
            _unitOfWork.Complete();

            string audio = string.Empty;

            if (word.HasAudioGenerated)
            {
                audio = _audioService.RetrieveAudio(word.Id, WordType.OriginalWord);
            }

            return(Task.FromResult(new PostLearningRepetitionResponseModel
            {
                IsCorrectAnswer = false,
                CorrectWord = new WordDto
                {
                    Id = word.Id,
                    Definition = word.Definition,
                    OriginalWord = word.OriginalWord,
                    TranslatedWord = word.TranslatedWord,
                    ExampleUse = Helper.ApplyStyleToText(word.ExampleUse),
                    Pronunciation = word.Pronunciation
                },
                Audio = audio
            }));
        }
                RepetitionType repetitionType) CreateRepetitionData(WordStats wordStats)
        {
            var availableTypes = _allRepetitionTypes
                                 .Except(string.IsNullOrEmpty(wordStats.UsedRepetitionTypes) ?
                                         new List <RepetitionType>() :
                                         JsonConvert.DeserializeObject <List <RepetitionType> >(wordStats.UsedRepetitionTypes))
                                 .ToList();

            var chosenType = availableTypes.Any() ?
                             availableTypes[_random.Next(availableTypes.Count)] :
                             _allRepetitionTypes.ToList()[_random.Next(_allRepetitionTypes.Count())];

            var language = wordStats?.Word?.Course?.Language?.Name;

            var hasAudio = wordStats.Word.HasAudioGenerated;

            return(chosenType switch
            {
                RepetitionType.FromTranslatedToOriginalOpen => (
                    wordStats.Word.TranslatedWord,
                    null,
                    null,
                    string.Empty,
                    RepetitionType.FromTranslatedToOriginalOpen
                    ),
                RepetitionType.FromOriginalToTranslatedOpen => (
                    wordStats.Word.OriginalWord,
                    wordStats.Word.Pronunciation,
                    null,
                    hasAudio ? _audioService.RetrieveAudio(wordStats.WordId, WordType.OriginalWord) : string.Empty,
                    RepetitionType.FromOriginalToTranslatedOpen
                    ),
                RepetitionType.FromExampleToTranslatedOpen => (
                    Helper.ApplyStyleToText(wordStats.Word.ExampleUse),
                    wordStats.Word.Pronunciation,
                    null,
                    hasAudio ? _audioService.RetrieveAudio(wordStats.WordId, WordType.FullExampleUse): string.Empty,
                    RepetitionType.FromExampleToTranslatedOpen
                    ),
                RepetitionType.FromDefinitionToOriginalOpen => (
                    wordStats.Word.Definition,
                    null,
                    null,
                    string.Empty,
                    RepetitionType.FromDefinitionToOriginalOpen
                    ),
                RepetitionType.FromOriginalToTranslatedClose => (
                    wordStats.Word.OriginalWord,
                    wordStats.Word.Pronunciation,
                    GetOtherTranslatedWords(wordStats.Word.Id, wordStats.Word.CourseId, wordStats.Word.TranslatedWord),
                    hasAudio ? _audioService.RetrieveAudio(wordStats.WordId, WordType.OriginalWord): string.Empty,
                    RepetitionType.FromOriginalToTranslatedClose
                    ),
                RepetitionType.FromTranslatedToOriginalClose => (
                    wordStats.Word.TranslatedWord,
                    null,
                    GetOtherOriginalWords(wordStats.Word.OriginalWord, language),
                    string.Empty,
                    RepetitionType.FromTranslatedToOriginalClose
                    ),
                RepetitionType.FromDefinitionToOriginalClose => (
                    wordStats.Word.Definition,
                    null,
                    GetOtherOriginalWords(wordStats.Word.OriginalWord, language),
                    string.Empty,
                    RepetitionType.FromDefinitionToOriginalClose
                    ),
                RepetitionType.FromExampleToTranslatedClose => (
                    Helper.ApplyStyleToText(wordStats.Word.ExampleUse),
                    wordStats.Word.Pronunciation,
                    GetOtherTranslatedWords(wordStats.Word.Id, wordStats.Word.CourseId, wordStats.Word.TranslatedWord),
                    hasAudio ? _audioService.RetrieveAudio(wordStats.WordId, WordType.FullExampleUse) : string.Empty,
                    RepetitionType.FromExampleToTranslatedClose
                    ),
                RepetitionType.FromOriginalToDefinitionClose => (
                    wordStats.Word.OriginalWord,
                    wordStats.Word.Pronunciation,
                    GetOtherDefinitions(wordStats.Word.Id, wordStats.Word.CourseId, wordStats.Word.Definition),
                    hasAudio ? _audioService.RetrieveAudio(wordStats.WordId, WordType.OriginalWord) : string.Empty,
                    RepetitionType.FromOriginalToDefinitionClose
                    ),
                RepetitionType.FromExampleToDefinitionClose => (
                    Helper.ApplyStyleToText(wordStats.Word.ExampleUse),
                    wordStats.Word.Pronunciation,
                    GetOtherDefinitions(wordStats.Word.Id, wordStats.Word.CourseId, wordStats.Word.Definition),
                    hasAudio ? _audioService.RetrieveAudio(wordStats.WordId, WordType.FullExampleUse) : string.Empty,
                    RepetitionType.FromExampleToDefinitionClose
                    ),
                RepetitionType.FromExampleToOriginalClose => (
                    Helper.HideOriginal(wordStats.Word.ExampleUse),
                    null,
                    GetOtherOriginalWords(Helper.GetOriginalFromExample(wordStats.Word.ExampleUse), language),
                    hasAudio ? _audioService.RetrieveAudio(wordStats.WordId, WordType.BlankExampleUse) : string.Empty,
                    RepetitionType.FromExampleToOriginalClose
                    ),
                RepetitionType.FromTranslatedToOriginalDifferentClose => (
                    wordStats.Word.TranslatedWord,
                    null,
                    GetDifferentOriginalWords(wordStats.Word.Id, wordStats.Word.CourseId, wordStats.Word.OriginalWord),
                    string.Empty,
                    RepetitionType.FromTranslatedToOriginalDifferentClose
                    ),
                RepetitionType.FromDefinitionToOriginalDifferentClose => (
                    wordStats.Word.Definition,
                    null,
                    GetDifferentOriginalWords(wordStats.Word.Id, wordStats.Word.CourseId, wordStats.Word.OriginalWord),
                    string.Empty,
                    RepetitionType.FromDefinitionToOriginalDifferentClose
                    ),
                _ => (null, null, null, string.Empty, RepetitionType.None),
            });
 private static string BuildWordStatistics(WordStats stat)
 {
     return($"{{{stat.Occurences}:{string.Join(",", stat.SentenceNumbers.Select(x => x.ToString()))}}}");
 }
 private static void RenderLine(TextWriter outputWriter, WordStats stat, int index)
 {
     outputWriter.Write($"{BuildAlphaIndex(index),-6}{stat.Word,-28}{BuildWordStatistics(stat),-20}");
 }