猪拉丁语-保留非字母python

猪拉丁语-保留非字母python,python,regex,non-alphanumeric,Python,Regex,Non Alphanumeric,我目前正在写一个猪拉丁代码,它要求所有的非字母都被忽略,即留在单词的同一个地方。例如,汤姆的将是om'sTay。因此,我使用以下方法删除了所有非字母字符: word=re.sub('[^A-Za-z]','',word) 我想把那些非字母带回来。 不确定如何将非字母恢复到更改单词的同一位置 In[2]: def pig_latinize(word): ...: cluster_dex = 0 ...: vowels = set('AEIOUaeiou') ...:

我目前正在写一个猪拉丁代码,它要求所有的非字母都被忽略,即留在单词的同一个地方。例如,汤姆的将是om'sTay。因此,我使用以下方法删除了所有非字母字符:
word=re.sub('[^A-Za-z]','',word)
我想把那些非字母带回来。 不确定如何将非字母恢复到更改单词的同一位置

In[2]: def pig_latinize(word):
  ...:     cluster_dex = 0
  ...:     vowels = set('AEIOUaeiou')
  ...:     for i, char in enumerate(word, start=1):
  ...:         if not char.isalpha() or char in vowels:
  ...:             break
  ...:         cluster_dex = i
  ...:     if cluster_dex == 0:
  ...:         suffix = 'hay'
  ...:     elif cluster_dex == len(word):
  ...:         cluster_dex = 1
  ...:         suffix = 'way'
  ...:     else:
  ...:         suffix = 'ay'
  ...:     return '{}{}{}'.format(word[cluster_dex:], word[:cluster_dex], suffix)
  ...: 
  ...: 
  ...: def pig_latin(words):
  ...:     return ' '.join(pig_latinize(word) for word in words.split())
  ...: 
In[3]: pig_latin("Tom's") == "om'sTay"
Out[3]: True
In[4]: pig_latin('the ap!ple is green') == 'ethay ap!plehay ishay eengray'
Out[4]: True
In[5]: pig_latin("Kate's care") == "ate'sKay arecay"
Out[5]: True
In[6]: pig_latin('myth') == 'ythmway'
Out[6]: True
In[7]: pig_latin('cry') == 'rycway'
Out[7]: True
编辑:

这显示了一种可能的方法,可以根据您给出的描述将这些函数转换为类

>>> class PigLatin(object):
...     VOWELS = set('AEIOUaeiou')
...
...     def __init__(self, to_convert):
...         self.convert_words(to_convert)
...
...     def convert_words(self, to_convert):
...         if not isinstance(to_convert, list):
...             # if to_convert is a string, split into separate words
...             to_convert = to_convert.split()
...         self.pig_latin = ' '.join(
...             self.p_latin_converter(word) for word in to_convert)
...
...     def p_latin_converter(self, word):
...         cluster_dex = 0
...         for i, char in enumerate(word, start=1):
...             if not char.isalpha() or char in self.VOWELS:
...                 break
...             cluster_dex = i
...         if cluster_dex == 0:
...             suffix = 'hay'
...         elif cluster_dex == len(word):
...             cluster_dex = 1
...             suffix = 'way'
...         else:
...             suffix = 'ay'
...         return '{}{}{}'.format(word[cluster_dex:], word[:cluster_dex], suffix)
...

# *** An object of this method initializes the string it wants to 
# convert when it is created. ***
>>> p = PigLatin("Tom's")  # initialize with a sting
# pig_latin attribute is then set with the converted text
>>> p.pig_latin == "om'sTay"
True

# *** Further, the class should have a method that allows the string to be
# modified to perform another conversion. ***
>>> p.convert_words('the ap!ple is green')  # modify the pig_latin attribute
>>> p.pig_latin == 'ethay ap!plehay ishay eengray'
True

# *** build a pigLatin_class that works with strings and lists and has a
# method to convert a string into pig latin. ***
>>> p.convert_words(['myth', 'cry'])  # use a list of strings
>>> p.pig_latin == 'ythmway rycway'
True

# *** The set of VOWELS can be defined as a static attribute of the class. ***
>>> p.VOWELS
{'E', 'e', 'A', 'O', 'a', 'i', 'u', 'I', 'U', 'o'}

# *** The class pigLatin should be based on the basic object datatype. ***
>>> isinstance(p, object)  # inherits from object "class PigLatin(object):"
True

# *** method pLatin_converter can be used. ***
>>> p.pig_latin  # current pig_latin attribute
'ythmway rycway'
# returns a word without modifying the pig_latin attribute
>>> p.p_latin_converter("Kate's care")
"ate's careKay"
# show that the pig_latin attribute wasn't modified after the above call
>>> p.pig_latin
'ythmway rycway'

... 不要删除它们?保留备份?你的确切问题是什么?听起来很有趣。你有问题吗?我还是不明白你在问什么。你能澄清一下吗?上面链接的问题对你有帮助吗?@delerious是的输入:美联社!ple是绿色输出:ethay ap!plehay ishay eengray或输入:Kate的护理输出:ate'sKay arecay非常感谢!但是输入:cry应该输出:rycwayyes对于一个没有元音的单词来说,最后一个字母是最后一个字母,后缀是添加的输入:神话输出:ythmway你能解释一下“{}{}{}{}{}”的意思吗谢谢!我必须把pig拉丁语改成一个类,因为有两个定义,我很难理解应该添加哪些变量。谢谢,谢谢!我们必须构建一个将字符串转换为pig拉丁语的类,它应该基于基本对象数据类型。该类应该有一个允许修改字符串以执行另一个转换的方法。