def g_eval(groundtruth_textgrid_filename, georgi_alignment_filename, tolerance):

    boundaryList                = georgiParser.syllables_total_parser(georgi_alignment_filename)

    try:

        utteranceList               = textgridParser.textGrid2WordList(groundtruth_textgrid_filename, whichTier='dian', utf16=False)

        utteranceDuration           = textgridParser.textGrid2WordList(groundtruth_textgrid_filename, whichTier='dianDuration', utf16=False)

    except:
        utteranceList               = textgridParser.textGrid2WordList(groundtruth_textgrid_filename, whichTier='dian', utf16=True)

        utteranceDuration           = textgridParser.textGrid2WordList(groundtruth_textgrid_filename, whichTier='dianDuration', utf16=True)

    # remove empty dian
    tempGroundtruthList         = []
    groundtruthDuration         = []
    for idx, utterance in enumerate(utteranceList):
        if len(utterance[2].strip()):# and float(utteranceDuration[idx][2]):
            tempGroundtruthList.append(utterance)
            groundtruthDuration.append(float(utteranceDuration[idx][2]))

    # remove 0 duration
    detectedBoundaryList    = []
    groundtruthList         = []
    for idx, utterance in enumerate(tempGroundtruthList):
        if groundtruthDuration[idx]:
            groundtruthList.append(tempGroundtruthList[idx])
            detectedBoundaryList.append(boundaryList[idx])

    numDetectedBoundaries, numGroundtruthBoundaries, numCorrect, numOnsetCorrect, numOffsetCorrect, numInsertion, numDeletion = \
        evaluation.boundaryEval(groundtruthList,detectedBoundaryList,tolerance)

    print "Detected: {0}, Ground truth: {1}, Correct: {2}, Onset correct: {3}, " \
                              "Offset correct: {4}, Insertion: {5}, Deletion: {6}\n".\
                            format(numDetectedBoundaries, numGroundtruthBoundaries,numCorrect, numOnsetCorrect,
                                   numOffsetCorrect, numInsertion, numDeletion)

    return  numDetectedBoundaries, numGroundtruthBoundaries, numCorrect, numOnsetCorrect, numOffsetCorrect, numInsertion, numDeletion
def batch_eval(aCapella_root, dataset_path, annotation_path, segPhrase_path, segSyllable_path, score_path, recordings, tolerance):

    sumDetectedBoundaries, sumGroundtruthPhrases, sumGroundtruthBoundaries, sumCorrect, sumOnsetCorrect, \
    sumOffsetCorrect, sumInsertion, sumDeletion = 0 ,0 ,0 ,0 ,0 ,0, 0, 0

    for i_recording, recording_name in enumerate(recordings):

        groundtruth_textgrid_file   = os.path.join(aCapella_root, dataset_path, annotation_path, recording_name+'.TextGrid')
        phrase_boundary_lab_file    = os.path.join(aCapella_root, dataset_path, segPhrase_path,  recording_name+'.lab')
        detected_lab_file_head      = os.path.join(aCapella_root, dataset_path, segSyllable_path,recording_name)
        score_file                  = os.path.join(aCapella_root, dataset_path, score_path,      recording_name+'.csv')

        if not os.path.isfile(score_file):
            print 'Score not found: ' + score_file
            continue

        lineList                    = textgridParser.textGrid2WordList(groundtruth_textgrid_file, whichTier='line')
        utteranceList               = textgridParser.textGrid2WordList(groundtruth_textgrid_file, whichTier='dianSilence')

        # parse lines of groundtruth
        nestedUtteranceLists, numLines, numUtterances   = textgridParser.wordListsParseByLines(lineList, utteranceList)

        # parse score
        utterance_durations, bpm                        = scoreParser.csvDurationScoreParser(score_file)


        # syllable boundaries groundtruth of each line
        for idx, list in enumerate(nestedUtteranceLists):
            if int(bpm[idx]):
                print 'Evaluating... ' + recording_name + ' phrase ' + str(idx+1)

                ul = list[1]
                firstStartTime          = ul[0][0]
                groundtruthBoundaries   = [(np.array(ul_element[:2]) - firstStartTime).tolist() for ul_element in ul]

                detected_syllable_lab   = detected_lab_file_head+'_'+str(idx+1)+'.syll.lab'
                if not os.path.isfile(detected_syllable_lab):
                    print 'Syll lab file not found: ' + detected_syllable_lab
                    continue

                # read boundary detected lab into python list
                detectedBoundaries          = labParser.lab2WordList(detected_syllable_lab)

                # read boundary groundtruth textgrid into python list

                # for segment in utteranceList:
                #     asciiLine = segment[2].encode("ascii", "replace")
                #     if len(asciiLine.replace(" ", "")):
                #         groundtruthBoundaries.append(segment[0:2])
                #
                # print groundtruthBoundaries

                #
                numDetectedBoundaries, numGroundtruthBoundaries, numCorrect, numOnsetCorrect, numOffsetCorrect, \
                numInsertion, numDeletion = evaluation.boundaryEval(groundtruthBoundaries, detectedBoundaries, tolerance)

                sumDetectedBoundaries       += numDetectedBoundaries
                sumGroundtruthBoundaries    += numGroundtruthBoundaries
                sumGroundtruthPhrases       += 1
                sumCorrect                  += numCorrect
                sumOnsetCorrect             += numOnsetCorrect
                sumOffsetCorrect            += numOffsetCorrect
                sumInsertion                += numInsertion
                sumDeletion                 += numDeletion

                if numCorrect/float(numGroundtruthBoundaries) < 0.7:
                    print "Detected: {0}, Ground truth: {1}, Correct: {2}, Onset correct: {3}, " \
                          "Offset correct: {4}, Insertion: {5}, Deletion: {6}\n".\
                        format(numDetectedBoundaries, numGroundtruthBoundaries,numCorrect, numOnsetCorrect,
                               numOffsetCorrect, numInsertion, numDeletion)

    return sumDetectedBoundaries, sumGroundtruthBoundaries, sumGroundtruthPhrases, sumCorrect, sumOnsetCorrect, \
           sumOffsetCorrect, sumInsertion, sumDeletion