gpt4 book ai didi

python - 在翻译文本中自动同步 Markdown(ProGit 书籍,可用资源)

转载 作者:行者123 更新时间:2023-11-28 18:50:32 26 4
gpt4 key购买 nike

摘要:将翻译内容中列出的子字符串包装为反引号的有效方法是什么?

动机:我正在将翻译文本中的 Markdown 标记与原文同步。我确实将 Scott Chacon 的 ProGit 书很好地翻译成了捷克语。不幸的是,它使用与原始版本完全不同的工具链进行排版,并且原始标记已经丢失。到目前为止,我已成功将大部分内容转换回 Markdown 并将文档结构与原始内容同步。下一步是在 code 周围使用反引号修复在翻译中。

情况

说,我有以下原文段落。实际上,如果重要的话,这是一个没有换行符的长行:

    On Windows systems, Git looks for the `.gitconfig` file in the 
`$HOME` directory (`C:\Documents and Settings\$USER` for most
people). It also still looks for /etc/gitconfig, although it’s
relative to the MSys root, which is wherever you decide to
install Git on your Windows system when you run the installer.

我也有翻译的段落:
    Ve Windows používá Git soubor .gitconfig, který je umístěný v 
domovském adresáři (u většiny uživatelů C:\Documents and
Settings\$USER). Dále se pokusí vyhledat ještě soubor
/etc/gitconfig, který je relativní vůči kořenovému adresáři.
Ten je umístěn tam, kam jste se rozhodli nainstalovat Git po
spuštění instalačního programu.

使用正则表达式,我确实从原始列表中提取了以下列表(这里是 repr()——因此是双反斜杠):
    ['.gitconfig', '$HOME', 'C:\\Documents and Settings\\$USER']

将翻译内容中列出的子字符串包装为反引号的有效方法是什么?问题还在于某些段落可能会多次重复相同的子字符串。我也不能告诉你会发生什么其他并发症。 ( "My brain hurts, too!" )

附注:对于那些对这个问题更感兴趣的人,一切都可以在 https://github.com/pepr/progitCZ 上找到。 (刚刚提交 04d1354656276bf1e6ba7305d06c12faca267a19;警告,评论是捷克语)。问题与 util/cz.py有关脚本。这是第四遍——在 pass4.py 中实现.目前,我将列表转换为集合,然后调用 str.replace()对于每个子串。
info_aux_cs\pass4backticks.txt文件显示了自动化过程的比较。 info_aux_cs\pass4.txt显示“固定”结果, txtCorrected\RucneUpravovanyVysledekPass2.txt显示最后一个手动修改的阶段。

另一个问题是... 文档的结构已经同步。另一方面,还没有检查段落的内容(翻译)是否有较新的原文。

更新 - 观察到新问题

自动替换可能不明确。我确实观察到了这样的案例 ['git clone', 'clone', ...] .如 set首先创建, clone实际上可以更早地包装。这条路
some text git `clone` other text 

出现在
some text `git clone` other text 

应该是正确的替换。

我知道这种方法非常具有启发性,实际上不需要非常精确地完成。一旦自动替换的文本将成为手动编辑的来源。这样,解决方案的一部分可以可视化应该由人眼检查并由人手修复的可疑差异:)

您对如何找到解决该问题的最可靠方法有任何想法吗?以下是我想到的一些启发式方法——即何时可视化潜在问题:
  • 原文中的所有子串都应该在翻译中找到。否则,翻译在某种程度上是特定的或不是最新的,或者只是瘫痪。翻译可能会更改她的子字符串,但应该认识到这一点,并且稍后应该明确禁止检查。
  • 目标语言中可能不会保留子字符串的顺序。无论如何,相同顺序的相同数量的子串是替换成功的好兆头。
  • 应该先替换最长的子串吗?
  • ...但较短的 substings 将在下一步替换?
  • 是否可以从子字符串构造正则表达式模式,并使用正则表达式的贪婪性来一次替换反引号所有模式?

  • 任何好主意都非常受欢迎;)

    感谢您的时间和经验,

    彼得

    最佳答案

    到目前为止,我发现使用正则表达式的解决方案是最有希望的。如果您找到更好的解决方案,我将很乐意接受您的解决方案:)

    首先,这是查找反引号子字符串的正则表达式:

    rexBackticked = re.compile(r'`(\S.*?\S?)`')

    拥有原版 enpara和翻译 cspara段落,我可以像这样提取反引号的子字符串列表:
    enlst = rexBackticked.findall(enpara)
    cslst = rexBackticked.findall(cspara)

    然后我测试一下是否应该修改捷克语段:
    if set(enlst) != set(cslst) or len(enlst) != len(cslst):

    如果是,那么我创建一个子串的差异列表,这些子串不是但应该在 cspara 中反引号。 (可能会写得更好):
        dlst = enlst[:]   # copy
    for s in cslst:
    if s in dlst:
    dlst.remove(s)

    现在我需要构建一个正则表达式对象来识别 dlst子串。我已经定义了以下函数:
    def buildRex(self, lst):
    '''Build a regular expression mathing substrings from the lst.'''

    # Build a list of escaped unique substrings from the input list.
    # The order is not important now as it must be corrected later.
    lst2 = [re.escape(s) for s in set(lst)]

    # Join the escaped substrings to form the regular expression
    # pattern, build the regular expression, and return it. There could
    # be longer paterns that contain shorter patterns. The longer patterns
    # should be matched first. This way, the lst2 must be reverse sorted
    # by the length of the patterns.
    pat = '|'.join(sorted(lst2, key=len, reverse=True))
    rex = re.compile(pat)
    return rex

    现在我可以用它来替换 cspara 中所有不重叠的子串:
        rex = self.buildRex(dlst)
    cspara, n = rex.subn(r'`\g<0>`', cspara)

    哪里 n是对 future 检查可能很重要的替换次数。

    欢迎任何意见!

    关于python - 在翻译文本中自动同步 Markdown(ProGit 书籍,可用资源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13584317/

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