<commit_before>package org.ifcasl.destress

class UiController {


    def index() {
        def id = params['id']
        def ex = Exercise.get(id)
        def cFG = WordUtterance.createCriteria()
        //def fgUtts = WordUtterance.findAllByWord(ex.word)
        def fgUtts = cFG {
            and {
                eq("word",ex.word)
                sentenceUtterance {
                    speaker {
                        eq("nativeLanguage", Language.F)
                    }
                }
            }
        }

        def ggUtts
        def nRefs = ex.diagnosisMethod.numberOfReferences
        if (nRefs > 0) {
            if (ex.diagnosisMethod.selectionType == SelectionType.MANUAL) {

                // def refType = ex.diagnosisMethod.referenceType
                // if (refType!=ReferenceType.ABSTRACT)

                if (nRefs > 0) {
                    def cGG = WordUtterance.createCriteria()
                    ggUtts = cGG {
                        and {
                            eq("word",ex.word)
                            sentenceUtterance {
                                speaker {
                                    eq("nativeLanguage", Language.G)
                                }
                            }
                        }
                    }
                }
            }
        }


        render(view:"exercise", model:[ex:ex,
                                       fgUtts:fgUtts,
                                       ggUtts:ggUtts,
                                       nRefs:(1..nRefs)])
    }

    def record() {
    }

    def upload() {

    }

    def download(long id) {
        SentenceUtterance utt = SentenceUtterance.get(id)
        File file = new File(utt.waveFile)
        response.setContentType('audio/wav')
        response.setContentLength (file.bytes.length)
        response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")
        response.outputStream << file.newInputStream() // Performing a binary stream copy

        return false
    }

    def downloadFeedback(long id) {
        Diagnosis diag = Diagnosis.get(id)
        def path = grailsApplication.mainContext.servletContext.getRealPath("/") + "audio/feedback/" + diag.feedbackWaveFile
        File file = new File(path)
        response.setContentType('audio/wav')
        response.setContentLength (file.bytes.length)
        response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")
        response.outputStream << file.newInputStream() // Performing a binary stream copy

        return false
    }

    def selfassess() {
        def ex = Exercise.get(params['id'])
        def studUtt = WordUtterance.get(params['fgUtts.id'])
        def studWav = studUtt.sentenceUtterance.sampleName + ".wav"

        def refUtts = getRefUtts(ex, studUtt)

        [ex:ex,studUtt:studUtt,refUtts:refUtts,studWav:studWav]

    }

    def List getRefUtts(Exercise ex, WordUtterance studUtt) {
        def refUtts = []
        def nRefs = ex.diagnosisMethod.numberOfReferences
        println("PARAMS: " + params)
        if (nRefs > 0) {
            if (ex.diagnosisMethod.selectionType == SelectionType.AUTO) {
                refUtts = DiagnosisUtil.findNBestRefUtts(studUtt, nRefs)
            }
            else if (ex.diagnosisMethod.selectionType == SelectionType.MANUAL) {
                for (i in 1..nRefs) {
                    def refUtt = WordUtterance.get(params['ggUtts.' + i])
                    refUtts.add(refUtt)
                }
            }
            else { // selectionType == FIXED
                render("FIXED selectionType not yet implemented!")
            }
        }
        return refUtts
    }

    def feedback() {
        def ex = Exercise.get(params['id'])

        //def basePath = grailsApplication.mainContext.servletContext.getRealPath("/web-app/")

        def studUtt = WordUtterance.get(params['fgUtts.id'])
        def studWav = studUtt.sentenceUtterance.sampleName + ".wav"

        def refUtts = []
        //def refWavs = []
        def nRefs = ex.diagnosisMethod.numberOfReferences

        def diag

        if (nRefs > 0) {
            refUtts = getRefUtts(ex, studUtt)
            println("REFUTTS: " + refUtts)

            ///// Moved to getRefUtts()
            // if (ex.diagnosisMethod.selectionType == SelectionType.AUTO) {
            //     refUtts = DiagnosisUtil.findNBestRefs(studUtt.sentenceUtterance.speaker, nRefs)
            // }
            // else if (ex.diagnosisMethod.selectionType == SelectionType.MANUAL) {
            //
            //
            //     for (i in 1..nRefs) {
            //         def refUtt = WordUtterance.get(params['ggUtts.' + i])
            //         //def refWav = grailsApplication.parentContext.getResource("/audio/" + refUtt.sentenceUtterance.sampleName + ".wav")
            //         //def refWav = refUtt.sentenceUtterance.sampleName + ".wav"
            //
            //         //TODO validate that input from multiple refUtts selects are not the same utterance
            //
            //         refUtts.add(refUtt)
            //         //refWavs.add(refWav)
            //     }
            // }
            // else { // selectionType == FIXED
            //     render("FIXED selectionType not yet implemented!")
            // }

            diag = DiagnosisUtil.getComparisonDiagnosis(ex, studUtt, refUtts)
        }
        else { //nRefs <= 0
            //WekaUtil.getInstance(studUtt)
            //render("Weka diag not implemented yet")

            diag = DiagnosisUtil.getClassificationDiagnosis(ex, studUtt)

        }


        //TODO set self-assessment info
        diag.selfAssessedStressLabel = params['stressLabel']
        diag.selfAssessedStressClarity = params['clearEnough']
        diag.selfAssessedAdvice = params['advice']
        diag.save()
        println("selfAssessedStressLabel: " + diag.selfAssessedStressLabel)
        println("selfAssessedStressClarity: " + diag.selfAssessedStressClarity)
        println("selfAssessedAdvice: " + diag.selfAssessedAdvice)


        // get label color & message
        def labelCol
        def labelMsg
        if (diag.label) {
            labelCol = DiagnosisUtil.getColor(diag.label)
            if (ex.feedbackMethod.displayMessages) {
                labelMsg = DiagnosisUtil.getClassificationMessage(diag)
            }
        }<commit_msg>Fixed self-assessment info not saving (closes #40)<commit_after>package org.ifcasl.destress

class UiController {


    def index() {
        def id = params['id']
        def ex = Exercise.get(id)
        def cFG = WordUtterance.createCriteria()
        //def fgUtts = WordUtterance.findAllByWord(ex.word)
        def fgUtts = cFG {
            and {
                eq("word",ex.word)
                sentenceUtterance {
                    speaker {
                        eq("nativeLanguage", Language.F)
                    }
                }
            }
        }

        def ggUtts
        def nRefs = ex.diagnosisMethod.numberOfReferences
        if (nRefs > 0) {
            if (ex.diagnosisMethod.selectionType == SelectionType.MANUAL) {

                // def refType = ex.diagnosisMethod.referenceType
                // if (refType!=ReferenceType.ABSTRACT)

                if (nRefs > 0) {
                    def cGG = WordUtterance.createCriteria()
                    ggUtts = cGG {
                        and {
                            eq("word",ex.word)
                            sentenceUtterance {
                                speaker {
                                    eq("nativeLanguage", Language.G)
                                }
                            }
                        }
                    }
                }
            }
        }


        render(view:"exercise", model:[ex:ex,
                                       fgUtts:fgUtts,
                                       ggUtts:ggUtts,
                                       nRefs:(1..nRefs)])
    }

    def record() {
    }

    def upload() {

    }

    def download(long id) {
        SentenceUtterance utt = SentenceUtterance.get(id)
        File file = new File(utt.waveFile)
        response.setContentType('audio/wav')
        response.setContentLength (file.bytes.length)
        response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")
        response.outputStream << file.newInputStream() // Performing a binary stream copy

        return false
    }

    def downloadFeedback(long id) {
        Diagnosis diag = Diagnosis.get(id)
        def path = grailsApplication.mainContext.servletContext.getRealPath("/") + "audio/feedback/" + diag.feedbackWaveFile
        File file = new File(path)
        response.setContentType('audio/wav')
        response.setContentLength (file.bytes.length)
        response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")
        response.outputStream << file.newInputStream() // Performing a binary stream copy

        return false
    }

    def selfassess() {
        def ex = Exercise.get(params['id'])
        def studUtt = WordUtterance.get(params['fgUtts.id'])
        def studWav = studUtt.sentenceUtterance.sampleName + ".wav"

        def refUtts = getRefUtts(ex, studUtt)

        [ex:ex,studUtt:studUtt,refUtts:refUtts,studWav:studWav]

    }

    def List getRefUtts(Exercise ex, WordUtterance studUtt) {
        def refUtts = []
        def nRefs = ex.diagnosisMethod.numberOfReferences
        println("PARAMS: " + params)
        if (nRefs > 0) {
            if (ex.diagnosisMethod.selectionType == SelectionType.AUTO) {
                refUtts = DiagnosisUtil.findNBestRefUtts(studUtt, nRefs)
            }
            else if (ex.diagnosisMethod.selectionType == SelectionType.MANUAL) {
                for (i in 1..nRefs) {
                    def refUtt = WordUtterance.get(params['ggUtts.' + i])
                    refUtts.add(refUtt)
                }
            }
            else { // selectionType == FIXED
                render("FIXED selectionType not yet implemented!")
            }
        }
        return refUtts
    }

    def feedback() {
        def ex = Exercise.get(params['id'])

        //def basePath = grailsApplication.mainContext.servletContext.getRealPath("/web-app/")

        def studUtt = WordUtterance.get(params['fgUtts.id'])
        def studWav = studUtt.sentenceUtterance.sampleName + ".wav"

        def refUtts = []
        //def refWavs = []
        def nRefs = ex.diagnosisMethod.numberOfReferences

        def diag

        if (nRefs > 0) {
            refUtts = getRefUtts(ex, studUtt)
            println("REFUTTS: " + refUtts)

            ///// Moved to getRefUtts()
            // if (ex.diagnosisMethod.selectionType == SelectionType.AUTO) {
            //     refUtts = DiagnosisUtil.findNBestRefs(studUtt.sentenceUtterance.speaker, nRefs)
            // }
            // else if (ex.diagnosisMethod.selectionType == SelectionType.MANUAL) {
            //
            //
            //     for (i in 1..nRefs) {
            //         def refUtt = WordUtterance.get(params['ggUtts.' + i])
            //         //def refWav = grailsApplication.parentContext.getResource("/audio/" + refUtt.sentenceUtterance.sampleName + ".wav")
            //         //def refWav = refUtt.sentenceUtterance.sampleName + ".wav"
            //
            //         //TODO validate that input from multiple refUtts selects are not the same utterance
            //
            //         refUtts.add(refUtt)
            //         //refWavs.add(refWav)
            //     }
            // }
            // else { // selectionType == FIXED
            //     render("FIXED selectionType not yet implemented!")
            // }

            diag = DiagnosisUtil.getComparisonDiagnosis(ex, studUtt, refUtts)
        }
        else { //nRefs <= 0
            //WekaUtil.getInstance(studUtt)
            //render("Weka diag not implemented yet")

            diag = DiagnosisUtil.getClassificationDiagnosis(ex, studUtt)

        }


        //TODO set self-assessment info
        diag.selfAssessedStressLabel = params['stressLabel']
        diag.selfAssessedStressClarity = params['clearEnough']
        diag.selfAssessedAdvice = params['advice']
        diag.save(flush:true)
        println("selfAssessedStressLabel: " + diag.selfAssessedStressLabel)
        println("selfAssessedStressClarity: " + diag.selfAssessedStressClarity)
        println("selfAssessedAdvice: " + diag.selfAssessedAdvice)


        // get label color & message
        def labelCol
        def labelMsg
        if (diag.label) {
            labelCol = DiagnosisUtil.getColor(diag.label)
            if (ex.feedbackMethod.displayMessages) {
                labelMsg = DiagnosisUtil.getClassificationMessage(diag)
            }
        }
{'fraction_non_alphanumeric': 0.10315430520034101, 'fraction_numerical': 0.0013950244129272262, 'mean_word_length': 1.5451676528599605, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6747109', 'n_tokens_mistral': 3877, 'n_tokens_neox': 3607, 'n_words': 636}