主要内容

removeWords

从文档或词袋模型中删除选定的词

描述

例子

newDocuments= removeWords (文档单词从中删除指定的单词文档.缺省情况下,该函数区分大小写。

例子

newBag= removeWords (单词从单词袋模型中移除指定的单词.缺省情况下,该函数区分大小写。

newDocuments= removeWords (___“IgnoreCase”,真的)使用任何前面的语法删除忽略大小写的单词。

例子

newDocuments= removeWords (文档idx通过指定数字或逻辑索引来删除单词idx的单词文档。词汇表.这个语法与newDocuments = removeWords(文档、documents.Vocabulary (idx))

例子

newBag= removeWords (idx通过指定数字或逻辑索引来删除单词idx的单词袋里。词汇表.这个语法与newBag = removeWords(袋、bag.Vocabulary (idx))

例子

全部折叠

从文档数组中删除单词,方法是输入一个单词字符串数组removeWords

创建一个标记化文档数组。

文件= tokenizedDocument ([一个短句的例子第二个短句]);

去掉“短”和“第二”这两个词。

话说= [“短”“第二”];newDocuments = removeWords(文件,字)
newDocuments = 2x1 tokenizedDocument: 5 tokens:一个句子的例子2 tokens:一个句子

若要使用文档的语言细节删除默认的停止词列表,请使用removeStopWords

要删除自定义停止词列表,请使用removeWords函数。的返回的停止词列表stopWords作为一个起点。

加载示例数据。该文件sonnetsPreprocessed.txt包含了经过预处理的莎士比亚十四行诗。该文件每行包含一首十四行诗,单词之间用空格分隔。将文本从sonnetsPreprocessed.txt,将文本以换行符分割为文档,然后标记文档。

文件名=“sonnetsPreprocessed.txt”;str = extractFileText(文件名);textData =分裂(str,换行符);文件= tokenizedDocument (textData);

查看前几个文档。

文档(1:5)
ans = 5x1 tokenizedDocument: 70 token:美丽的生物欲望增加从而美丽玫瑰可能永远不会死成熟时间流逝的记忆交给娇嫩的后嗣收缩你的明亮的眼睛feedst你灯火焰selfsubstantial燃料使饥荒丰富是你自我你的敌人你甜蜜的自我残酷的艺术世界新鲜点缀春天华丽使者你自己的花蕾埋葬你知足的温柔的粗鲁的人浪费了吝啬的怜悯,否则贪婪的吃了世界应有的坟墓,你的71个记号:四十冬天围攻你的眉毛挖深沟你的美容领域你年轻人骄傲制服盯着tatterd杂草小值得问你的美丽谎言珍惜你说你自己的深凹的眼睛的日夜alleating羞愧浪费的赞美赞美deservd你的美丽你能够回答我公平的孩子应当和计数使老借口证明美丽继承你的新,你的旧,你的血液温暖,你的感觉寒冷,看你的玻璃告诉脸你查看时间脸形成另一个新鲜的修复17你欺骗世博会unbless母亲的子宫uneard不屑走旁人走过耕作你饲养喜欢墓selflove停止后人你是你母亲的玻璃你电话回可爱的4月最佳窗口你年龄要尽管皱纹你的黄金时间活着,记住,独自死去,你的形象死去,你的象征。unthrifty可爱为什么你花在你的自我你美丽遗产性质遗赠给了什么难道借弗兰克借自由美丽的吝啬鬼为什么你虐待你慷慨的慷慨给无益的高利贷者为什么你伟大的金额总和还能实时路况你自我孤独你自我你甜蜜的自我欺骗内急你消失了你能把你那未用过的美留在坟墓里吗?小时温柔的工作框架可爱的眼睛凝视每个难道住打暴君一样不公平很难道excel neverresting时间导致夏天可怕的冬天混淆sap检查霜精力充沛的叶子很美丽了oersnowed赤裸每个夏天蒸馏液体离开囚禁囚犯墙玻璃美容效果美丽失去也没有鲜花和纪念 distilld though winter meet leese show substance still lives sweet

创建一个以输出开始的停止字列表stopWords函数。

customStopWords = [stopWords“你”“你”“你”“难道”“难道”];

从文档中删除自定义停止词并查看前几个文档。

= removeWords文档(文档、customStopWords);文档(1:5)
ans = 5x1 tokenizedDocument: 62 token:美丽的生物欲望增加从而美丽玫瑰可能永远不会死成熟时间流逝的记忆交给娇嫩的后嗣收缩你的明亮的眼睛feedst灯火焰selfsubstantial燃料使饥荒丰富在于自我敌人甜自我残酷的艺术世界新鲜点缀春天华丽使者你自己的花蕾埋葬内容投标乡下人makst浪费小气可怜的世界,否则贪婪的吃世界应有的61代币:四十冬天围攻额头深入挖掘战壕美容领域青年骄傲制服盯着tatterd杂草小值得问美丽谎言宝藏的日夜说你自己的深凹的眼睛alleating羞愧浪费的赞美赞美deservd美丽能够回答我公平的孩子应当和计数使旧借口证明美继承你的新art old blood warm feelst cold 52 tokens: look glass tell face viewest time face form another whose fresh repair renewest beguile world unbless mother fair whose uneard womb disdains tillage husbandry fond tomb selflove stop posterity art mothers glass calls back lovely april prime windows thine age shalt despite wrinkles golden time live rememberd die single thine image dies 52 tokens: unthrifty loveliness why spend upon self beautys legacy natures bequest gives nothing lend frank lends free beauteous niggard why abuse bounteous largess give profitless usurer why great sum sums yet canst live traffic self alone self sweet self deceive nature calls gone acceptable audit canst leave unused beauty tombed lives th executor 59 tokens: hours gentle work frame lovely gaze every eye dwell play tyrants same unfair fairly excel neverresting time leads summer hideous winter confounds sap checked frost lusty leaves quite gone beauty oersnowed bareness every summers distillation left liquid prisoner pent walls glass beautys effect beauty bereft nor nor remembrance flowers distilld though winter meet leese show substance still lives sweet

通过输入带有数字索引的向量来从文档中删除单词removeWords

创建一个标记化文档数组。

文件= tokenizedDocument ([“我爱MATLAB”“我爱MathWorks”])
documents = 2x1 tokenizedDocument: 3 token:我爱MATLAB 3 token:我爱MathWorks

查看文档

文档。词汇表
ans =1 x4字符串"I" "love" "MATLAB" "MathWorks"

通过指定数字索引,从文档中删除词汇表的第一个和第三个单词3 [1]

Idx = [1 3];idx newDocuments = removeWords(文档)
newDocuments = 2x1 tokenizedDocument: 1 token: love 2 token: love MathWorks

或者,您可以指定逻辑索引。

Idx =逻辑([1 0 1 0]);idx newDocuments = removeWords(文档)
newDocuments = 2x1 tokenizedDocument: 1 token: love 2 token: love MathWorks

通过输入停止词列表来从词袋模型中删除停止词removeWords.停止词是像“a”、“the”和“in”这样的词,这些词在分析之前通常会从文本中删除。

文件= tokenizedDocument ([一个短句的例子第二个短句]);袋= bagOfWords(文件);stopWords newBag = removeWords(袋)
newBag = bagOfWords with properties: Counts: [2x4 double] Vocabulary: ["example" "short" "sentence" "second"] NumWords: 4 NumDocuments: 2

通过输入一个带有数字索引的向量来从单词包模型中删除单词removeWords

创建一个标记化文档数组。

文件= tokenizedDocument ([“我爱MATLAB”“我爱MathWorks”]);袋= bagOfWords(文档)
bag = bagOfWords with properties: Counts: [2x4 double]词汇:["I" "love" "MATLAB" "MathWorks"

查看

袋里。词汇表
ans =1 x4字符串"I" "love" "MATLAB" "MathWorks"

通过指定数字索引,从单词袋模型中删除词汇表的第一个和第三个单词3 [1]

Idx = [1 3];idx newBag = removeWords(袋)
newBag = bagOfWords with properties: Counts: [2x2 double

或者,您可以指定逻辑索引。

Idx =逻辑([1 0 1 0]);idx newBag = removeWords(袋)
newBag = bagOfWords with properties: Counts: [2x2 double

输入参数

全部折叠

输入文档,指定为tokenizedDocument数组中。

输入词袋模型,指定为bagOfWords对象。

要删除的字,指定为字符串向量、字符向量或字符向量的单元格数组。如果您指定单词作为字符向量,函数将其视为单个单词。

数据类型:字符串|字符|细胞

要删除的单词的索引,指定为数字索引向量或逻辑索引向量。的指标idx对应于单词的位置词汇表属性的输入文档或词袋模型。

例子:[1 5 10]

输出参数

全部折叠

输出文档,作为tokenizedDocument数组中。

输出词袋模型,返回为bagOfWords对象。

介绍了R2017b