求助,stardict转txt再转mdx的过程中,单词变体怎么处理

stardict转换出来的txt,不同变体都排一起用|分割,比如warp|warped|warpest|warpeth|warping|warps,转换成mdx的时候就会把这一条转为同一个单词,搜中间的词就搜不出来,因为这个词典有80多万条,所以手动编也不现实,能不能直接在txt里面替换一下 :sob:

python 脚本 和正则都能处理,问题是,你掌握的怎么样,到哪一个程度了。

这决定了回答这个问题要说多详细。

或者换个方式,可以上传下文本样本,让别人来处理吗?

昨天为了用pyglossary才安装python的…txt我是拿emeditor修改的(还是靠对比mdx文件拆出来的txt格式修改的…)文本传不上来,老出错,大概是太大了,只能把词典来源放上来了:joy:https://www.reader-dict.com/en。stardict的文件拆出来的txt,加了换行和</>就是现在的文件

文本文件可以只取前 10 行。

截了十几传行上来了,所有的基本都是这个格式。例.txt (2.4 KB)

Text

1 个赞

def process_file(input_file, output_file):
    output = ""
    with open(input_file, 'r') as f:
        lines = f.readlines()
    
    i = 0
    while i < len(lines):
        # Read first line and split by '|'
        if i >= len(lines):
            break
        foo = lines[i].strip().split('|')
        i += 1
        
        # Read rest lines as bar until '</>'
        bar = []
        while i < len(lines) and lines[i].strip() != '</>':
            bar.append(lines[i])
            i += 1
        
        # Skip the '</>' line
        if i < len(lines) and lines[i].strip() == '</>':
            i += 1
        
        # Process each element in foo
        for element in foo:
            element = element.strip()
            if element:  # Skip empty elements
                output += element + '\n'
                output += '\n'.join(bar).strip() + '\n'
                output += '</>\n'
    
    # Save output to file
    with open(output_file, 'w') as f:
        f.write(output)

# Example usage:
process_file('input.txt', 'output.txt')


反复报错UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 2315: illegal multibyte sequence
怎么办 :sob:

with open(input_file, 'r') as f
修改成:
with open(input_file, 'r', encoding='utf-8') as f

还是不行,会出现之前的unicode encode error,改成encoding=‘gbk’,errors= 'ignore '的话倒是可以输出,但是会出现很多乱码,encoding utf8 error ignore也是不能转换的,另外就是只能把文件切成20份才能转,整个塞进去电脑转了两个小时都不行:joy:

txt 文本文件的编码确保是 utf-8 无 bom,如果不确定用 emeditor 检查一下。

在emeditor原地展开应该也行

查找:

^([^<>|]+?) *\| *([^<>|]+?) *(\|.+)?$

替换为:

\2\n@@@LINK=\1\n</>\n\1\3

(重复替换直到结束)

效果:

1 个赞

\3 是指第三个括号的内容,改成 \9 会丢失相应的数据
不断重复“替换全部”,直到所有词头都被展开
(你可以先找几个词条做测试,看它是如何被层层展开的)

后面发现丢失内容,就用3反复替换了,所以就把刚才的回复删掉了 :joy:但是现在做出来的文件它都有两套翻译