要从文档中获取所有文本,
tree.descendants
这里会更加友好。这将按顺序输出所有文本。
def getText(section):
for token in section.descendants:
if isinstance(token, str):
corpus.write(str(x))
</code>
为了捕捉边缘情况,我写了一个稍微更加丰富的版本。这包括检查您在那里列出的所有条件。
from TexSoup import RArg
def getText(section):
for x in section.descendants:
if isinstance(x, str):
if x.startswith(‘$’) and x.endswith(‘$’):
continue
corpus.write(str(x))
elif isinstance(x, RArg):
corpus.write(str(x))
elif hasattr(x, ‘source’) and hasattr(x.source, ‘name’) and x.source.name in (‘acknowledgements’, ‘appendix’):
return
</code>