r - Quanteda - 从具有多个文档的数据框创建语料库
问题描述
这里的第一个问题,所以为任何失礼道歉。我在 R 中有一个包含 4 个变量的 657 个观测值的数据框。每个观察都是澳大利亚总理的演讲或采访。所以变量是:
- 日期
- 标题
- 网址
- txt(演讲/采访的全文)。
我正试图把它变成 Quanteda 中的语料库
我第一次尝试corp <- corpus(all_content)
,但这给了我一条错误消息
Error in corpus.data.frame(all_content) :
text_field column not found or invalid
这虽然有效:corp <- corpus(paste(all_content))
然后summary(corp)
这给了我
Corpus consisting of 4 documents, showing 4 documents:
Text Types Tokens Sentences
text1 243 1316 1
text2 1095 6523 3
text3 661 2630 1
text4 25243 1867648 62572
我的理解是,这样做是有效地将每一列变成一个文档,而不是每一行?
如果重要,txt
变量将保存为列表。用于创建每一行的代码是
```{r new_function}
scrape_speech <- function(url){
speech_page <- read_html(url)
date <- speech_page %>% html_nodes(".date-display-single") %>% html_text() %>% dmy()
title <- speech_page %>% html_nodes(".pagetitle") %>% html_text()
txt <- speech_page %>% html_nodes("#block-system-main p") %>% html_text() %>% list()
tibble (date = date, title = title, URL = url, txt=txt)}
然后,我使用该map_dfr
函数遍历并抓取了 657 个单独的 URL。
有人向我建议这是因为它txt
被保存为列表。我试过不使用list()
in 函数,得到 21,904 个观察结果,因为全文文档中的每个段落都变成了单独的观察结果。我可以把它变成一个语料库corp <- corpus(paste(all_content_not_list))
(再一次,没有paste
我得到与上面相同的错误)。这同样给了我语料库中的 4 个文档!
summary(corp)
给我
Corpus consisting of 4 documents, showing 4 documents:
Text Types Tokens Sentences
text1 243 43810 1
text2 1092 214970 25
text3 657 87618 1
text4 25243 1865687 62626
提前感谢丹尼尔
解决方案
很难准确解决这个问题,因为您的 data.frame 对象没有可重现的示例,但如果结构包含您列出的变量,那么应该这样做:
corpus(all_content, text_field = "txt")
详情请参阅?corpus.data.frame
。如果这样做不行,请尝试将输出添加到您的问题中
str(all_content)
以便我们可以更详细地查看您的all_content
对象中的内容。
在 OP 添加新数据后进行了编辑:
好的,所以txt
在你的小标题中是一个字符元素列表。您需要将它们组合成一个字符才能将其用作corpus.data.frame()
. 就是这样:
library("quanteda")
## Package version: 3.0.0
## Unicode version: 10.0
## ICU version: 61.1
## Parallel computing: 12 of 12 threads used.
## See https://quanteda.io for tutorials and examples.
dframe <- structure(list(
date = structure(18620, class = "Date"),
title = " Prime Minister's Christmas Message to the ADF",
URL = "https://www.pm.gov.au/media/prime-ministers-christmas-message-adf",
txt = list(c(
"G'day and Merry Christmas to everyone in our Australian Defence Force.",
"You know, throughout our history, successive Australian governments... And this year was no different.",
"God bless."
))
),
row.names = c(NA, -1L),
class = c("tbl_df", "tbl", "data.frame")
)
dframe$txt <- vapply(dframe$txt, paste, character(1), collapse = " ")
corp <- corpus(dframe, text_field = "txt")
print(corp, max_nchar = -1)
## Corpus consisting of 1 document and 3 docvars.
## text1 :
## "G'day and Merry Christmas to everyone in our Australian Defence Force. You know, throughout our history, successive Australian governments... And this year was no different. God bless."
由reprex 包于 2021-04-08 创建(v1.0.0)
推荐阅读
- javafx - 如何从矩形中找到图像模式?
- python - 在 windows 上安装 biopython(使用 mobaxterm)
- splunk - 如何使通过公共相关 ID 链接的变量显示在 Splunk 表的同一行上?
- ansible - 如何将文件从远程主机拉取并推送到另一台远程主机?
- php - 无法在 wordpress 代码段中对 foreach 进行排序
- mongodb - MongoDB 嵌入文档作为相关字段的容器
- vba - 运行时错误“5”:运行秒时的参数无效 = Dir
- windows-7 - 自己的程序性能大幅下降
- python - 将“数字”字符串转换为日期时间 python
- python - 当我尝试运行 SendKeys.py 时,Python 无法识别 _sendkeys.c 文件