5

で困っていpexpectます。tralics次のように、ラテックス方程式を読み取り、MathML 表現を出力する出力を取得しようとしています。

1 ~/ % tralics --interactivemath
This is tralics 2.14.5, a LaTeX to XML translator, running on tlocal
Copyright INRIA/MIAOU/APICS/MARELLE 2002-2012, Jos\'e Grimm
Licensed under the CeCILL Free Software Licensing Agreement
Starting translation of file texput.tex.
No configuration file.
> $x+y=z$
<formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi>   <mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula>
> 

だから私は pexpect を使って数式を取得しようとします:

import pexpect
c = pexpect.spawn('tralics --interactivemath')
c.expect('>')
c.sendline('$x+y=z$')
s = c.read_nonblocking(size=2000)
print s

出力には式がありますが、最初に元の入力があり、最後にいくつかの制御文字があります。

"x+y=z$\r\n<formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi><mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula>\r\n\r> \x1b[K"

出力文字列をきれいにすることはできますが、何か基本的なものが欠けているに違いありません。MathML を取得するよりクリーンな方法はありますか?

4

1 に答える 1

5

私が理解していることから、あなたは pexpect からこれを取得しようとしています:

<formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi>   <mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula>

期待される結果を得るために、マッチングに ">" の代わりに正規表現を使用できます。これは最も簡単な例です:

c.expect("<formula.*formula>");

その後、pexpect の match 属性を呼び出すことで、一致した文字列にアクセスできます。

print c.match

私が投稿したものは貪欲なものであり、式が大きい場合は実行時間を妨げる可能性があるため、別の正規表現を試すこともできます。

于 2012-10-10T18:50:39.357 に答える