使用 tm 包在 R 中打印语料库一个元素的第一行 [英] Print first line of one element of Corpus in R using tm package

查看:34
本文介绍了使用 tm 包在 R 中打印语料库一个元素的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 tm 包在 R 中打印语料库的小样本或第一行?我有一个非常大的语料库(> 1 GB)并且正在做一些文本清理.我想在应用清洁程序时进行测试.仅打印语料库的第一行或前几行将是理想的.

How do you print a small sample, or first line, of a corpus in R using the tm package? I have a very large corpus ( > 1 GB) and am doing some text cleaning. I would like to test as I apply cleaning procedures. Printing just the first line, or first few lines of a corpus would be ideal.

# Load Libraries
library(tm)

# Read in Corpus
corp <- SimpleCorpus( DirSource( 
    "C:/TextDocument"))

# Remove puncuation
corp <- removePunctuation(corp,
                      preserve_intra_word_contractions = TRUE,
                      preserve_intra_word_dashes = TRUE)

我尝试了几种访问语料库的方法:

I have tried accessing the corpus several ways:

# Print first line of first element of corpus
corp[[1]][[1]] 

# Print first line using 'content' element of corpus
corp[[1]]$content[[1]]

这两种情况都会导致很长的运行时间而没有所需的输出.

Both of these result in very long run times without the desired output.

tm 包中的原始语料可用于示例目的.

The crude corpus in the tm package can be used for example purposes.

data("crude")

推荐答案

strwrap 很好地完成了这项工作,因为它打印您的段落通过在单词边界处换行而格式化.(请参阅?strwrap.)然后您可以使用head 函数查看前6 行.

strwrap does this job nicely since it prints your paragraphs formatted by breaking lines at word boundaries. (See ?strwrap.) Then you can use the head function to see the first 6 lines.

 head(strwrap(corp))

这篇关于使用 tm 包在 R 中打印语料库一个元素的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆