文档帮助中心文档
将文档添加到bag-of-words或bag-of-n-grams模型
newBag = addDocument(袋、文档)
例子
newBag= addDocument (袋,文档)增加了文档变成单词袋或n-gram袋模型袋.
newBag= addDocument (袋,文档)
newBag
袋
文档
全部折叠
从标记化文档数组创建单词袋模型。
文件= 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.
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: 金宝app[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
bagOfWords
bagOfNgrams
输入bag-of-words或bag-of-n-grams模型,指定为bagOfWords对象或一个bagOfNgrams对象。
tokenizedDocument
输入文档,指定为tokenizedDocument数组、字的字符串数组或字符向量的单元格数组。如果文档不是一个tokenizedDocument数组,则它必须是表示单个文档的行向量,其中每个元素都是一个单词。要指定多个文档,请使用tokenizedDocument数组中。
输出模型,作为bagOfWords对象或一个bagOfNgrams对象。的类型newBag是同类型的吗袋.
bagOfNgrams|bagOfWords|removeDocument|removeEmptyDocuments|tokenizedDocument
removeDocument
removeEmptyDocuments
您有这个示例的修改版本。您想打开这个示例与您的编辑吗?
你点击一个链接对应于这个MATLAB命令:
通过在MATLAB命令窗口中输入命令来运行命令。Web浏览器不支持MATLAB命令。金宝app
选择一个网站,在那里获得翻译的内容,并看到当地的活动和优惠。根据您的位置,我们建议您选择:.
你也可以从以下列表中选择一个网站:
选择中国网站(中文或英文)以获得最佳网站性能。其他MathWorks国家站点没有针对您所在位置的访问进行优化。
与当地办事处联系