<commit_before>mainApp.controller('WordLookupCtrl', function ($scope, $http, $modal, $route, lookupService, imageSearchService, $timeout) {

    $scope.selectBook = function(book){
        $scope.selectedBook = book;
    }

    $scope.getSelectedBookWords = function(){
        if($scope.selectedBook){
            var result = [];
            for(var i = 0; i < $scope.words.length; i++){
                if($scope.selectedBook.book_name == $scope.words[i].book_name){
                    result.push($scope.words[i])
                }
            }
            return result;
        }
        else
            return $scope.words;
    }

    $http({method: 'GET', url: 'http://localhost:3000/user/1/word-lookup'}).success(function(response, status, headers, config) {
        $scope.words = response.result;
    });

    $http({method: 'GET', url: 'http://localhost:3000/user/1/word-lookup/books'}).success(function(response, status, headers, config) {
        $scope.books = response.result;
    });

    $scope.quizWords = function() {

        var notTakenQuiz = [];
        var words = $scope.getSelectedBookWords();

        angular.forEach(words, function(word){
            if(!word.word_lookup_quiz_taken)
                this.push(word);
        }, notTakenQuiz);

        return notTakenQuiz;
    };

    $scope.startQuiz = function(words) {

        var modalInstance = $modal.open({
            templateUrl: './views/partial/quiz.html',
            controller: ModalInstanceCtrl,
            resolve: {
                words: function () {
                    return words;
                }
            }
        });

        modalInstance.result.then(function () {
            $route.reload();
        });
    };

    var ModalInstanceCtrl = function ($scope, $modalInstance, words) {

        // init variables
        $scope.wordCursor = 0;
        $scope.words = words;
        $scope.word = $scope.words[0];
        $scope.showingResult = false;

        $scope.hint = {
            showing: false,
            loading: false,
            images: []
        };

        // show meaning
        $scope.showMeaning = function(word){
            $scope.showingResult = true;
            lookupService.wordLookup(word.word_lookup_word, function(result){
                $scope.word.lookupResult = result;
                if(result.sound){
                    $scope.word.lookupResult.listen = function(){
                        angular.element('#lookup-listen-audio').get(0).play();
                    }
                }
            });

            // hide the hint
            $scope.hint.showing = false;
        }

        $scope.giveHint = function(word){
            $scope.hint.showing = true;
            $scope.hint.loading = true;

            imageSearchService.search(word.word_lookup_word, function(result){
                $timeout(function() {
                    $scope.hint.loading = false;
                    $scope.hint.images = result;
                });
            });
        }

        // user clicks on correct or wrong
        $scope.answerQuestion = function(word, answer){

            word.word_lookup_quiz_taken = 1;
            word.word_lookup_quiz_result = answer ? 1 : 0;

            $http({
                url: "http://localhost:3000/word-lookup/" + word.word_lookup_id,
                method: "POST",
                data: word
            }).success(function(data, status, headers, config) {

                    console.log(data);

                    // reset hint
                    $scope.hint = {
                        showing: false,
                        loading: false,
                        images: []
                    };

                    // remove the answer for previous word
                    $scope.word.answer = '';

                    // show the next word
                    $scope.wordCursor++;
                    $scope.word = $scope.words[$scope.wordCursor];

                    // hide previous word's result screen
                    $scope.showingResult = false;

                    // if answered all the questions, finish the quiz
                    if($scope.words.length == $scope.wordCursor)
                        $scope.finished = true;

            });
        }

        $scope.finish = function () {
            $modalInstance.close();
        };

        $scope.cancel = function () {
            $modalInstance.dismiss();
        };
    };


});
<commit_msg>updated word-lookup controller<commit_after>mainApp.controller('WordLookupCtrl', function ($scope, $http, $modal, $route, lookupService, imageSearchService, $timeout) {

	// changes selectedBook based on user selection
    $scope.selectBook = function(book){
        $scope.selectedBook = book;
    }

	// list words from selected book
    $scope.getSelectedBookWords = function(){
        if($scope.selectedBook){
            var result = [];
            for(var i = 0; i < $scope.words.length; i++){
                if($scope.selectedBook.book_name == $scope.words[i].book_name){
                    result.push($scope.words[i])
                }
            }
            return result;
        }
        else
            return $scope.words;
    }

    $http({method: 'GET', url: 'http://localhost:3000/user/1/word-lookup'}).success(function(response, status, headers, config) {
        $scope.words = response.result;
    });

    $http({method: 'GET', url: 'http://localhost:3000/user/1/word-lookup/books'}).success(function(response, status, headers, config) {
        $scope.books = response.result;
    });

	// time to take a quiz,
	// send words that user will be take quiz on
    $scope.quizWords = function() {

        var notTakenQuiz = [];
        var words = $scope.getSelectedBookWords();

        angular.forEach(words, function(word){
            if(!word.word_lookup_quiz_taken)
                this.push(word);
        }, notTakenQuiz);

        return notTakenQuiz;
    };

	// start taking a quiz
    $scope.startQuiz = function(words) {

        var modalInstance = $modal.open({
            templateUrl: './views/partial/quiz.html',
            controller: ModalInstanceCtrl,
            resolve: {
                words: function () {
                    return words;
                }
            }
        });

        modalInstance.result.then(function () {
            $route.reload();
        });
    };

    var ModalInstanceCtrl = function ($scope, $modalInstance, words) {

        // init variables
        $scope.wordCursor = 0;
        $scope.words = words;
        $scope.word = $scope.words[0];
        $scope.showingResult = false;

        $scope.hint = {
            showing: false,
            loading: false,
            images: []
        };

        // show meaning
        $scope.showMeaning = function(word){
            $scope.showingResult = true;
            lookupService.wordLookup(word.word_lookup_word, function(result){
                $scope.word.lookupResult = result;
                if(result.sound){
                    $scope.word.lookupResult.listen = function(){
                        angular.element('#lookup-listen-audio').get(0).play();
                    }
                }
            });

            // hide the hint
            $scope.hint.showing = false;
        }

        $scope.giveHint = function(word){
            $scope.hint.showing = true;
            $scope.hint.loading = true;

            imageSearchService.search(word.word_lookup_word, function(result){
                $timeout(function() {
                    $scope.hint.loading = false;
                    $scope.hint.images = result;
                });
            });
        }

        // user clicks on correct or wrong
        $scope.answerQuestion = function(word, answer){

            word.word_lookup_quiz_taken = 1;
            word.word_lookup_quiz_result = answer ? 1 : 0;

            $http({
                url: "http://localhost:3000/word-lookup/" + word.word_lookup_id,
                method: "POST",
                data: word
            }).success(function(data, status, headers, config) {

                    console.log(data);

                    // reset hint
                    $scope.hint = {
                        showing: false,
                        loading: false,
                        images: []
                    };

                    // remove the answer for previous word
                    $scope.word.answer = '';

                    // show the next word
                    $scope.wordCursor++;
                    $scope.word = $scope.words[$scope.wordCursor];

                    // hide previous word's result screen
                    $scope.showingResult = false;

                    // if answered all the questions, finish the quiz
                    if($scope.words.length == $scope.wordCursor)
                        $scope.finished = true;

            });
        }

        $scope.finish = function () {
            $modalInstance.close();
        };

        $scope.cancel = function () {
            $modalInstance.dismiss();
        };
    };


});

{'fraction_non_alphanumeric': 0.12173532947218883, 'fraction_numerical': 0.004589662331985576, 'mean_word_length': 1.4128658054310572, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5391599', 'n_tokens_mistral': 2476, 'n_tokens_neox': 2292, 'n_words': 449}