猪拉丁语翻译 [英] Pig Latin Translator
本文介绍了猪拉丁语翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我有一个基本的Pig拉丁语翻译只能用于一个单词。
So, I have a basic Pig Latin translator that only works for one word.
def Translate(Phrase):
Subscript = 0
while Phrase[Subscript] != "a" or Phrase[Subscript] != "e" or Phrase[Subscript] != "i" or
Phrase[Subscript] != "o" or Phrase[Subscript] != "u":
Subscript += 1
if Phrase[Subscript] == "a" or Phrase[Subscript] == "e" or Phrase[Subscript] == "i" or
Phrase[Subscript] == "o" or Phrase[Subscript] == "u":
return Phrase[Subscript:] + Phrase[:Subscript] + "ay"
有人可以帮我编辑这个翻译是为了获取多个单词?谢谢。
Can someone please assist me in editing this translator in order to take more than one word? Thank you.
推荐答案
这是猪拉丁方言,考虑了单词的发音方式:
Here's pig latin dialect that takes into account how the words are pronounced:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
sentences = ["Pig qoph an egg.",
"Quiet European rhythms.",
"My nth happy hour.",
"Herb unit -- a dynasty heir."]
for sent in sentences:
entsay = " ".join(["".join(map(to_piglatin, re.split("(\W+)", nonws)))
for nonws in sent.split()])
print(u'"{}" → "{}"'.format(sent, entsay))
输出
Output
"Pig qoph an egg." → "igpay ophqay anway eggway."
"Quiet European rhythms." → "ietquay uropeaneay ythmsrhay."
"My nth happy hour." → "ymay nthway appyhay hourway."
"Herb unit -- a dynasty heir." → "herbway itunay -- away ynastyday heirway."
注意:
-
- way
后缀用于以元音开头的单词 -
qu
inquiet被视为一个单位 -
欧洲
,单位
以辅音开头 -
y
inrhythms,dynasty是元音 -
nth
,小时
,herb
,继承人
以元音开头
"-way"
suffix is used for words that start with a vowel soundqu
in "quiet" is treated as a unitEuropean
,unit
start with a consonanty
in "rhythms", "dynasty" is a vowelnth
,hour
,herb
,heir
start with a vowel
其中 to_piglatin ()
是:
from nltk.corpus import cmudict # $ pip install nltk
# $ python -c "import nltk; nltk.download('cmudict')"
def to_piglatin(word, pronunciations=cmudict.dict()):
word = word.lower() #NOTE: ignore Unicode casefold
i = 0
# find out whether the word start with a vowel sound using
# the pronunciations dictionary
for syllables in pronunciations.get(word, []):
for i, syl in enumerate(syllables):
isvowel = syl[-1].isdigit()
if isvowel:
break
else: # no vowels
assert 0
if i == 0: # starts with a vowel
return word + "way"
elif "y" in word: # allow 'y' as a vowel for known words
return to_piglatin_naive(word, vowels="aeiouy", start=i)
break # use only the first pronunciation
return to_piglatin_naive(word, start=i)
def to_piglatin_naive(word, vowels="aeiou", start=0):
word = word.lower()
i = 0
for i, c in enumerate(word[start:], start=start):
if c in vowels:
break
else: # no vowel in the word
i += 1
return word[i:] + word[:i] + "w"*(i == 0) + "ay"*word.isalnum()
要将文本拆分成句子,可以使用的单词 nltk
tokenizers。可以修改代码以尊重字母的大小写(大写/小写),收缩。
To split the text into sentences, words you could use nltk
tokenizers. It is possible to modify the code to respect letters' case (uppercase/lowercase), contractions.
这篇关于猪拉丁语翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文