gpt4 book ai didi

r - R中两个文本的简单比较

转载 作者:行者123 更新时间:2023-12-04 23:30:15 25 4
gpt4 key购买 nike

我想比较两个文本的相似性,因此我需要一个简单的函数来清楚地按时间顺序列出两个文本中出现的单词和短语。这些单词/句子应突出显示或加下划线以更好地可视化)

在@joris Meys 的想法的基础上,我添加了一个数组来将文本分为句子和从句。

这是它的样子:

  textparts <- function (text){
textparts <- c("\\,", "\\.")
i <- 1
while(i<=length(textparts)){
text <- unlist(strsplit(text, textparts[i]))
i <- i+1
}
return (text)
}

textparts1 <- textparts("This is a complete sentence, whereas this is a dependent clause. This thing works.")
textparts2 <- textparts("This could be a sentence, whereas this is a dependent clause. Plagiarism is not cool. This thing works.")

commonWords <- intersect(textparts1, textparts2)
commonWords <- paste("\\<(",commonWords,")\\>",sep="")


for(x in commonWords){
textparts1 <- gsub(x, "\\1*", textparts1,ignore.case=TRUE)
textparts2 <- gsub(x, "\\1*", textparts2,ignore.case=TRUE)
}
return(list(textparts1,textparts2))

但是,有时它起作用,有时不起作用。

我希望得到这样的结果:
>   return(list(textparts1,textparts2))
[[1]]
[1] "This is a complete sentence" " whereas this is a dependent clause*" " This thing works*"

[[2]]
[1] "This could be a sentence" " whereas this is a dependent clause*" " Plagiarism is not cool" " This thing works*"

而我没有得到任何结果。

最佳答案

@Chase 的回答有一些问题:

  • 不考虑大小写差异
  • 间断会弄乱结果
  • 如果有不止一个相似的词,那么由于 gsub 调用,您会收到很多警告。

  • 基于他的想法,有以下解决方案利用 tolower()以及正则表达式的一些不错的功能:
    compareSentences <- function(sentence1, sentence2) {
    # split everything on "not a word" and put all to lowercase
    x1 <- tolower(unlist(strsplit(sentence1, "\\W")))
    x2 <- tolower(unlist(strsplit(sentence2, "\\W")))

    commonWords <- intersect(x1, x2)
    #add word beginning and ending and put words between ()
    # to allow for match referencing in gsub
    commonWords <- paste("\\<(",commonWords,")\\>",sep="")


    for(x in commonWords){
    # replace the match by the match with star added
    sentence1 <- gsub(x, "\\1*", sentence1,ignore.case=TRUE)
    sentence2 <- gsub(x, "\\1*", sentence2,ignore.case=TRUE)
    }
    return(list(sentence1,sentence2))
    }

    这给出了以下结果:
    text1 <- "This is a test. Weather is fine"
    text2 <- "This text is a test. This weather is fine. This blabalba This "

    compareSentences(text1,text2)
    [[1]]
    [1] "This* is* a* test*. Weather* is* fine*"

    [[2]]
    [1] "This* text is* a* test*. This* weather* is* fine*. This* blabalba This* "

    关于r - R中两个文本的简单比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6136749/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com