主要内容

addDocument

将文档添加到bag-of-words或bag-of-n-grams模型

描述

例子

newBag= addDocument (文档增加了文档变成单词袋或n-gram袋模型

例子

全部折叠

从标记化文档数组创建单词袋模型。

文件= tokenizedDocument ([一个短句的例子第二个短句]);袋= bagOfWords(文档)
bag = bagOfWords with properties: Counts: [2x7 double] Vocabulary: [1x7 string] NumWords: 7 NumDocuments: 2

创建另一个标记化文档数组,并将其添加到相同的单词袋模型中。

文件= tokenizedDocument ([第三个短句的例子另一个“短句”]);newBag = addDocument(袋、文档)
newBag = bagOfWords with properties: Counts: [4x9 double] Vocabulary: [1x9 string] NumWords: 9 NumDocuments: 4

如果您的文本数据包含在一个文件夹中的多个文件中,那么您可以使用文件数据存储将文本数据导入MATLAB。

为示例十四行诗文本文件创建文件数据存储。示例十四行诗有文件名"exampleSonnetN.txt”,N是十四行诗的编号。指定要读取的函数extractFileText

readFcn = @extractFileText;fds = fileDatastore (“exampleSonnet * . txt”“ReadFcn”readFcn)
/tp706790c2/textanalytics-ex73762432/exampleSonnet1.txt';“…/ tp706790c2 / textanalytics-ex73762432 / exampleSonnet2.txt”;“…/ tp706790c2 / textanalytics-ex73762432 / exampleSonnet3.txt”……{'/tmp/Bdoc21a_1606923_11597/tp706790c2/textanalytics-ex73762432'} UniformRead: 0 ReadMode: 'file' BlockSize: Inf PreviewFcn: @extractFileText SupportedOutputFormats: 万博1manbetx[1x16 string] ReadFcn: @extractFileText AlternateFileSystemRoots: {}

创建一个空的单词袋模型。

袋= bagOfWords
bag = bagOfWords with properties: Counts: [] Vocabulary: [1x0 string] NumWords: 0 NumDocuments: 0

循环遍历数据存储中的文件并读取每个文件。标记每个文件中的文本并将文档添加到

Hasdata (fds) STR = read(fds);文档= tokenizedDocument (str);袋= addDocument(袋、文档);结束

查看更新的单词袋模型。

bag = bagOfWords with properties: Counts: [4x276 double] Vocabulary: [1x276 string] NumWords: 276 NumDocuments: 4

输入参数

全部折叠

输入bag-of-words或bag-of-n-grams模型,指定为bagOfWords对象或一个bagOfNgrams对象。

输入文档,指定为tokenizedDocument数组、字的字符串数组或字符向量的单元格数组。如果文档不是一个tokenizedDocument数组,则它必须是表示单个文档的行向量,其中每个元素都是一个单词。要指定多个文档,请使用tokenizedDocument数组中。

输出参数

全部折叠

输出模型,作为bagOfWords对象或一个bagOfNgrams对象。的类型newBag是同类型的吗

介绍了R2017b