在Python中有没有处理TMX(翻译记忆交换)文件的模块,如果没有,还有其他方法吗?
目前,我手头有一个2GB巨大的法英字幕文件。是否可能处理这样的文件,还是必须将其拆分?
在Python中有没有处理TMX(翻译记忆交换)文件的模块,如果没有,还有其他方法吗?
目前,我手头有一个2GB巨大的法英字幕文件。是否可能处理这样的文件,还是必须将其拆分?
正如@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]- msoutopicofrom 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]
pip install lxml
命令安装 lxml
库。 - Anwarvicnode.source
代替node.getsource()
和node.target
代替node.gettarget()
。 - Anwarvicfrom 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)