简体   繁体   English

Python翻译,如何只替换一个单词

[英]Python translator, how to replace just one word

So i'm trying to create a translator in python(in a s60 device). 所以我想在python中创建一个翻译器(在s60设备中)。 So what 'm trying to do is to replace just one whole word without touching the other words. 所以我要做的就是在不触及其他单词的情况下替换一个完整的单词。 Here's an example 这是一个例子

Original: "The brown fox jumps over the dog named brownie." 原文:“棕色的狐狸跳过名叫布朗尼的狗。” I want to replace the word "brown" into "deathlesi"(Just ignore why) The result should be: "The deathlesi fox jumps over the dog named brownie." 我想把“褐色”改为“deathlesi”(只是忽略原因)结果应该是:“死神狐狸跳过名叫布朗尼的狗。” But instead it also changes "brownie" in the string which results to: "The deathlesi fox jumps over the dog named deathlesiie." 但相反,它也改变了字符串中的“布朗尼”,结果是:“死神狐狸跳过名为deathlesiie的狗。”

Since I'm trying to replace each and every word, sometimes it goes into a never ending paradox. 因为我试图取代每一个词,有时它会陷入永无止境的悖论。 Example: "I am stupid" I'm trying to change "I" into "ium" and this is what happens. 例如:“我很蠢”我试图将“我”变成“ium”,这就是所发生的事情。 "iumumumumumumumumumumumumumumumumumumumum.... am stupiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuim..", it basically changes every "I" in the string and won't stop until there's no "I" in the string. “iumumumumumumumumumumumumumumuiuiumumumumumum ...”,它基本上改变了字符串中的每个“I”,并且在字符串中没有“I”之前不会停止。

Any help? 有帮助吗? Thanks! 谢谢!

Edit: I already tried "stringhere".replace() but certain parts like a lowercase "i" usually replaces the "i" in stupid. 编辑:我已经尝试了“stringhere”.replace()但是像小写“i”这样的某些部分通常会替换愚蠢的“i”。

Here's another example: "People are getting excited at the giant hare." 这是另一个例子:“人们对巨型野兔感到兴奋。” replacing "are" to "iume", instead of "People iume getting excited at the giant hare." 将“are”替换为“iume”,而不是“人们对巨型野兔感到兴奋”。 it also replaced "hare" which resulted into "People iume getting excited at the giant hiume." 它也取代了“野兔”,导致“人们对巨人的兴奋感到兴奋”。

Supposedly I array'ed the sentence and translate each of them. 据说我把这个句子排成一行并翻译出来。 That is my current method now. 那是我现在的方法。 Basically converting each word into a array and converting each one of them. 基本上将每个单词转换为数组并转换它们中的每一个。 Then doing a 然后做一个

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])

and still it throws a "particulus uime geus exchantus d qun gesas huime(instead of hsont)" 并且它仍然抛出一个“粒子uime geus exchantus d qun gesas huime(而不是hsont)”

I just got it figured it out. 我刚才弄清楚了。 I just splitted the string into an array, and preserved the formatting by cleaning the current word and doing a string.replace() to the original word. 我只是将字符串拆分成一个数组,并通过清理当前单词并对原始单词执行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

This sounds like a regex scenario: 这听起来像一个正则表达式场景:

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)

Which yields: 产量:

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

Or: 要么:

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)

Which Yields: 哪个收益率:

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

The first capture group (\\s+|[:punct:]+|^) matches a space, punctuation or the beginning of the string, and the other group (\\s+|[:punct:]+|$) matches the end of the string. 第一个捕获组(\\s+|[:punct:]+|^)匹配空格,标点符号或字符串的开头,另一组(\\s+|[:punct:]+|$)匹配结束字符串。

When making the replacement, the \\1 and \\2 put the puncuation or spacing back with the replaced text- making things neat. 在进行更换时, \\1\\2将更换的文字制作物整齐地放回去。

PS PS

If you're lazy, just make the capture groups (\\W+|^) and (\\W+|$) ... 如果你很懒,只需制作捕捉组(\\W+|^)(\\W+|$) ......

Since you only want to find the first occurrence, you just need a way to keep track of it. 由于您只想找到第一个匹配项,因此您只需要一种方法来跟踪它。 You can do this many ways. 你可以做很多事。 As simple as this: 就这么简单:

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)

The number indicates how many occurrences do you want to replace. 该数字表示您要替换的次数。 If there exists two, and you put 2, both occurrences will be replaced. 如果存在两个,并且您输入2,则两个出现都将被替换。

String is immutable, so you must re-assign the new string. 字符串是不可变的,因此您必须重新分配新字符串。 Each time you do replace you are generating a new string. 每次replace都会生成一个新字符串。

You can also write a loop to find the N-th occurrence if you don't want the built-in. 如果不需要内置函数,也可以编写一个循环来查找第N个匹配项。

I recommend making your post shorter (I mean fewer words, and more syntax highlight). 我建议你缩短帖子(我的意思是更少的单词,更多的语法高亮)。 Format it. 格式化它。 Correct me if I didn't read your post correctly. 如果我没有正确阅读你的帖子,请纠正我。

Just call replace function of string 只需调用string的replace函数

"I am stupid".replace("I", "ium")

I don't have python with me right now, but how about making a function to convert the string into a list. 我现在没有python,但是如何创建一个函数将字符串转换为列表。 You can take out white space, so the list would be [The, brown, fox, jumps...]. 你可以拿出空白区域,所以列表将是[The,brown,fox,jumps ...]。 Then do a .replace. 然后做一个.replace。

You want to replace the exact equal word. 你想要替换完全相同的单词。 not a string.replace() 不是string.replace()

replace "are" but don't replace "hare" 替换“是”但不要取代“野兔”

if that's the case 如果是这样的话

edited 编辑

as @Niall said Regular Expression search and replace is the best tool to satisfy your tasks. 正如@Niall所说, 正则表达式搜索和替换是满足您任务的最佳工具。

alternatively, if you've just started learning Python and regex is too complicate. 或者,如果你刚开始学习Python并且正则表达式太复杂了。 just split string to words using str.split() then loops through the words. 只需使用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.'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM