%run "../Static data/English localization.ipynb"

"""
Explanation: French localization
End of explanation
"""


processGFormFR = not ('gformFR' in globals())
if processGFormFR:
    try:
        gformFR = pd.read_csv(\
                        gformPath + dataFilesNamesStem + genericFormName + frSuffix + csvSuffix,\
                        dtype=str,\
                        parse_dates=['Timestamp'],\
                        date_parser=dateparseGForm,\
                         )
        print("gformFR read_csv success")
    except FileNotFoundError:
        print("gformFR read_csv failed")

"""
Explanation: Preparation


Additional columns


Language column


Preparation for temporalities




List of localized possible answers


List of answers


Scientific questions


Demographic questions




Preparation
<a id=prep />
cf English localization.ipynb
http://localhost:8888/notebooks/v1.52/Static%20data/English%20localization.ipynb#
End of explanation
"""


if processGFormFR:
    gformFR['Language'] = pd.Series(frLanguageID, index=gformFR.index)

"""
Explanation: Additional columns
<a id=addcol />
Language column
<a id=lang />
End of explanation
"""


if processGFormFR:
    gformFR['Temporality'] = pd.Series(answerTemporalities[2], index=gformFR.index)

"""
Explanation: Preparation for temporalities
<a id=temp />
End of explanation
"""


if processGFormFR:
    questionsAnswersTranslationsFR = pd.read_csv(frTranslationsPath)

    questionsAnswersTranslationsFR.index = questionsAnswersTranslationsFR[questionsAnswersTranslationsFR.columns[0]].values
    questionsAnswersTranslationsFR = questionsAnswersTranslationsFR.T.iloc[1]

    questionsAnswersTranslationsFR = pd.Series([[]],index = list(['Timestamp'])).append(questionsAnswersTranslationsFR)

"""
Explanation: List of localized possible answers
<a id=frtranspos />
Loading, normalization of index and columns
End of explanation
"""


if processGFormFR:
    replacementToken = '$$$$'

    def replaceInWords(word, _replacement = ''):
        return '"' + word.group(1) + word.group(2) + _replacement + "'" + word.group(3) + word.group(4) + '"'

    def prepareForDictJsonLoading(input_string):
        # replace in-words \' by $$$$'
        #print(input_string)
        previous = ''
        while(previous != input_string):
            previous = input_string
            input_string = re.sub('"(.*)(\w+)\'(\w+)(.*)"', lambda w: replaceInWords(w, _replacement=replacementToken), input_string, re.UNICODE)

        #print(input_string)
        # replace all \' by \"
        input_string = input_string.replace("'", '"')

        #print(input_string)
        # replace back in-words $$$$\" to \'
        #input_string = re.sub('"(.*)(\w+)$$$$"(\w+)(.*)"', lambda w: replaceInWords(w, _replacement = '@'), input_string, re.UNICODE)
        previous = ''
        while(previous != input_string):
            previous = input_string
            input_string = input_string.replace(replacementToken + '"', '\'')

        return input_string

    def string_to_dict(dict_string):
        #print("loading " + dict_string)
        return json.loads(dict_string)

#questionsAnswersTranslationsFR.loc['Do you play video games?']

#string_to_dict(prepareForDictJsonLoading(questionsAnswersTranslationsFR.loc['Do you play video games?']))

if processGFormFR:
    for label in questionsAnswersTranslationsFR.index:
        #print(label)
        val = questionsAnswersTranslationsFR.loc[label]
        if(type(val) is str):
            _dict = string_to_dict(prepareForDictJsonLoading(val))
            #print(val)
            #print(_dict)
            questionsAnswersTranslationsFR.loc[label] = [_dict]

"""
Explanation: Loading, normalization of index and columns
End of explanation
"""


#correctAnswersFR = pd.read_csv(frCorrectScientificAnswersPath)

"""
Explanation: List of correct answers
<a id=frform />
Scientific questions
<a id=frsciform />
End of explanation
"""


#demographicAnswersFR = pd.read_csv(frDemographicAnswersPath)

"""
Explanation: Demographic questions
<a id=frdemform />
End of explanation
"""


