- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想分析一个大的(n=500,000)文档语料库。我使用 quanteda
期望 will be faster比 tm
中的 tm_map()
更好。我想逐步进行,而不是使用 dfm()
的自动化方式。我这样做是有原因的:在一种情况下,我不想在删除停用词之前进行标记,因为这会导致许多无用的二元组,在另一种情况下,我必须使用特定于语言的程序预处理文本。
我希望实现这个序列:
1)删除标点符号和数字
2)删除停用词(即在标记化之前,以避免无用的标记)
3)使用一元组和二元组进行标记
4)创建dfm
我的尝试:
> library(quanteda)
> packageVersion("quanteda")
[1] ‘0.9.8’
> text <- ie2010Corpus$documents$texts
> text.corpus <- quanteda:::corpus(text, docnames=rownames(ie2010Corpus$documents))
> class(text.corpus)
[1] "corpus" "list"
> stopw <- c("a","the", "all", "some")
> TextNoStop <- removeFeatures(text.corpus, features = stopw)
# Error in UseMethod("selectFeatures") :
# no applicable method for 'selectFeatures' applied to an object of class "c('corpus', 'list')"
# This is how I would theoretically continue:
> token <- tokenize(TextNoStop, removePunct=TRUE, removeNumbers=TRUE)
> token2 <- ngrams(token,c(1,2))
奖励问题如何删除 quanteda
中的稀疏标记? (即相当于 tm
中的 removeSparseTerms()
。
更新根据@Ken的回答,以下是逐步使用 quanteda
进行操作的代码:
library(quanteda)
packageVersion("quanteda")
[1] ‘0.9.8’
1) 删除自定义标点符号和数字。例如。注意ie2010语料库中的“\n”
text.corpus <- ie2010Corpus
texts(text.corpus)[1] # Use texts() to extrapolate text
# 2010_BUDGET_01_Brian_Lenihan_FF
# "When I presented the supplementary budget to this House last April, I said we
# could work our way through this period of severe economic distress. Today, I
# can report that notwithstanding the difficulties of the past eight months, we
# are now on the road to economic recovery.\nIt is
texts(text.corpus)[1] <- gsub("\\s"," ",text.corpus[1]) # remove all spaces (incl \n, \t, \r...)
texts(text.corpus)[1]
2010_BUDGET_01_Brian_Lenihan_FF
# "When I presented the supplementary budget to this House last April, I said we
# could work our way through this period of severe economic distress. Today, I
# can report that notwithstanding the difficulties of the past eight months, we
# are now on the road to economic recovery. It is of e
关于人们可能更喜欢预处理的原因的进一步说明。我目前的语料库是意大利语,这种语言的冠词与带有撇号的单词相关。因此,直接的 dfm() 可能会导致不精确的标记化。例如:
broken.tokens <- dfm(corpus(c("L'abile presidente Renzi. Un'abile mossa di Berlusconi"), removePunct=TRUE))
将为同一个单词生成两个单独的标记(“un'abile”和“l'abile”),因此需要在此处使用 gsub()
执行额外步骤。
2) 在 quanteda
中,不可能在标记化之前直接删除文本中的停用词。在我之前的示例中,必须删除“l”和“un”,以免产生误导性的二元组。这可以在 tm
中使用 tm_map(..., removeWords)
进行处理。
3) 代币化
token <- tokenize(text.corpus[1], removePunct=TRUE, removeNumbers=TRUE, ngrams = 1:2)
4)创建 dfm:
dfm <- dfm(token)
5)删除稀疏特征
dfm <- trim(dfm, minCount = 5)
最佳答案
我们设计了dfm()
它不是一个“黑匣子”,而更像是一把瑞士军刀,它结合了典型用户在将文本转换为文档和功能矩阵时想要应用的许多选项。然而,如果您希望进行更精细的控制,所有这些选项也可以通过较低级别的处理命令来使用。
然而,quanteda 的设计原则之一是文本只能通过标记化过程成为“特征”。如果您希望排除一组标记化功能,则必须首先对文本进行标记化,否则无法排除它们。与 R 的其他文本包(例如 tm)不同,这些步骤是从语料库“下游”应用的,因此语料库仍然是一组未处理的文本,将对其应用操作(但本身不会是一组经过转换的文本)。这样做的目的是为了保持通用性,同时也是为了提高文本分析的可重复性和透明度。
回答您的问题:
但是,您可以使用texts(myCorpus) <-
来覆盖我们鼓励的行为。函数,其中分配给文本的内容将覆盖现有文本。因此,您可以使用正则表达式来删除标点符号和数字 - 例如 stringi 命令并使用标点符号和数字的 Unicode 类来识别模式。
我建议您在删除停用词之前进行分词。停止“单词”是标记,因此在对文本进行标记之前无法从文本中删除它们。甚至应用正则表达式来替换""
涉及在正则表达式中指定某种形式的单词边界 - 同样,这就是标记化。
标记为一元组和二元组:
token (myCorpus, ngrams = 1:2)
要创建 dfm,只需调用 dfm(myTokens)
。 (您也可以在此阶段对 ngram 应用步骤 3。
奖励 1:n=2 搭配产生与二元组相同的列表,只是格式不同。你还有别的打算吗? (也许是单独的问题?)
奖励 2:参见 dfm_trim(x, sparsity = )
。 removeSparseTerms()
对于大多数人来说,选择是相当令人困惑的,但这包括来自tm的移民。请参阅this post以获得完整的解释。
顺便说一句:使用texts()
而不是ie2010Corpus$documents$texts
-- 我们很快将重写语料库的对象结构,因此当存在提取器函数时,您不应该以这种方式访问其内部。 (此外,这一步是不必要的 - 这里您只是重新创建了语料库。)
2018-01 更新:
语料库对象的新名称是 data_corpus_irishbudget2010
,搭配评分函数为textstat_collocations()
.
关于r - 使用 Quanteda 一步一步创建 dfm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38931507/
我有下一个问题。其他团队更改(删除)了组件的属性,现在某些表单打开时显示消息“读取表单属性时出错....不存在”。如何检查 Delphi 项目中 dfm 文件的正确性? 最佳答案 您需要的不仅仅是 d
我的项目中有一个 xyz.pas 文件引用。但那个文件不在我身边。我有该 xyz.pas 文件的 xyz.dcu 和 xyz.obj 文件。当我尝试编译该项目时,出现错误“未找到 xyz.dcu”。所
不标准的属性顺序会导致任何问题吗? 例如,IDE 在 DFM 文件中存储一个 TButton 组件,如下所示: object Button1: TButton Left = 288
德尔福 XE2 我有以下自定义组件: TNaharWebDBEdit = class(TUniDBEdit) private FThemeColor : TNaharPalleteCo
我在自定义组件上有一个属性,我不想将其保存在 DFM 文件中。我已经覆盖了 DefineProperties 方法以不提供 ReadData 和 WriteData 过程,期望它不会保存其值,但它仍然
有没有办法跳过从 dfm 创建的组件? 我有一个有时不想创建的可视化组件。当然,我可以手动创建组件,但是由于组件有很多项目,这将需要大量工作。 最佳答案 实际上有一种方法可以做到这一点。这是一个跳过特
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我将所有 ADO 引擎卸载到一个单独的数据模块中,因此单个模块可以被多个应用程序引用。我的所有应用程序基本上只需要两种 worker 方法来访问数据: AdoQuery 以 TADODataSet 的
Delphi 应用程序是否可以接收包含对象、其属性和事件分配的 dfm 文件,并加载所有这些信息,就像它们如何处理用它编译的内部 dfm 一样? 我们怎样才能做到这一点?有直接的办法吗? 注意:应用程
我们为两个略有不同版本的机器生产两个版本的软件。两台机器上使用的硬件完全不同,因此我们维护两个项目:Project1 和 Project2。一些代码(.pas 和 .dfm,视情况而定)在两个项目之间
当您右键单击 Delphi 表单时,您会看到弹出的上下文菜单。底部选项是文本DFM。该选项可以勾选也可以不勾选。选中或取消选中此选项意味着什么? 最佳答案 此选项用于选择保存表单的格式(二进制或文本格
我正在开发一个包含许多小型自定义对话框的应用程序。 这些对话框例如提供选择、显示图表或提供额外的界面。大多数情况下,它们只需要很少的标记代码并且只有很少的子元素。 目前我正在使用 embarcader
我有一个包含 2 个文本字段的数据框:评论和主要帖子 基本上是这样的结构 id comment post_text
我创建了一个包 - 一个自定义组件。包中包含 *.pas 和 *.dfm。我编译包并将 *.DCU 路径添加到库。我从 Tool Palette 中删除了我的组件来测试应用程序表单,Delphi 提示
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Is there a Delphi option to ‘lock’ the design (layout) of
Delphi 表单设计器非常好,但我们需要直接从源代码开始工作。是否有工具或脚本可以获取一批 DFM 文件并将它们转换为 Delphi 源代码? 最佳答案 您可以使用 GExperts 中的 Comp
代码编辑器中的这个短语{$R *.dfm}是什么? 我尝试过这个: //{$R *.dfm} 最佳答案 你会想把它保留在那里。 $R 指令将资源文件链接到 EXE 中。 *.dfm 表示“与此 .pa
当在 Delphi 中打开包含继承的 TImageList 的表单时(本例为 2010,但它可能适用于其他版本),IDE 总是(我找不到任何韵律或节奏)添加图像数据再次回到继承的形式。这会增加可执行文
我们的 Delphi 7 开发团队以文本模式存储表单文件 (dfm),并使用源代码控制系统 (Subversion) 来跟踪更改。 有时,我们注意到表单文件在修订之间返回为二进制格式,导致 Torto
使用 DXE2,我编写了一个表单生成器,可以创建 .pas 和 .dfm 文件。我正在开发一个例程,该例程允许我修改 dfm 中的不同属性,例如 Font.Height 和 TabOrder。 当我在
我是一名优秀的程序员,十分优秀!