如何正确使用gensim的deaccent方法?

2024-06-03 14:06:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是读取文件中的行,并用普通字符(a,e,c,…)替换所有特殊字符,如法语字符(a,e,ç,…)

我使用的是python3,在gensim的文档中,这个例子使用的是一个简单的句子,比如:deaccent(“戥戥ç”),但不使用我从文件中读取的行 在这个时候,我的代码只得到了“戥戥”而不是“aec”

from gensim.utils import deaccent

def getTextFromFile(filename):
    with open(filename) as file:
        text = [line.rstrip() for line in file.readlines()]
    file.close()
    for line in text:
        print(deaccent(line))
    return text

我的文件包含:ç

我想得到:aec


Tags: 文件textin文档目标forlinefilename
1条回答
网友
1楼 · 发布于 2024-06-03 14:06:04

据我所知,它运行良好:

Python 3.7.0 (default, Aug 22 2018, 20:50:05) 
Type "copyright", "credits" or "license" for more information.
IPython 4.1.2   An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
In [1]: from gensim.utils import deaccent
In [2]: deaccent('àéç')
Out[2]: 'aec'
In [3]: astr = 'àéç'
In [4]: dstr = deaccent(astr)
In [5]: print(dstr)
aec

如果希望getTextFromFile()方法返回取消重音的文本,则不要返回原始的text,而是返回deaccent()调用的结果

相关问题 更多 >