主要内容

bagOfNgrams

Bag-of-n-grams模型

描述

n-gram的bag模型记录每个n-gram出现在集合的每个文档中的次数。n-gram是集合n连续的字。

bagOfNgrams不会将文本分割成单词。要创建标记文档的数组,请参见令人畏缩的鳕文

创建

描述

= bagOfNgrams创建一个空袋式模型。

例子

= bagOfNgrams (文档创建一个n-gram的bag模型,并计算其中的双字组(单词对)文档

例子

= bagOfNgrams (___“NgramLengths”,长度使用先前的任何语法计数指定长度的n-grams。

例子

= bagOfNgrams (uniqueNgrams计数使用中的n-g创建一个n-g包模型uniqueNgrams和相应的频率计数计数。如果uniqueNgrams包含<缺失>值,然后对应的值计数将被忽略。

输入参数

全部展开

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

唯一的n-gram列表,指定为numngrams.-经过-maxN字符串数组或字符向量的单元数组,其中numngrams.是唯一的n字格数,和maxN是最大n元的长度。

的价值无唯一名称(I,J)是个j这句话语法。如果字数在第n个gram小于maxN,那么剩下的条目th排uniqueNgrams是空的。

如果uniqueNgrams包含<缺失>,则函数忽略中对应的值计数

每个n-gram必须至少有一个单词。

例子:[“一个” ””;“一个例子”;“例子” ””]

数据类型:字符串|细胞

与行相对应的n-gram的频率计数uniqueNgrams,指定为一个非负整数矩阵。的值计数(i,j)对应于n元的次数uniqueNgrams (j,:)出现在文档。

计数必须有多少列uniqueNgrams也行。

n-gram的长度,指定为正整数或正整数的向量。

属性

全部展开

每个文档的N-gram计数,指定为一个稀疏矩阵。

模型中唯一的n-gram,指定为字符串数组。Ngrams (i, j)是个j这句话语法。如果列数为ngrams.大于n-gram中的单词数,则其余项为空。

n-gram的长度,指定为正整数或正整数的向量。

模型中的唯一单词,指定为字符串向量。

数据类型:字符串

所见的n克数,指定为非负整数。

被指定为非负整数的文档数量。

对象的功能

编码 将文档编码为单词或n-gram计数的矩阵
tfidf 频率逆文档频率(tf-idf)矩阵
Topkngrams. 最常见的字格
adddocument. 将文档添加到单词袋或n-g袋模型中
删除录制 从单词袋或n-g袋模型中删除文档
removeEmptyDocuments 从令牌化文档阵列中删除空文档,文字袋模型或N-r克模型
removeNgrams 从n字袋模型中删除n字格
removeinfrequentngrams. 从n字袋模型中删除不常见的n字格
加入 组合多个单词袋或n-g袋模型
wordcloud 从文本、词袋模型、n字袋模型或LDA模型创建单词云图

例子

全部折叠

加载示例数据。该文件sonnetsPreprocessed.txt包含莎翁十四行诗的预处理版本。该文件每行包含一首十四行诗,单词之间用空格隔开。从sonnetsPreprocessed.txt,以换行符将文本拆分为文档,然后对文档进行标记。

文件名=“sonnetsPreprocessed.txt”;str = extractFileText(文件名);textData =分裂(str,换行符);文件= tokenizedDocument (textData);文档(1:10)
ans = 10x1 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 68 tokens: let winters ragged hand deface thee thy summer ere thou distilld make sweet vial treasure thou place beautys treasure ere selfkilld forbidden usury happies pay willing loan thats thy self breed another thee ten times happier ten ten times thy self happier thou art ten thine ten times refigurd thee death thou shouldst depart leaving thee living posterity selfwilld thou art fair deaths conquest make worms thine heir 64 tokens: lo orient gracious light lifts up burning head eye doth homage newappearing sight serving looks sacred majesty climbd steepup heavenly hill resembling strong youth middle age yet mortal looks adore beauty still attending golden pilgrimage highmost pitch weary car like feeble age reeleth day eyes fore duteous converted low tract look another way thou thyself outgoing thy noon unlookd diest unless thou get son 70 tokens: music hear why hearst thou music sadly sweets sweets war joy delights joy why lovst thou thou receivst gladly else receivst pleasure thine annoy true concord welltuned sounds unions married offend thine ear sweetly chide thee confounds singleness parts thou shouldst bear mark string sweet husband another strikes mutual ordering resembling sire child happy mother pleasing note sing whose speechless song many seeming sings thee thou single wilt prove none 70 tokens: fear wet widows eye thou consumst thy self single life ah thou issueless shalt hap die world wail thee like makeless wife world thy widow still weep thou form thee hast left behind every private widow well keep childrens eyes husbands shape mind look unthrift world doth spend shifts place still world enjoys beautys waste hath world end kept unused user destroys love toward others bosom sits murdrous shame commits 69 tokens: shame deny thou bearst love thy self art unprovident grant thou wilt thou art belovd many thou none lovst evident thou art possessd murderous hate gainst thy self thou stickst conspire seeking beauteous roof ruinate repair thy chief desire o change thy thought change mind shall hate fairer lodgd gentle love thy presence gracious kind thyself least kindhearted prove make thee another self love beauty still live thine thee

创建一个n克袋模型。

袋= bagOfNgrams(文档)
[1x3092 string] Ngrams: [8799x2 string] ngramlength: 2 NumNgrams: 8799 NumDocuments: 154

使用单词云将模型可视化。

图WordCloud(包);

图包含一个类型为词云的对象。

加载示例数据。该文件sonnetsPreprocessed.txt包含莎翁十四行诗的预处理版本。该文件每行包含一首十四行诗,单词之间用空格隔开。从sonnetsPreprocessed.txt,以换行符将文本拆分为文档,然后对文档进行标记。

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

创建一个n克袋模型。计算长度为2和3(Bigrams和Trigrams),指定“NgramLengths”成为向量3 [2]

袋= bagOfNgrams(文档,“NgramLengths”3 [2])
[1×3092 string] Ngrams: [18022×3 string] ngramnames: [2 3] NumNgrams: 18022 NumDocuments: 154

查看10个最常见的长度2(Bigrams)。

topkngrams(包10“NGramLengths”, 2)
ans =10×3表Ngram计数NgramLength  _______________________ _____ ___________ " 你”“艺术”“我“34 2”“眼睛”““15 2”你”“自我”““14 2”你”“你”“我“13 2”“自己的”“13 2”你”“香”““12 2”你”“爱”““11 2”难道你”“你”““10 2”“要”““10 2”爱情”“你”““9 2

查看10个最常见的长度为3的n-gram(三元图)。

topkngrams(包10“NGramLengths”3,3)
ans =10×3表Ngram计数NgramLength  ____________________________ _____ ___________ " 你”“香”“自我”4 3“为什么”“难道”“你“4 3”“自我”“你“3 3”你”“你”“自我”3 3“我”“眼睛”“心”3 3“你”“要”“找到”3 3“公平”“善意”“真实”3 3”你”“艺术”“公平”2 3“爱”你的“自我”2 3“你”“自我”“你”2 3

使用唯一的n-g字符串数组和计数矩阵创建n-g包模型。

从其中加载示例n-gram和计数sonnetsbigramcounts.mat。这个文件包含一个字符串数组uniqueNgrams,它包含唯一的n-gram,以及矩阵计数,它包含n gram的频率计数。

负载sonnetsbigramcounts.mat

查看前几个n-gramsuniqueNgrams

uniqueNgrams (1:10,:)
ans =10x2字符串“最公平”“生物”“生物”“欲望”“欲望”“增加”“增加”“从而”从而“的”“美容”“美容”“玫瑰”“玫瑰”“可能”“”永远不会“”“永远不会”““”死“”死“”涟漪“

创建n克袋模型。

袋= bagOfNgrams (uniqueNgrams计数)
[1x3092 string] Ngrams: [8799x2 string] ngramlength: 2 NumNgrams: 8799 NumDocuments: 154
介绍了R2018a