gpt4 book ai didi

python - 使用Python将引号转换为Latex格式

转载 作者:行者123 更新时间:2023-12-01 03:22:02 25 4
gpt4 key购买 nike

tl;博士版本

我有一个段落可能包含引号(例如“blah blah”、“这也是”等)。现在我必须在 python 3.0 的帮助下用 latex 风格的引用(例如“blah blah”、“这也是”等)替换它。

背景

我有很多纯文本文件(超过 100 个)。现在,我必须在对这些文件进行少量文本处理后,使用从这些文件中获取的内容制作一个单独的 Latex 文档。为此,我使用 Python 3.0。现在我可以使其他所有内容(如转义字符、部分等)正常工作,但我无法正确获取引号。

我可以使用正则表达式找到模式(如所述 here ),但如何用给定的模式替换它?我不知道在这种情况下如何使用“re.sub()”函数。因为我的字符串中可能有多个引号实例。有this与此相关的问题,但我如何用 python 实现这个?

最佳答案

设计考虑因素

  1. 我只考虑了常规 "double-quotes"'single-quotes' 。可能还有其他引号(参见 this question )
  2. LaTeX 端引号也是单引号 - 我们不想捕获 LaTeX 双端引号(例如“LaTeX 双引号”)并将其误认为是单引号(几乎没有)<
  3. 单词缩写和所有权 's包含单引号(例如 don'tJohn's )。这些的特点是字母字符围绕在引号的两侧
  4. 常规名词(复数所有权)在单词后带有单引号(例如 the actresses' roles )

解决方案

import re

def texify_single_quote(in_string):
in_string = ' ' + in_string #Hack (see explanations)
return re.sub(r"(?<=\s)'(?!')(.*?)'", r"`\1'", in_string)[1:]

def texify_double_quote(in_string):
return re.sub(r'"(.*?)"', r"``\1''", in_string)

测试

with open("test.txt", 'r') as fd_in, open("output.txt", 'w') as fd_out:
for line in fd_in.readlines():

#Test for commutativity
assert texify_single_quote(texify_double_quote(in_string)) == texify_double_quote(texify_single_quote(in_string))

line = texify_single_quote(line)
line = texify_double_quote(line)
fd_out.write(line)

输入文件(test.txt):

# 'single', 'single', "double"
# 'single', "double", 'single'
# "double", 'single', 'single'
# "double", "double", 'single'
# "double", 'single', "double"
# I'm a 'single' person
# I'm a "double" person?
# Ownership for plural words; the peoples' 'rights'
# John's dog barked 'Woof!', and Fred's parents' 'loving' cat ran away.
# "A double-quoted phrase, with a 'single' quote inside"
# 'A single-quoted phrase with a "double quote" inside, with contracted words such as "don't"'
# 'A single-quoted phrase with a regular noun such as actresses' roles'

输出(output.txt):

# `single', `single', ``double''
# `single', ``double'', `single'
# ``double'', `single', `single'
# ``double'', ``double'', `single'
# ``double'', `single', ``double''
# I'm a `single' person
# I'm a ``double'' person?
# Ownership for plural words; the peoples' `rights'
# John's dog barked `Woof!', and Fred's parents' `loving' cat ran away.
# ``A double-quoted phrase, with a `single' quote inside''
# `A single-quoted phrase with a ``double quote'' inside, with contracted words such as ``don't'''
# `A single-quoted phrase with a regular noun such as actresses' roles'

(注意注释已预先添加以停止对帖子输出进行格式化!)

说明

我们将分解这个正则表达式模式,(?<=\s)'(?!')(.*?)' :

  • 摘要:(?<=\s)'(?!')处理开头的单引号,而 (.*?)处理引号中的内容。
  • (?<=\s)'positive look-behind并且仅匹配前面有空格 ( \s ) 的单引号。这对于防止匹配缩略词(例如 can't)非常重要。 (考虑因素 3、4)。
  • '(?!')negative look-ahead并且仅匹配后跟另一个单引号的单引号(考虑2)。
  • this answer 中所述, 模式(.*?)捕获引号之间的内容,而 \1包含捕获。
  • “黑客” in_string = ' ' + in_string是否存在,因为正后视捕获从行开头开始的单引号,因此为所有行添加一个空格(然后在返回时使用切片将其删除, return re.sub(...)[1:] )解决了这个问题问题!

关于python - 使用Python将引号转换为Latex格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41820839/

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