我想用R来比较不同元素之间的文字和摘录部分。
考虑a和b两个文本段落。一种是另一种的修改版本:
a <- "This part is the same. This part is old."
b <- "This string is updated. This part is the same."我希望比较这两个字符串,并接收字符串中对这两个字符串中的任何一个都是唯一的部分作为输出,最好对两个输入字符串分开。
预期产出:
stringdiff <- list(a = " This part is old.", b = "This string is updated. ")
> stringdiff
$a
[1] " This part is old."
$b
[1] "This string is updated. "我尝试过来自Extract characters that differ between two strings的解决方案,但这只是比较独特的字符。Simple Comparing of two texts in R中的答案更接近,但仍然只是比较独特的词。
有没有办法在没有太多麻烦的情况下获得预期的产出?
发布于 2017-11-29 09:10:22
我们将这两个字符串连在一起,在.之后的空间分割,以创建一个句子的list ( 'lst‘),从unlist ( 'un1’)中获取unique元素,使用setdiff我们得到不在‘un1’中的元素。
lst <- strsplit(c(a= a, b = b), "(?<=[.])\\s", perl = TRUE)
un1 <- unique(unlist(lst))
lapply(lst, setdiff, x= un1)https://stackoverflow.com/questions/47548826
复制相似问题