1

请帮我!

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

例子: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

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

4

2 回答 2

2

这是我对您的代码的修改。您应该考虑使用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
于 2013-03-14T03:06:21.133 回答
0

我做除了标点符号的工作。我仍在考虑解决方案。这是我的代码:

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]
于 2013-03-14T03:28:35.803 回答