Exemple #1
0
def __write_word_encoding_result(dic, word_kana, word_kanji):
    """
    Try to get the result of the encoding and write it so we have
    something to work with.  If we can't get it, then just error
    out and write a blank result.

    dic (Dictionary object): dictionary to use to get the page.
    word_kana/kanji (unicode): words in kana and kanji to parse result of.

    Writes DictionaryName/WORDKANA_WORDKANJI.result.json file.
    """
    _, _, html_abs_path, json_abs_path, html_rel_path, json_rel_path = get_paths_for_word(dic, word_kana, word_kanji)

    html = get_html_for_word(dic, word_kana, word_kanji)

    with codecs.open(json_abs_path, "w", "utf8") as f:
        try:
            result = dic.lookup(word_kanji, word_kana, html)
            jsonable = result.to_jsonable()
            json.dump(jsonable, f, encoding="utf8", sort_keys=True, indent=4, ensure_ascii=False)
            print(u"Wrote result file: %s" % json_rel_path)
        except:
            traceback.print_exc()
            print(u"Error occured when parsing %s (%s) in %s." % (word_kanji, word_kana, dic.short_name))
            print(u'Writing template file "%s".' % json_rel_path)
            print(u"You need to go in and edit the information manually.")

            url = dic._create_url(word_kanji, word_kana)
            example_sentence = ExampleSentence(u"***JAPANESE SENTENCE***", u"***ENGLISH SENTENCE***")
            part1 = DefinitionPart(u"***PART 1***")
            part2 = DefinitionPart(u"***PART 2***")
            definition = Definition([part1, part2], [example_sentence])
            result = Result(dic, word_kanji, word_kana, url, u"***KANJI***", u"***KANA***", u"", [definition])
            jsonable = result.to_jsonable()
            json.dump(jsonable, f, encoding="utf8", sort_keys=True, indent=4, ensure_ascii=False)
def checkword(dictionary, kanji, kana):
    """
    dictionary (Dictionary): the dictionary we want to check in
    kanji (unicode): the kanji we want to check
    kana (unicode): the kana we want to check
    """
    html = manage_words.get_html_for_word(dictionary, kana, kanji)
    json_object = manage_words.get_json_for_word(dictionary, kana, kanji)

    html_parse_result = dictionary.lookup(kanji, kana, html)
    json_result = Result.from_jsonable(dictionary, json_object)

    if (json_result != html_parse_result):
        print_differences_result(json_result, html_parse_result, "JSON result", "HTML result")
        assert(json_result == html_parse_result)