python - 提取两个句子之间不同的词

标签 python r python-2.7 python-3.x

我有一个非常大的数据框,其中有两列,分别称为 sentence1sentence2。 我正在尝试用两个句子之间不同的词创建一个新列,例如:

sentence1=c("This is sentence one", "This is sentence two", "This is sentence three")
sentence2=c("This is the sentence four", "This is the sentence five", "This is the sentence six")
df = as.data.frame(cbind(sentence1,sentence2))

我的数据框具有以下结构:

ID    sentence1                    sentence2
 1     This is sentence one         This is the sentence four
 2     This is sentence two         This is the sentence five
 3     This is sentence three       This is the sentence six

我的预期结果是:

ID    sentence1        sentence2     Expected_Result
 1     This is ...      This is ...   one the four 
 2     This is ...      This is ...   two the five
 3     This is ...      This is ...   three the six

在 R 中,我试图拆分句子,然后得到列表之间不同的元素,例如:

df$split_Sentence1<-strsplit(df$sentence1, split=" ")
df$split_Sentence2<-strsplit(df$sentence2, split=" ")
df$Dif<-setdiff(df$split_Sentence1, df$split_Sentence2)

但是这种方法在应用setdiff时不起作用...

在 Python 中,我尝试应用 NLTK,尝试先获取标记,然后提取两个列表之间的差异,例如:

from nltk.tokenize import word_tokenize

df['tokensS1'] = df.sentence1.apply(lambda x:  word_tokenize(x))
df['tokensS2'] = df.sentence2.apply(lambda x:  word_tokenize(x))

在这一点上,我没有找到可以给我所需结果的函数..

我希望你能帮助我。谢谢

最佳答案

这是一个 R 解决方案。

我创建了一个 exclusiveWords 函数,用于查找两组之间唯一的单词,并返回由这些单词组成的“句子”。我将它包装在 Vectorize() 中,以便它同时处理 data.frame 的所有行。

df = as.data.frame(cbind(sentence1,sentence2), stringsAsFactors = F)

exclusiveWords <- function(x, y){
    x <- strsplit(x, " ")[[1]]
    y <- strsplit(y, " ")[[1]]
    u <- union(x, y)
    u <- union(setdiff(u, x), setdiff(u, y))
    return(paste0(u, collapse = " "))
}

exclusiveWords <- Vectorize(exclusiveWords)

df$result <- exclusiveWords(df$sentence1, df$sentence2)
df
#                sentence1                 sentence2        result
# 1   This is sentence one This is the sentence four  the four one
# 2   This is sentence two This is the sentence five  the five two
# 3 This is sentence three  This is the sentence six the six three

关于python - 提取两个句子之间不同的词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43358543/

相关文章:

python - 尽管 xticks 有值(value),如何使它们均匀分布?

Python 在 __new__ 方法中检查可迭代

r - Selectizeinput 输入互斥 R Shiny

python-2.7 - 如何杀死/停止 openshift 服务器进程以释放服务端口?错误 : "Address already in use"

django - Paypal 丢失 session 数据

python - 为什么 `tf.matmul` 不能与转置张量一起使用?

r - R是编译语言吗?

r - 如何使用 ggplot 制作多柱图表,给定一列包含二进制数据

python-2.7 - 如何为 pyspark 中的 s3 指定服务器端加密?

python - BeautifulSoup 在Python中提取没有类的值