2024-07-14 10:18:14 +00:00
|
|
|
from lxml import etree
|
2024-07-14 12:25:59 +00:00
|
|
|
from typing import List
|
2024-07-14 10:18:14 +00:00
|
|
|
|
|
|
|
|
2024-07-14 12:25:59 +00:00
|
|
|
global_ast_path = r"E:\storyline.xast"
|
|
|
|
|
|
|
|
|
|
|
|
def items_filter(items, proc):
|
|
|
|
values = []
|
|
|
|
for it in items:
|
|
|
|
if proc(it):
|
|
|
|
values.append(it)
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
return values
|
|
|
|
|
|
|
|
|
|
|
|
class FragmentSlice():
|
|
|
|
def __init__(self, signature: str):
|
|
|
|
self.signature = signature
|
|
|
|
self.text_sections = []
|
|
|
|
pass
|
|
|
|
|
|
|
|
def append_text_section(self, section: str):
|
|
|
|
self.text_sections.append(section)
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class StorySlice():
|
|
|
|
def __init__(self, signature: str):
|
|
|
|
self.signature = signature
|
|
|
|
self.text_sections = []
|
|
|
|
self.fragment_slices = []
|
|
|
|
pass
|
|
|
|
|
|
|
|
def append_text_section(self, section: str):
|
|
|
|
self.text_sections.append(section)
|
|
|
|
pass
|
|
|
|
|
|
|
|
def append_fragment_slice(self, slice: FragmentSlice):
|
|
|
|
self.fragment_slices.append(slice)
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AstParse():
|
2024-07-14 10:18:14 +00:00
|
|
|
def __init__(self, ast_path: str):
|
|
|
|
self.ast_path = ast_path
|
2024-07-14 12:25:59 +00:00
|
|
|
ast_file = open(ast_path, "rb")
|
2024-07-14 10:18:14 +00:00
|
|
|
ast_text = ast_file.read()
|
|
|
|
self.ast_inst = etree.XML(ast_text)
|
|
|
|
pass
|
|
|
|
|
2024-07-14 12:25:59 +00:00
|
|
|
def generate_time(self) -> str:
|
|
|
|
attr_time = self.ast_inst.xpath("/ast[1]/@time")[0]
|
|
|
|
return str(attr_time)
|
|
|
|
|
|
|
|
def peak_storylines(self) -> List[StorySlice]:
|
|
|
|
story_list = self.ast_inst.xpath("/ast/story") # 获取所有故事节点
|
|
|
|
|
|
|
|
story_items = []
|
|
|
|
for story in story_list:
|
|
|
|
story_name = story.xpath("@name")[0]
|
|
|
|
story_slice = StorySlice("story:"+story_name)
|
|
|
|
story_items.append(story_slice)
|
|
|
|
|
|
|
|
story_text_lines = story.xpath("text-section/@text") # 获取所有描述
|
|
|
|
for line in story_text_lines:
|
|
|
|
story_slice.append_text_section(line)
|
|
|
|
pass
|
|
|
|
|
|
|
|
fragment_items = story.xpath("fragment") # 获取所有情节定义
|
|
|
|
for fragm in fragment_items:
|
|
|
|
fragment_name = fragm.xpath("@name")
|
|
|
|
fragm_slice = FragmentSlice("fragment:"+story_name+"#"+fragment_name)
|
|
|
|
fragm_text_lines = fragm.xpath("text-section/@text")
|
|
|
|
for line in fragm_text_lines:
|
|
|
|
fragm_slice.append_text_section(line)
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
fragm_refers = self.ast_inst.xpath("//refer")
|
|
|
|
for refer in fragm_refers:
|
|
|
|
story_name = refer.xpath("@story")
|
|
|
|
fragm_name = refer.xpath("@fragment")
|
|
|
|
story_refers = items_filter(story_items, lambda x:x.signature == f"story:{story_name}")
|
|
|
|
if(len(story_refers) > 0):
|
|
|
|
story_refer = story_refers[0]
|
|
|
|
|
|
|
|
|
|
|
|
return story_items
|
2024-07-14 10:18:14 +00:00
|
|
|
|