【问题标题】:Python translator, how to replace just one wordPython翻译器,如何只替换一个单词
【发布时间】:2012-05-06 19:13:33
【问题描述】:

所以我正在尝试在 python 中创建一个翻译器(在 s60 设备中)。所以我们要做的是只替换一个完整的单词而不触及其他单词。这是一个例子

原文:“棕狐跳过了名叫布朗尼的狗。” 我想把“brown”这个词换成“deathlesi”(忽略为什么) 结果应该是: “死亡狐狸跳过了一只名叫布朗尼的狗。” 但相反,它也会更改字符串中的“brownie”,从而导致: “deathlesi 狐狸跳过了名为 deathlesiie 的狗。”

由于我试图替换每一个单词,有时它会陷入一个永无止境的悖论。 例子: “我很蠢” 我正在尝试将“I”更改为“ium”,这就是发生的事情。 “iumumumumumumumumumumumumumumumumumumum.... am stupiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuim..”,它基本上改变了字符串中的每个“I”,直到字符串中没有“I”才会停止。

有什么帮助吗?谢谢!

编辑:我已经尝试过“stringhere”.replace(),但某些部分,如小写“i”通常会替换愚蠢的“i”。

这是另一个例子: “人们对巨型野兔感到兴奋。”将“are”替换为“iume”,而不是 “人们对巨型野兔感到兴奋。”它还取代了“野兔”,导致 “人们 iume 对巨大的 hiume 感到兴奋。”

假设我排列了句子并翻译了它们中的每一个。 这就是我现在的方法。基本上将每个单词转换成一个数组并转换它们中的每一个。然后做一个

translated_sentence=["particulus:people", "iume:are", "geus:getting", "exchantus:excited", "d:at", "qun:the", "gesas:giant", "hsont:hare"]
sentence= "People are getting excited at the giant hare."
for i in translated_sentence do
element=i.split(":")
sentence=sentence.replace(element[1], element[0])

它仍然抛出“particulus uime geus exchantus d qun gesas huime(而不是 hsont)”

我刚刚弄明白了。 我只是将字符串拆分为一个数组,并通过清理当前单词并对原始单词执行 string.replace() 来保留格式。

sentence="The quick brown fox jumps over the lazy dog.".split(" ")
result=""

for i in sentence:

cleaned=clean(i) #removes the punctuations and stuff leaving the raw word.

translated=translate(cleaned) #returns the translated word

result=result+i.replace(cleaned,translated)+" "

return result

【问题讨论】:

    标签: python text replace language-translation


    【解决方案1】:

    这听起来像是一个正则表达式场景:

    import re
    x = "The brown fox jumps over the dog named brownie."
    newstring = re.sub(r"(\s+|[:punct:]+|^)brown(\s+|[:punct:]+|$)",r"\1deathlies\2",x, flags=re.IGNORECASE)
    

    产量:

    >>> print newstring
    The deathlies fox jumps over the dog named brownie.
    

    或者:

    x = "People are getting excited at the giant hare."
    newstring = re.sub(r"(\s+|[:punct:]+|^)are(\s+|[:punct:]+|$)",r"\1iume\2",x, flags=re.IGNORECASE)
    

    产量:

    >>> print newstring
    People iume getting excited at the giant hare.
    

    第一个捕获组(\s+|[:punct:]+|^) 匹配空格、标点符号或字符串的开头,另一个组(\s+|[:punct:]+|$) 匹配字符串的结尾。

    在进行替换时,\1\2 将标点或空格与被替换的文本一起放回原处,使事情变得整洁。

    PS

    如果你很懒,只需创建捕获组(\W+|^)(\W+|$) ...

    【讨论】:

    • 谢谢,它工作...在我的电脑上...不是在我的 s60 设备上。没关系,虽然我明白了。
    【解决方案2】:

    由于您只想找到第一次出现,您只需要一种方法来跟踪它。您可以通过多种方式做到这一点。就这么简单:

    def replacer(original, looking_for, replace_with):
       ''' A straightforward way... '''
       return original.replace(looking_for, replace_with, 1)
       #return regex.sub(replace_with, looking_for, 1)
    

    数字表示您要替换多少次。如果存在两个,并且您输入 2,则两个匹配项都将被替换。

    字符串是不可变的,因此您必须重新分配新字符串。每次您执行replace 时,您都会生成一个新字符串。

    如果你不想要内置的,你也可以编写一个循环来查找第 N 次出现。

    我建议缩短您的帖子(我的意思是更少的单词,更多的语法亮点)。格式化它。 如果我没有正确阅读您的帖子,请纠正我。

    【讨论】:

      【解决方案3】:

      只需调用字符串的替换函数

      “我很蠢”.replace("I", "ium")

      【讨论】:

      • 是的,我试过了,但是像小写“i”这样的某些部分通常会替换愚蠢的“i”。
      【解决方案4】:

      我现在没有python,但是如何创建一个将字符串转换为列表的函数。您可以取出空白,因此列表将是 [The, brown, fox, jumps...]。然后做一个 .replace。

      【讨论】:

      • 这是我现在的方法。并做一个
      【解决方案5】:

      您想替换完全相等的单词。不是 string.replace()

      替换“are”,但不要替换“hare”

      如果是这样的话

      已编辑

      正如@Niall 所说,Regular Expression search and replace 是满足您任务的最佳工具。

      或者,如果您刚刚开始学习 Python 并且正则表达式过于复杂。只需使用str.split() 将字符串拆分为单词,然后循环遍历单词。

      def simply_replace(string, search, replace):
          words = string.split(' ')
          for i in range(len(words)):
              if(words[i].lower() == search):
                  words[i] = replace
          return ' '.join(words)
      
      >>> simply_replace("I am stupid", 'i', 'ium')
      'ium am stupid'
      >>> simply_replace("The brown fox jumps over the dog named brownie.", 'brown', 'deathly')
      'The deathly fox jumps over the dog named brownie.'
      >>> simply_replace("People are getting excited at the giant hare.", 'are', 'ium')
      'People ium getting excited at the giant hare.'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-13
        • 1970-01-01
        • 1970-01-01
        • 2014-09-18
        • 2017-11-13
        • 2018-10-30
        • 2015-09-11
        • 1970-01-01
        相关资源
        最近更新 更多