Python中的TMX(Translation Memory eXchange)文件

10

在Python中有没有处理TMX(翻译记忆交换)文件的模块,如果没有,还有其他方法吗?

目前,我手头有一个2GB巨大的法英字幕文件。是否可能处理这样的文件,还是必须将其拆分?


当然,TMX只是XML。2GB将提供约1GB的数据,因此,与其使用法语-英语映射,将所有内容导入数据库会更有意义。 - Joop Eggen
以下是完整解决方案:https://softans.com/question/tmxtranslation-memory-exchange-files-in-python/#comment-514 - GHULAM NABI
3个回答

7

正如@hurrial所说,您可以使用翻译工具包

安装

这个工具包只能通过pip安装。要安装它,请运行:

pip install translate-toolkit

使用方法

假设您有以下简单的sample.tmx文件:

<tmx version="1.4">
  <header
    creationtool="XYZTool" creationtoolversion="1.01-023"
    datatype="PlainText" segtype="sentence"
    adminlang="en-us" srclang="en"
    o-tmf="ABCTransMem"/>
  <body>
    <tu>
      <tuv xml:lang="en">
        <seg>Hello world!</seg>
      </tuv>
      <tuv xml:lang="ar">
        <seg>اهلا بالعالم!</seg>
      </tuv>
    </tu>
  </body>
</tmx>

您可以这样解析这个简单的文件:
>>> from translate.storage.tmx import tmxfile
>>>
>>> with open("sample.tmx", 'rb') as fin:
...     tmx_file = tmxfile(fin, 'en', 'ar')
>>>
>>> for node in tmx_file.unit_iter():
...     print(node.source, node.target)
Hello world! اهلا بالعالم!

如需更多信息,请查阅此处的官方文档。


无法在python3中工作: [code]
from translate.storage.tmx import tmxfile\ Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/souto/.local/share/virtualenvs/folder-WrAyGpIU/lib/python3.5/site-packages/translate/storage/tmx.py", line 22, in <module> from lxml import etree ImportError: No module named 'lxml'[/code]
- msoutopico
1
使用 pip install lxml 命令安装 lxml 库。 - Anwarvic
出现错误:'tmxunit' 对象没有 'getsource' 属性。 - Aditya Landge
2
@AdityaLandge,显然他们改变了API。无论如何,我已经更新了我的答案,使用node.source代替node.getsource()node.target代替node.gettarget() - Anwarvic


0
这是一个可以轻松将TMX转换为pandas数据框的脚本:
from collections import namedtuple
import pandas as pd
from tqdm import tqdm
from bs4 import BeautifulSoup

def tmx2df(tmxfile):
    # Pick your poison for parsing XML.
    with open(tmxfile) as fin:
        content = fin.read()
        bsoup = BeautifulSoup(content, 'lxml')    # Actual TMX extraction.
    lol = [] # Keep a list of the rows to populate.
    for tu in tqdm(bsoup.find_all('tu')):
        # Parse metadata from tu
        metadata = tu.attrs
        # Parse prop
        properties = {prop.attrs['type']:prop.text for prop in tu.find_all('prop')}
        # Parse seg
        segments = {}
        # The order of the langauges might not be consistent, 
        # so keep them in some dict and unstructured first.
        for tuv in tu.find_all('tuv'):
            segment = ' '.join([seg.text for seg in tuv.find_all('seg')])
            segments[tuv.attrs['xml:lang']] = segment
        lol.append({'metadata':metadata, 'properties':properties, 'segments':segments})    # Put the list of rows into a dataframe.
    df = pd.DataFrame(lol)    # See https://dev59.com/floT5IYBdhLWcg3wvBek#38231651
    return pd.concat([df.drop(['segments'], axis=1), df['segments'].apply(pd.Series)], axis=1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接