Python 猪拉丁文转换器
请帮帮我!
我正在把一个包含多行的文本文件转换成猪拉丁语。
举个例子:这句话 "This is an example." 转换成猪拉丁语后应该是 "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
我该如何让它们按照原来的行打印出来?还有,我该如何处理标点符号和大小写的问题?
2 个回答
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]
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