2015-04-06 85 views
0

在你说还有其他线程的时候,请相信我,我已经阅读了它们,而且我发现它们没有帮助,因为我鼓励我的代码不同,而且我一直在做我自己从一开始。不寻找免费赠品,但一些实际的帮助。PYTHON 3:猪拉丁语,但听我说

我的代码如下所示,我可以打开要翻译成Pig Latin的文件,将所有标点符号和无用数字去掉,然后返回文件中的单词列表。规定是我只需要翻译文字。如果单词以元音开头,则在末尾添加“ay”。如果单词以辅音开头,请从单词中移除第一个字母,将其添加到单词的末尾,并在末尾添加“ay”。

即:字=“奥德韦: 和橙色= “orangeay”

验证码:

import re 
import nltk 

def usr_name_file(): 
    """ 
     Function: Gets name of file to translate 
     Parameter: n/a 
     Returns: name of file to open 
    """ 
    nameFile = input('\nEnter the filename to translate into Piglatin >>>') 

    return nameFile 

def validate_name(nameFile): 
    """ 
     Function: Validates the existance of the usr file 
     Parameter: Name of file input by usr 
     Returns: Error if file not found, none if file found 
    """ 
    try: 
     inputFile= open(nameFile, 'r') 
     inputFile.close() 
    except IOError: 
     print('Error: File not found in this directory.\nTry again.\n') 

    return 

def open_named_file(nameFile): 
    """ 
     Function: 
     Parameter: 
     Returns: 
    """ 
    with open(nameFile, 'r') as readFile: 
     data = readFile.read() 
     print(data)   # import re makes this easier 
     words_list = re.findall(r"[\w']+", data) # extract punctuation 
     sans_numbers = [x for x in words_list if not (x.isdigit() or x[0] == 
                '-' and x[1:].isdigit())] 

    return sans_numbers 


def translate(list):  # Help Here Please! 
    """ 
     Function: Takes in word and translates into piglatin 
     Parameter: Word 
     Returns: Translated word 
    """ 

    return 

def main(): 

    x = usr_name_file() 
    validate_name(x) 
    WordsList = open_named_file(x) 
    print(WordsList) 

if __name__ == '__main__': 
    main() 

这让我对我现在在哪里,在文件中的单词列表,没。PUNC 未完成的翻译()函数是在那里我有一些困难,下面是我想要它做的伪代码:

def translate(list): 
    for vowel_word, consonant_word in list: 
     if the word starts with a vowel, add "ay" to the end 
     if the word starts with a consonant, replace the first letter to the 
     end and add "ay" 
    return translated_list 

的想法是THA对于我传递给翻译功能的单词列表,我希望它按照相同的顺序进入并制作新的单词列表,但翻译完毕。我现在知道如何做的唯一方法是创建一个元音单词列表和辅音单词列表,但如果我这样做,我认为使用.write()编写带有翻译句子的新文件会很困难。我知道如何编写新的文件代码,所以我需要帮助的是这个功能。完全披露这是为硬件,但正如你可以看到我不是要求免费赠品,只是一点点帮助这个功能,这是非常赞赏。

+0

也许地图某些波长的职能,减少或类似的东西? – 11thZone 2015-04-06 02:37:00

+0

我会建议调查'str.startswith()'方法。 – Kevin 2015-04-06 02:41:22

+0

当然,我也想到了这一点,但我真正的问题是不知道如何进入我的单词列表,并基于两个if语句添加一个新列表 – 11thZone 2015-04-06 02:45:55

回答

0

提示:

  • 开始与空转换列表。
  • 迭代单词列表,对于每个单词:
    • 找到第一个元音的索引。
    • 如果它为零,则添加'way'
    • 如果它不为零,则使用字符串切片,例如, word [i:] + word [:i] +'ay'
    • 将新单词添加到翻译列表中。
+0

HAHA!那就是我刚刚做的,它也起作用。现在我正在编写将所有内容放回到文本文件中的函数。 – 11thZone 2015-04-06 03:27:01