我有一个非常大的数据框,其中有两列称为sentence1
和sentence2
。我试图用两个句子之间不同的词来创建新列,例如:
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
在RI中,尝试拆分句子,并在获得列表之间不同的元素后,例如:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句