【问题标题】:Convert text written in some syntax to another specified syntax将以某种语法编写的文本转换为另一种指定的语法
【发布时间】:2022-01-03 04:00:30
【问题描述】:

我想将用 Markdown 扩展 Fountain 编写的剧本转换为 LaTeX(更具体地说是我自己的剧本 LaTeX 模板)。为此,我需要转换以下格式的文本

Some stage directions.

CHARACTER A:
Text the character is saying.

CHARACTER B:
Text the other character is saying.

Some other stage direction.

CHARACTER B:
Some more text the other character is saying.

\textit{Some stage directions.}

\dialog{Character A}{Text the character is saying.}
\dialog{Character B}{Text the other character is saying.}

\textit{Some other stage direction.}

\dialog{Character B}{Some more text the other character is saying.}

我想避免从头开始编写这样的程序。是否有允许执行此相当基本的重新格式化的工具或包(例如 Python)?有问题的可能是舞台方向在文本中分布不均匀,即在一个角色说完之后,可能有也可能没有舞台方向。

【问题讨论】:

    标签: python text nlp formatting text-processing


    【解决方案1】:

    假设块由双换行符分隔,这很容易使用正则表达式实现:

    输入:

    t='''Some stage directions.
    
    CHARACTER A:
    Text the character is saying.
    
    CHARACTER B:
    Text the other character is saying.
    
    Some other stage direction.
    
    CHARACTER B:
    Some more text the other character is saying.'''
    

    代码:

    import re
    out = '\n\n'.join(fr'\dialog{{{m.group(1)}}}{{{m.group(2)}}}'
                      if (m:=re.match('([^\n]+):\n(.*)', s))
                      else fr'\textit{{{s}}}'
                      for s in re.split('\n\n', t))
    
    print(out)
    

    输出:

    \textit{Some stage directions.}
    
    \dialog{CHARACTER A}{Text the character is saying.}
    
    \dialog{CHARACTER B}{Text the other character is saying.}
    
    \textit{Some other stage direction.}
    
    \dialog{CHARACTER B}{Some more text the other character is saying.}
    

    【讨论】:

    • 太好了,我对正则表达式或 Python 字符串操作了解不多,但是使用您的示例我可以根据需要对其进行调整。
    • 很高兴它帮助了你!
    猜你喜欢
    • 2021-12-27
    • 2018-02-15
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多