gpt4 book ai didi

python - 解析文本以替换引号和嵌套引号

转载 作者:行者123 更新时间:2023-11-30 23:36:44 25 4
gpt4 key购买 nike

使用 python,我想“教育”纯文本输入的引号并将它们转换为上下文语法。这是一个(递归)示例:

原文:

Using python, I would like "educate" quotes of 
a plain text input and turn them into the Context syntax.
Here is a (recursive) example:

输出:

Using python, I would like \quotation{educate} quotes of 
a plain text input and turn them into the Context syntax.
Here is a (recursive) example:

我希望它也能处理嵌套引用:

原文:

Original text: "Using python, I would like 'educate' quotes of 
a plain text input and turn them into the Context syntax.
Here is a (recursive) example:"

输出:

Original text: \quotation {Using python, I would like \quotation{educate} quotes of 
a plain text input and turn them into the Context syntax.
Here is a (recursive) example:}

当然,我应该处理边缘情况,例如:

She said "It looks like we are back in the '90s"

上下文引用的规范在这里:

http://wiki.contextgarden.net/Nested_quotations#Nested_quotations_in_MkIV

对于这种情况最敏感的方法是什么?非常感谢!

最佳答案

这个可以使用嵌套引号,但它不能处理你的边缘情况

def quote(string):
text = ''
stack = []
for token in iter_tokes(string):
if is_quote(token):
if stack and stack[-1] == token: # closing
text += '}'
stack.pop()
else: # opening
text += '\\quotation{'
stack.append(token)
else:
text += token
return text

def iter_tokes(string):
i = find_quote(string)
if i is None:
yield string
else:
if i > 0:
yield string[:i]
yield string[i]
for q in iter_tokes(string[i+1:]):
yield q

def find_quote(string):
for i, char in enumerate(string):
if is_quote(char):
return i
return None

def is_quote(char):
return char in '\'\"'

def main():
quoted = None
with open('input.txt') as fh:
quoted = quote(fh.read())
print quoted

main()

关于python - 解析文本以替换引号和嵌套引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16262004/

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