【问题标题】:Replacing a word in a sentence with another word用另一个词替换句子中的一个词
【发布时间】:2019-09-24 16:35:07
【问题描述】:

如果我有一个句子列表。我需要遍历每个句子并检查任何两个句子中的任何两个单词是否相同。如果是,则将第二个句子中的单词替换为已初始化的第三个单词。第三个词是常用词(var3)。例如:拉胡尔正在吃一个苹果。拉胡尔喝牛奶。输出: 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  



summary 是句子的列表。现在在这种情况下有三个句子。其中“Rahul”在两句话中是相同的。所以第二句中的单词被替换了。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 哪两句话?有两句话吗?对于每个句子的每个单词,你想看看它是否出现在任何其他句子中?如果是这样,你想替换那个词吗?
  • 是的。任意两句。因此,如果该词存在,则将第二个词替换为 var3。例如:拉胡尔正在吃一个苹果。拉胡尔正在喝牛奶。输出: Rahul 正在吃一个苹果。他在喝牛奶。
  • 在你的例子中,is 这个词怎么样?示例输出不应该是:Rahul is eating an apple. He He drinking milk 吗?
  • 在你的代码中,变量i定义在哪里?
  • 您能否编辑原始问题以澄清所有这些讨论的内容?并先进行另一次编码尝试。您需要做的是迭代每个句子,并且对于每个句子,使用索引进行迭代,以便您可以在迭代时替换单词。一旦您找到要替换的单词的第一个匹配项,您就可以开始替换 - 这可以通过 boolean 来完成。

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


【解决方案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']

【讨论】:

    【解决方案2】:

    这是一个建议的解决方案

    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)
    

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

    【讨论】:

    • 是这样的。但是如何遍历许多句子并检查? @Starter
    【解决方案3】:
    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']
    

    【讨论】:

    • 这里的句子大概有5-6句。所以我试图一次比较两个,看看里面的词是否相同。 @普拉米特
    • 只有第二句中的 Rahul 被替换,而不是两个句子。
    • 可以在不初始化replace='Rahul'的情况下完成。像遍历并检查单词是否相同,如果它们相同然后替换它
    • 替换功能已经在做。遍历和检查..为什么要再做一次??
    • 我的意思是你已经提到了要替换的内容。所以在这种情况下,replace=Rahul。但是如果必须动态地完成,它会检查每个句子中的每个单词,看它们是否相同。找到匹配项时,替换该单词。
    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 2012-02-26
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2020-01-16
    相关资源
    最近更新 更多