TM 回顾(I)
在课程 Text Mining: Bag of Words 中,您学习了语料库(corpus)是一组文本,并了解了若干用于预处理文本的函数。这里简要回顾一下:创建并清洗语料库的一种方式如下。尽管这是另一门课程,情感分析属于文本挖掘的一部分,因此复习会很有帮助。
- 使用
VectorSource()将字符向量转换为文本源。 - 使用
VCorpus()将文本源转换为语料库。 - 使用
tm提供的清洗函数(如removePunctuation()和stripWhitespace()),以及qdap的replace_abbreviation()从语料库中移除不需要的字符。
在本练习中,我们已用常见的预处理函数构建了一个自定义的 clean_corpus() 函数,便于直接应用。
clean_corpus() 接收 VCorpus() 的输出并应用清洗函数。例如:
processed_corpus <- clean_corpus(my_corpus)
本练习是课程的一部分
R 中的情感分析
练习说明
您的 R 会话中已提供一个文本向量 tm_define(包含两个短文档)以及函数 clean_corpus()。
- 将
VectorSource()应用于tm_define,创建名为tm_vector的对象。 - 对
tm_vector使用VCorpus()创建tm_corpus。 - 使用
content()查看tm_corpus中第 1 篇文档的内容。- 语料库中的文档通过列表语法访问,请使用双中括号,例如
[[1]]。
- 语料库中的文档通过列表语法访问,请使用双中括号,例如
- 对
tm_corpus使用自定义函数clean_corpus()清洗文本。将新对象命名为tm_clean。 - 再次查看新对象
tm_clean的第 1 篇文档,比较应用clean_corpus()前后的变化。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# clean_corpus(), tm_define are pre-defined
clean_corpus
tm_define
# Create a VectorSource
tm_vector <- ___
# Apply VCorpus
tm_corpus <- ___
# Examine the first document's contents
___(___[[___]])
# Clean the text
tm_clean <- ___
# Reexamine the contents of the first doc
___