用另一个单词替换句子中的单词

时间:2019-05-07 04:03:03

标签: python-2.7 list replace utf-8 jupyter-notebook

如果我有一个句子列表。我需要遍历每个句子,并检查两个句子中是否有两个单词相同。如果是,则将第二个句子中的单词替换为已初始化的第三个单词。第三个单词是普通单词(var3)。例如:Rahul正在吃一个苹果。拉胡尔喝牛奶。输出:Rahul正在吃一个苹果。他正在喝牛奶。

var3='तो' #word to replace if words are same 

summary=['Rahul drinks milk', 'Rahul eats rice', Seema is going to the market']
for sent in summary:
  occurences = [index for index, value in enumerate(summary) if value == sent]
  if len(occurences) > 1
for i in range(len(summary)):
    for word in i:
        var1=sent[i]
        var2=sent[i+1]
        if(var1==var2):
            var3=var1  



摘要是句子列表。现在,在这种情况下,有三个句子。其中“ Rahul”在两个句子中是相同的。因此第二句中的单词被替换了。

有人可以帮我吗?

3 个答案:

答案 0 :(得分:1)

class People():
    def __init__(self,name,replace_with):
        self.name = name
        self.replace_with = replace_with
        self.first_encountered = False
    def __str__(self):
        return self.name+"  -- "+str(self.first_encountered)

sentences = ["Rahul is eating an apple.",
             "Rahul drinks milk.",
             "Rahul also drinks Beer.",
             "Rahul likes Pizza",
             "Seema is going to the market",
             "Seema also drinks beer",
             "and i am going to hell"
             ]

names= ["Rahul", "Seema"]
replaces = ["He","She"]

people = [ People(n,r) for n,r in zip(names,replaces) ]

new_sentence = []
found_in_any = [False,False]
for sentence in sentences:
    for index,person in enumerate(people):
        if(sentence.find(person.name)!=-1):
            found_in_any[index] = True           
            if(not person.first_encountered):
                person.first_encountered = True
                new_sentence.append(sentence)
                continue
            if(person.first_encountered):
                new_sentence.append(sentence.replace(person.name,person.replace_with)) 
        else:
            found_in_any[index] = False

        if len(list(set(found_in_any))) == 1 and list(set(found_in_any))[0] == False:
            new_sentence.append(sentence) 

print(new_sentence)

output : ['Rahul is eating an apple.',
 'He drinks milk.',
 'He also drinks Beer.',
 'He likes Pizza',
 'Seema is going to the market',
 'Seema is going to the market',
 'She also drinks beer',
 'and i am going to hell']

答案 1 :(得分:0)

sentences = ["Rahul is eating an apple.","Rahul drinks milk.","Rahul also drinks Beer.","Rahul likes Pizza","Seema is going to the market"]

new_sentence = [] first_encountered = False for sentence in sentences:
    if(sentence.find(replace)!=-1):
        if(not first_encountered):
            first_encountered = True
            new_sentence.append(sentence)
            continue
        if(first_encountered):
            new_sentence.append(sentence.replace(replace,replace_with)) 
    else:
        new_sentence.append(sentence) new_sentence

输出:

['Rahul is eating an apple.',
 'He drinks milk.',
 'He also drinks Beer.',
 'He likes Pizza',
 'Seema is going to the market']

答案 2 :(得分:0)

这是一个建议的解决方案

sen1 = "Rahul is eating an apple"
sen2 = "Rahul drinks milk"
var = "He"

for i in sen1.split(" "):
    if i in sen2.split(" "):
        sen2 = sen2.replace(i, var)

print(sen1)
print(sen2)

输出: 拉胡尔正在吃一个苹果。 他喝牛奶