将向量转换为 VCorpus 对象(2)
现在我们已经把向量转换成了 Source 对象,接下来将它传给另一个 tm 函数 VCorpus(),以创建易失性语料库。相当直接,对吧?
VCorpus 对象是一个嵌套列表(列表的列表)。在 VCorpus 对象的每个索引位置上都有一个 PlainTextDocument 对象,它本身是一个列表,包含实际的文本数据(content)以及相应的元数据(meta)。为了帮助理解整体结构,您可以可视化 一个 VCorpus 对象。
要查看单个文档对象(第 10 个),请使用双层方括号取子集。
coffee_corpus[[10]]
要查看实际的"文本",需要对列表进行两次索引。若要访问文档的元数据(如时间戳),将 [1] 改为 [2]。另一种查看纯"文本"的方式是使用 content() 函数,它不需要第二层方括号。
coffee_corpus[[10]][1]
content(coffee_corpus[[10]])
本练习是课程的一部分
使用 R 的 Bag-of-Words 进行文本挖掘
练习说明
- 对
coffee_source对象调用VCorpus()函数以创建coffee_corpus。 - 通过将其打印到控制台,验证
coffee_corpus是一个VCorpus对象。 - 将
coffee_corpus的第 15 个元素打印到控制台,验证它是一个包含第 15 条推文内容和元数据的PlainTextDocument。使用双层方括号取子集。 - 打印
coffee_corpus中第 15 条推文的内容。使用双层方括号选择相应推文,然后用单层方括号提取该推文的内容。 - 打印
coffee_corpus中第 10 条推文的content()。
交互式实操练习
通过完成这段示例代码来试试这个练习。
## coffee_source is already in your workspace
# Make a volatile corpus from coffee_source
coffee_corpus <- ___
# Print out coffee_corpus
___
# Print the 15th tweet in coffee_corpus
___
# Print the contents of the 15th tweet in coffee_corpus
___
# Now use content to review the plain text of the 10th tweet
___(___[[___]])