Ejemplo n.º 1
0
    def assure_valid(self):
        if self.validated:
            return
        par_cnt = 0
        in_par = False
        for c in self.characters_and_words:
            if self.__is_left_parenthesis(c):
                par_cnt = par_cnt + 1
                word_len = 0
                if in_par:
                    self.raise_unexpected_parenthesis_exception(par_cnt)
                in_par = True
            elif self.__is_right_parenthesis(c):
                par_cnt = par_cnt + 1
                if not in_par:
                    self.raise_unexpected_parenthesis_exception(par_cnt)
                elif word_len < self.MIN_WORD_LEN:
                    raise GenException('Words must be at least ' + \
                                        str(self.MIN_WORD_LEN) + \
                                        ' characters long')
                elif word_len > self.MAX_WORD_LEN:
                    raise GenException('Words cannot be longer than ' + \
                                        str(self.MAX_WORD_LEN) + \
                                        ' characters')
                in_par = False
            elif in_par:
                word_len = word_len + 1

        if in_par:
            self.raise_unexpected_parenthesis_exception(par_cnt)
        self.validated = True
Ejemplo n.º 2
0
Archivo: gen.py Proyecto: rbadin/cwg
def generate_infos(makemeahanzi_path, cedict_path, working_dir, characters):
    if (len(characters) == 0):
        raise GenException('No characters provided')
    manager = WordManager(characters, cedict_path)
    characters = manager.get_characters()
    words = manager.get_words()
    if len(characters) > MAX_INPUT_CHARACTERS:
        raise GenException('Maximum number of characters exceeded (' + \
                str(len(characters)) + \
                '/' + str(MAX_INPUT_CHARACTERS) + ')')

    generate_character_infos(working_dir, characters, makemeahanzi_path)
    generate_word_infos(working_dir, words)
Ejemplo n.º 3
0
Archivo: gen.py Proyecto: rbadin/cwg
def get_guide(guide_str):
    if guide_str == '' or guide_str == Guide.NONE.name.lower():
        return Guide.NONE
    elif guide_str == Guide.STAR.name.lower():
        return Guide.STAR
    elif guide_str == Guide.CROSS.name.lower():
        return Guide.CROSS
    else:
        raise GenException('Invalid guide ' + guide_str)
Ejemplo n.º 4
0
Archivo: gen.py Proyecto: rbadin/cwg
def create_radical_svg(dataset_path, working_dir, character_info):
    radical = character_info.radical

    g = get_graphics_json(dataset_path, radical)
    if g == -1:
        raise GenException('Could not find data for radical ' + radical)
    stroke_order = g['strokes']
    create_stroke_svg(working_dir, radical, stroke_order, \
                        len(stroke_order))
Ejemplo n.º 5
0
Archivo: gen.py Proyecto: rbadin/cwg
def generate_character_infos(working_dir, characters, makemeahanzi_path):
    with open(os.path.join(working_dir, CHARACTERS_FILE), 'w') as cf:
        for i in range(len(characters)):
            character = characters[i]
            info = retrieve_info(makemeahanzi_path, character)
            if info == -1:
                raise GenException('Could not find data for character ' + \
                        character)
            j = info.toJSON()
            cf.write(j + '\n')
Ejemplo n.º 6
0
def combine_and_shorten_definition(definition, sep, max_w, font_name,
                                   font_size):
    if len(definition) == 0:  # TODO!
        raise GenException('Definition is too long and could not be shortened')
    text = sep.join(definition)
    w = stringWidth(text, font_name, font_size)
    if w <= max_w:
        return Result(text, len(definition))
    return combine_and_shorten_definition(definition[0:-1], sep, max_w,
                                          font_name, font_size)
Ejemplo n.º 7
0
def generate_sheet(makemeahanzi_path, working_dir, title, guide):
    print("Generating")
    if len(title) > MAX_TITLE_LENGTH:
        raise GenException('Title length exceeded (' + str(len(title)) + \
                '/' + str(MAX_TITLE_LENGTH) + ')')

    character_infos = []
    words = []

    character_infos = load_data_from_json_file(working_dir, CHARACTERS_FILE, \
                                                object_to_character_info)
    words = load_data_from_json_file(working_dir, WORDS_FILE, Word.fromJSON)
    words = filter_out_words_with_empty_definition(words)

    c = canvas.Canvas(os.path.join(working_dir, SHEET_FILE), PAGE_SIZE)
    pdfmetrics.registerFont(TTFont(FONT_NAME, FONT_NAME + '.ttf'))

    words_with_spanning_translation = get_spanning_translations( \
                                        character_infos, words)

    # draw_header(c, title, HEADER_FONT_SIZE, \
    #         PAGE_SIZE[1]-HEADER_PADDING);
    for i in range(len(character_infos)):
        i_mod = i % CHARACTERS_PER_PAGE
        page_number = int(i / CHARACTERS_PER_PAGE + 1)
        if i != 0 and i_mod == 0:
            # draw_footer(c, FOOTER_FONT_SIZE, y-CHARACTER_ROW_HEIGHT - \
            #         GRID_OFFSET/2);
            draw_page_number(c, i / CHARACTERS_PER_PAGE, PAGE_NUMBER_FONT_SIZE)
            draw_words(c, character_infos, words, page_number-1, \
                        words_with_spanning_translation)
            c.showPage()
            # draw_header(c, title, HEADER_FONT_SIZE, \
            #         PAGE_SIZE[1]-HEADER_PADDING);
        info = character_infos[i]
        create_character_svg(working_dir, info)
        #create_radical_svg(makemeahanzi_path, working_dir, info);
        create_stroke_order_svgs(working_dir, info)
        convert_svgs_to_pngs(working_dir)
        y = FIRST_CHARACTER_ROW_Y - i_mod * CHARACTER_ROW_HEIGHT
        draw_character_row(working_dir, c, info, y, guide)
        delete_files(working_dir, '.*\.svg')
        delete_files(working_dir, '.*\.png')

    y = PAGE_SIZE[1]-HEADER_PADDING-GRID_OFFSET/2 - \
        (CHARACTERS_PER_PAGE-1)*CHARACTER_ROW_HEIGHT
    # TODO: extract
    # draw_footer(c, FOOTER_FONT_SIZE, y-CHARACTER_ROW_HEIGHT - \
    #                 GRID_OFFSET/2);
    draw_page_number(c, page_number, PAGE_NUMBER_FONT_SIZE)
    draw_words(c, character_infos, words, page_number, \
            words_with_spanning_translation)
    c.setTitle(title)
    c.showPage()
    c.save()
Ejemplo n.º 8
0
 def translate(self, chinese):
     lo = 0
     hi = len(self.line_offset) - 1
     while lo <= hi:
         mid = (int)((lo + hi) / 2)
         self.dictionary.seek(self.line_offset[mid])
         line = self.dictionary.readline()
         characters = self.__parse_characters(line)
         if characters < chinese:
             lo = mid + 1
         elif characters > chinese:
             hi = mid - 1
         else:
             return self.__parse_definition(line)
     raise GenException('Word ' + chinese + ' was not found in dictionary')
Ejemplo n.º 9
0
Archivo: gen.py Proyecto: rbadin/cwg
def retrieve_info(dataset_path, character):
    d = get_dictionary_json(dataset_path, character)
    g = get_graphics_json(dataset_path, character)

    if d == -1 or g == -1:
        return -1

    try:
        radical = d['radical']
        pinyin = d['pinyin']

        r = get_dictionary_json(dataset_path, radical)

        if r == -1:
            return -1

        radical_pinyin = r['pinyin']
        definition = d['definition']

        stroke_order = g['strokes']
        return character_info(character, radical, pinyin, radical_pinyin, \
                definition, stroke_order)
    except KeyError:
        raise GenException('Invalid dataset data for character ' + character)
Ejemplo n.º 10
0
 def raise_unexpected_parenthesis_exception(self, par_cnt):
     raise GenException('Unexpected parenthesis #' + str(par_cnt))