Простое сравнение двух текстов в R - PullRequest
4 голосов
/ 26 мая 2011

Я хочу сравнить два текста по сходству, поэтому мне нужна простая функция, чтобы четко и в хронологическом порядке перечислить слова и фразы, встречающиеся в обоих текстах.эти слова / предложения должны быть выделены или подчеркнуты для лучшей наглядности)

на основе идей @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*"           

, тогда какя не получаю результатов.

Ответы [ 2 ]

5 голосов
/ 30 мая 2011

Есть некоторые проблемы с ответом @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* "
3 голосов
/ 26 мая 2011

Я уверен, что на странице обработки естественного языка есть намного более надежные функции, но вот одно решение, использующее intersect() для поиска общих слов. Подход состоит в том, чтобы прочитать два предложения, определить общие слова и gsub() их комбинацией слова и прозвища по нашему выбору. Здесь я решил использовать *, но вы можете легко это изменить или добавить что-то еще.

sent1 <- "I shot the sheriff."
sent2 <- "Dick Cheney shot a man."

compareSentences <- function(sentence1, sentence2) {
  sentence1 <- unlist(strsplit(sentence1, " "))
  sentence2 <- unlist(strsplit(sentence2, " "))

  commonWords <- intersect(sentence1, sentence2)

  return(list(
      sentence1 = paste(gsub(commonWords, paste(commonWords, "*", sep = ""), sentence1), collapse = " ")
    , sentence2 = paste(gsub(commonWords, paste(commonWords, "*", sep = ""), sentence2), collapse = " ")
    ))
}

> compareSentences(sent1, sent2)
$sentence1
[1] "I shot* the sheriff."

$sentence2
[1] "Dick Cheney shot* a man."
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...