示例#1
0
def __parse_kana(kana: Element) -> dict:
    text = kana.find('reb').text

    priorities = kana.findall('re_pri')
    is_common = __is_common(priorities)

    tags = []
    for info in kana.findall('re_inf'):
        tags.append(convert_tag(info.text))

    no_kanji = kana.find('re_nokanji')

    applies_to_kanji = []
    if no_kanji is None:
        for item in kana.findall('re_restr'):
            applies_to_kanji.append(item.text)

        if not len(applies_to_kanji):
            applies_to_kanji = ['*']

    return {
        'text': text,
        'common': is_common,
        'tags': tags,
        'appliesToKanji': applies_to_kanji
    }
示例#2
0
    def _fill_subject(
        self,
        subject: TileSubject,
        subject_element: Element,
    ) -> None:
        subject_properties = {}

        subject.position = tuple(
            map(
                int,
                get_text_xml_element(subject_element, 'position').split(','),
            ), )
        subject.direction = float(
            get_text_xml_element(subject_element, 'direction'), )
        # TODO BS 2018-06-20: Maybe need apply this mode no ?
        subject.combat_mode = \
            get_text_xml_element(subject_element, 'combat_mode')

        properties_element = subject_element.find('properties')
        decode_properties_map = self._get_decode_properties_map()

        for item_element in properties_element.findall('item'):
            key_text = item_element.find('key').text
            value_text = item_element.find('value').text

            try:
                decoded_value = decode_properties_map[key_text](value_text)
            except KeyError:
                raise NotFoundError(
                    'You try to load property "{}" but it is unknown'.format(
                        key_text, ))

            subject_properties[key_text] = decoded_value

        subject.properties = subject_properties
示例#3
0
def __parse_entry(entry: Element) -> dict:
    entry_id = int(entry.find('ent_seq').text)

    kanji_list = []
    for kanji in entry.findall('k_ele'):
        kanji_list.append(__parse_kanji(kanji))

    kana_list = []
    for kana in entry.findall('r_ele'):
        kana_list.append(__parse_kana(kana))

    last_part_of_speech = []
    sense_list = []
    for sense in entry.findall('sense'):
        part_of_speech = sense.findall('pos')
        if len(part_of_speech):
            last_part_of_speech = []
            for item in part_of_speech:
                short_pos = convert_tag(item.text)
                last_part_of_speech.append(short_pos)

        sense_list.append((__parse_sense(sense, last_part_of_speech)))

    return {
        'id': entry_id,
        'kanji': kanji_list,
        'kana': kana_list,
        'sense': sense_list
    }
示例#4
0
def __parse_kanji(kanji: Element) -> dict:
    text = kanji.find('keb').text

    priorities = kanji.findall('ke_pri')
    is_common = __is_common(priorities)

    tags = []
    for info in kanji.findall('ke_inf'):
        tags.append(convert_tag(info.text))

    return {'text': text, 'common': is_common, 'tags': tags}
示例#5
0
def get_text_xml_element(
    element: Element,
    search_element_name: str,
    default_value: str = __DEFAULT__,
) -> str:
    found = element.find(search_element_name)
    if found is None:
        if default_value == __DEFAULT__:
            raise NotFoundError(
                'Asked element "{}" not exist in {}'.format(
                    search_element_name,
                    str(element),
                ),
            )
        return default_value

    return found.text