【问题标题】:python pig latin converterpython 猪拉丁语转换器
【发布时间】:2013-03-14 02:16:01
【问题描述】:

请帮帮我!

我正在将包含多行的文本文件转换为猪拉丁语。

示例:Pig 拉丁语翻译:这是一个示例。应该是:Histay siay naay xampleeay。

我需要将任何标点符号留在应有的位置(大多数情况下是句末) 我还需要在原文中以大写字母开头的任何单词在猪拉丁语版本中以大写字母开头,其余字母小写。

这是我的代码:

def main():
    fileName= input('Please enter the file name: ')

    validate_file(fileName)
    newWords= convert_file(fileName)
    print(newWords)


def validate_file(fileName):
    try:
        inputFile= open(fileName, 'r')
        inputFile.close()
    except IOError:
        print('File not found.')


def convert_file(fileName):
    inputFile= open(fileName, 'r')
    line_string= [line.split() for line in inputFile]

    for line in line_string:
        for word in line:
            endString= str(word[1:])
            them=endString, str(word[0:1]), 'ay'
            newWords="".join(them)
            return newWords

我的文本文件是:

This is an example. 

My name is Kara!

然后程序返回:

Please enter the file name: piglatin tester.py
hisTay
siay
naay
xample.eay
yMay
amenay
siay
ara!Kay
None

我如何让它们在它们所在的行中打印出来?还有我该如何处理标点符号和大写的问题?

【问题讨论】:

    标签: python


    【解决方案1】:

    这是我对您的代码的修改。您应该考虑使用nltk。它对单词标记化的处理更加强大。

    def main():
        fileName= raw_input('Please enter the file name: ')
    
        validate_file(fileName)
        new_lines = convert_file(fileName)
        for line in new_lines:
            print line
    
    def validate_file(fileName):
        try:
            inputFile= open(fileName, 'r')
            inputFile.close()
        except IOError:
            print('File not found.')
    
    def strip_punctuation(line):
        punctuation = ''
        line = line.strip()
        if len(line)>0:
            if line[-1] in ('.','!','?'):
                punctuation = line[-1]
                line = line[:-1]
        return line, punctuation
    
    def convert_file(fileName):
        inputFile= open(fileName, 'r')
        converted_lines = []
        for line in inputFile:
            line, punctuation = strip_punctuation(line)
            line = line.split()
            new_words = []
            for word in line:
                endString= str(word[1:])
                them=endString, str(word[0:1]), 'ay'
                new_word="".join(them)
                new_words.append(new_word)
            new_sentence = ' '.join(new_words)
            new_sentence = new_sentence.lower()
            if len(new_sentence):
                new_sentence = new_sentence[0].upper() + new_sentence[1:]
            converted_lines.append(new_sentence + punctuation)
        return converted_lines
    

    【讨论】:

    • 谢谢!但是我收到此错误:文件“/Users/tinydancer9454/Documents/python/pigLatinFile.py”,第 17 行,主要 strip_punc(line) UnboundLocalError: local variable 'line' referenced before assignment
    • 还有 new_lines 指的是什么?
    • new_lines 是指已经从英语转换为 PigLatin 的行。
    • 关于未绑定的本地错误,你确定没有把参数名改成方法吗?例如,你还在定义 "def strip_punctuation(line):" 吗?
    • @j_mcnally:此代码不需要考虑元音。
    【解决方案2】:

    我负责标点符号以外的工作。我仍在考虑解决方案。这是我的代码:

    def convert_file(fileName):
        inputFile = open(fileName,'r')
        punctuations = ['.',',','!','?',':',';']
        newWords = []
        linenum = 1
    
        for line in inputFile:
            line_string  = line.split()
            for word in line_string:
                endString= str(word[1]).upper()+str(word[2:])
                them=endString, str(word[0:1]).lower(), 'ay'
                word = ''.join(them)
                wordAndline = [word,linenum]
                newWords.append(wordAndline)
            linenum +=1
        return newWords
    

    不同之处在于它在文件中返回单词及其行。

    ['Histay', 1], ['Siay', 1], ['Naay', 1], ['Xample.eay', 1], ['Ymay', 3], ['Amenay', 3], ['Siay', 3], ['Ara!kay', 3]
    

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多