Пример #1
0
def get_model(path=""):
    model = Model()
    # Adding chapters
    for chapterFile in os.listdir(path + DATA_PATH + '/chapter/'):
        chapter = get_state(path + DATA_PATH + '/chapter/' + chapterFile, StateName.chapter)
        model.add_edge('INITIAL', chapter)
        model.add_state(StateName.chapter, chapter)

        # Adding subchapters
        if not os.path.exists(path + DATA_PATH + '/subchapter/' + chapterFile[:-4]):
            continue
        subchapters = []
        for subchapterFile in os.listdir(path + DATA_PATH + '/subchapter/' + chapterFile[:-4]):
            subchapter = get_state(path + DATA_PATH + '/subchapter/' + chapterFile[:-4] + '/' + subchapterFile, StateName.sub_chapter)
            subchapters.append(subchapter)

            # Adding sections
            if not os.path.exists(path + DATA_PATH + '/section/' + subchapterFile[:-4]):
                continue
            sections = []
            for sectionFile in os.listdir(path + DATA_PATH + '/section/' + subchapterFile[:-4]):
                section = get_state(path + DATA_PATH + '/section/' + subchapterFile[:-4] + '/' + sectionFile, StateName.section)
                sections.append(section)

                # Adding subsections
                if not os.path.exists(path + DATA_PATH + '/subsection/' + sectionFile[:-4]):
                    continue
                subsections = []
                for subsectionFile in os.listdir(path + DATA_PATH + '/subsection/' + sectionFile[:-4]):
                    subsection = get_state(path + DATA_PATH + '/subsection/' + sectionFile[:-4] + '/' + subsectionFile, StateName.sub_section)
                    subsections.append(subsection)

                scale_probabilities(subsections)
                for subsection in subsections:
                    model.add_edge(section.code, subsection)
                    model.add_state(StateName.sub_section, subsection)
            scale_probabilities(sections)
            for section in sections:
                model.add_edge(subchapter.code, section)
                model.add_state(StateName.section, section)

        scale_probabilities(subchapters)
        for subchapter in subchapters:
            model.add_edge(chapter.code, subchapter)
            model.add_state(StateName.sub_chapter, subchapter)

    scale_probabilities(model.states[StateName.chapter])
    return model