gpt4 book ai didi

python - 标记复杂输入

转载 作者:太空宇宙 更新时间:2023-11-03 11:56:40 24 4
gpt4 key购买 nike

我正在尝试在 Python 中标记以下输入:

text = 'This @example@ is "neither":/defn/neither complete[1] *nor* trite, *though _simple_*.'

我想生成如下内容,同时避免使用正则表达式:

tokens = [
('text', 'This '),
('enter', 'code'),
('text', "example")
('exit', None),
('text', ' is '),
('enter', 'a'),
('text', "neither"),
('href', "/defn/neither"),
('exit', None),
('text', ' complete'),
('enter', 'footnote'),
('id', 1),
('exit', None),
('text', ' '),
('enter', 'strong'),
('text', 'nor'),
('exit', None),
('text', ' trite, '),
('enter', 'strong'),
('text', 'though '),
('enter', 'em'),
('text', 'simple'),
('exit', None),
('exit', None),
('text', '.')
]

假设上面的内容是由生成器生成的。我的current implementation有效,尽管代码有些可怕并且不易扩展以支持链接。

如有任何帮助,我们将不胜感激。

已更新以将所需的语法从复杂的嵌套列表结构更改为简单的元组流。我们人类的缩进。链接文本内的格式是可以的。这是 a simple parser生成我正在寻找的词法分析结果,但仍然不处理链接或脚注。

最佳答案

嗯,here's a more complete parser具有足够的可扩展性来做我将来可能需要的任何事情。只用了三个小时。它不是非常快,但通常我正在编写的解析器类的输出无论如何都会被大量缓存。即使有了这个 tokenizer 和解析器,我的完整引擎的时钟频率仍然低于默认 python-textile 渲染器 SLoC 的 75%,同时保持更快的速度。全部没有正则表达式。

脚注解析仍然存在,但与链接解析相比,这是次要的。输出(截至这篇文章)是:

tokens = [
('text', 'This '),
('enter', 'code'),
('text', 'example'),
('exit', None),
('text', ' is '),
('enter', 'a'),
('text', 'neither'),
('attr', ('href', '/defn/neither')),
('exit', None),
('text', ' complete[1] '),
('enter', 'strong'),
('text', 'nor'),
('exit', None),
('text', ' trite, '),
('enter', 'strong'),
('text', 'though '),
('enter', 'em'),
('text', 'simple'),
('exit', None),
('exit', None),
('text', '.')
]

关于python - 标记复杂输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7142934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com