def WriteData(input_path, output_path): outputs = [] with open(input_path) as input_stream: input_stream = code_generator_util.SkipLineComment(input_stream) input_stream = code_generator_util.ParseColumnStream(input_stream, num_column=3) # ex. (value, error, correction) = ("雰囲気", "ふいんき", "ふんいき") for value, error, correction in input_stream: outputs.append([value, error, correction]) # In order to lookup the entries via |error| with binary search, # sort outputs here. outputs.sort(lambda x, y: cmp(x[1], y[1]) or cmp(x[0], y[0])) with open(output_path, 'w') as output_stream: output_stream.write('static const ReadingCorrectionItem ' 'kReadingCorrections[] = {\n') for output in outputs: (value, error, correction) = output output_stream.write(' // %s, %s, %s\n' % (value, error, correction)) output_stream.write( code_generator_util.FormatWithCppEscape( ' { %s, %s, %s },\n', value, error, correction)) output_stream.write('};\n')
def WriteVariantInfo(variant_info, stream): """Writes single kanji variants info.""" (variant_types, variant_items) = variant_info stream.write('static const char *kKanjiVariantTypes[] = {\n') for variant_type in variant_types: stream.write(code_generator_util.FormatWithCppEscape( ' %s,', variant_type)) stream.write(' // %s\n' % variant_type) stream.write('};\n') stream.write('static const KanjiVariantItem kKanjiVariants[] = {\n') for item in variant_items: (target, original, variant_type) = item stream.write(code_generator_util.FormatWithCppEscape( ' { %s, %s, %d },', target, original, variant_type)) stream.write(' // %s, %s, %d\n' % (target, original, variant_type)) stream.write('};\n')
def WriteSingleKanji(outputs, stream): """Writes single kanji list for readings.""" stream.write('static const SingleKanjiList kSingleKanjis[] = {\n') for output in outputs: (key, values) = output stream.write(' // %s, %s\n' % (key, values)) stream.write(code_generator_util.FormatWithCppEscape( ' { %s, %s },\n', key, values)) stream.write('};\n')