udpipe (keywords_rake) 如何将 link 关键字添加到从中提取的文档

udpipe (keywords_rake) how to link keywords to the document they where extracted from

我正在使用 udpipe 包(针对 R)中的函数 keywords_rake 从一堆文档中提取关键字。

udmodel_en <- udpipe_load_model(file = dl$file_model)
x <- udpipe_annotate(udmodel_en, x = data$text)
x <- as.data.frame(x)

keywords <- keywords_rake(x = x, term = "lemma", group = "doc_id", 
                          relevant = x$xpos %in% c("NN", "JJ"), ngram_max = 2)

数据看起来像这样

  Text
  "cats are nice but dogs are better..."
  "I really like dogs..."
  "red flowers are pretty, especially roses..."
  "once I saw a blue whale ..."
  ....

(每一行是一个单独的文档)

但是输出不包括关键字的来源,并提供了所有文档的关键字列表

我怎样才能link将这些关键词对应到它们所取自的文档中呢? (即每个文档都有一个关键字列表)

像这样:

      keywords
doc1   dog, cat, blue whale
doc2   dog 
doc3   red flower, tower, Donald Trump 

您可以使用 txt_recode_ngramkeywords_rake 的结果来做到这一点。好处是一切都回到原来的状态 data.frame 然后你可以 select 你需要什么。请参阅下面使用 udpipe 提供的数据集的示例。

免责声明:从 udpipe 的 github 页面上的 issue 41 中的 jwijffels 答案复制的代码。

data(brussels_reviews_anno)
x <- subset(brussels_reviews_anno, language == "nl")
keywords <- keywords_rake(x = x, term = "lemma", group = "doc_id", 
                          relevant = x$xpos %in% c("NN", "JJ"), sep = "-")
head(keywords)

            keyword ngram freq     rake
1  openbaar-vervoer     2   19 2.391304
2         heel-fijn     2    2 2.236190
3  heel-vriendelijk     2    3 2.131092
4 herhaling-vatbaar     2    6 2.000000
5  heel-appartement     2    2 1.935450
6 steenworp-afstand     2    4 1.888889

x$term <- txt_recode_ngram(x$lemma, compound = keywords$keyword, ngram = keywords$ngram, sep = "-")
x$term <- ifelse(!x$term %in% keywords$keyword, NA, x$term)
head(x[!is.na(x$term), ])

        doc_id language sentence_id token_id       token      lemma xpos                term
67039 19991431       nl        4379       11         erg        erg   JJ        erg-centraal
67048 19991431       nl        4379       20        leuk       leuk   JJ          leuk-adres
67070 21054450       nl        4380        6       goede       goed   JJ        goed-locatie
67077 21054450       nl        4380       13    Europese   europees   JJ       europees-wijk
67272 23542577       nl        4393       84 uitstekende uitstekend   JJ uitstekend-gastheer
67299 40676307       nl        4396       25   gezellige   gezellig   JJ      gezellig-buurt