属性错误:str没有属性rem

2024-06-25 23:04:19 发布

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

很抱歉,我上次使用这个网站是我第一次,我没有缩进我的代码和所有。但这次我试着这么做了。我希望代码是可以理解的。我正在开发一个由Joshua Tauberer编写的python程序,可从https://p2tk.svn.sourceforge.net/svnrooot/p2tk/python/syllabify/syllabifier.py获得。这个程序是免费使用的,而且我在我的项目中引用了源代码。我用这个程序把我输入的音素列表拼成音节。此程序接受一个输入文件,其内容如下:

pau s aa m ih k l eh k t aa n ih t pau g eh l v ae n ih k pau aa p l ay k pau

这些是由语音文件生成的音素。pau代表短暂的停顿。然后,该程序对输入进行提纲,生成如下输出:

s aa ' m ih ' k l eh k ' t aa ' n ih t ' g eh l ' v ae ' n ih ' k aa ' p l ay k

此输出是音节版本。但是当我从输入文件中手动删除pau时,程序生成了这个输出。因为程序只识别音素,而pau不是一个。所以我需要在程序中做一个更改,从列表中删除所有现有的pau。我在这里复制了程序的主要部分。我加了几行 text.remove(“pau”)并尝试添加另一个phoneme.remove(“pau”)。但在这两种情况下,我都会得到一个错误消息:str对象没有属性remove。我不明白我错在哪里。请帮忙。非常感谢你。

def syllabify(language, text) :

 text.remove("pau") 
 if type(text) == str :
    text = text.split()

 syllables = [] # This is the returned data structure.

 internuclei = [] # This maintains a list of phonemes between nuclei.

 for phoneme in text :
    #phoneme.remove("pau")
    phoneme = phoneme.strip()
    if phoneme == "" :
        continue
    stress = None

Tags: 文件代码text程序列表removeaa音节
2条回答
phoneme = phoneme.replace("pau", "")
def syllabify(language, text) :
    #These lines will take any list of phonemes or string of phonemes
    #and strip all of the whitespace and 'pau's
    #and won't return any empty phonemes
    if type(text) is not str:
        text = ' '.join(text)
    text.replace("pau", "") 
    text = text.split()

    syllables = [] # This is the returned data structure.
    internuclei = [] # This maintains a list of phonemes between nuclei.

    for phoneme in text:
        # so you don't have to check for empty phonemes,
        # delete 'pau's, or strip whitespace here
        stress = None
        # whatever else you do

相关问题 更多 >