主要内容

bagOfNgrams

Bag-of-n-grams模型

描述

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

bagOfNgrams不能将文本分割成单词。要创建令牌化文档数组,请参见tokenizedDocument

创建

描述

= bagOfNgrams创建一个空的n-grams包模型。

例子

= bagOfNgrams (文档创建一个bag-of-n-grams模型,并计算其中的双格拉姆(单词对)文档

例子

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

例子

= bagOfNgrams (uniqueNgrams计数中的n-g创建n-g包模型uniqueNgrams相应的频率计入计数.如果uniqueNgrams包含< >失踪值,然后输入相应的值计数将被忽略。

输入参数

全部展开

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

唯一的n-gram列表,指定为NumNgrams——- - - - - -maxN字符向量的字符串数组或单元格数组,其中NumNgramsn-g的个数是唯一的吗maxN是最大n元的长度。

的价值uniqueNgrams (i, j)jthe word of the语法。如果单词的数量第n个gram小于maxN,然后是th排uniqueNgrams是空的。

如果uniqueNgrams包含< >失踪,则该函数将忽略中相应的值计数

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

例子:[" " "”;“一个”“例子”;“例子”“”)

数据类型:字符串|细胞

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

计数有多少列uniqueNgrams也行。

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

属性

全部展开

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

模型中唯一的n-gram,指定为字符串数组。Ngrams (i, j)jthe word of the语法。如果的列数Ngrams大于n-gram中的单词数,则其余条目为空。

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

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

数据类型:字符串

n-g的数目,指定为非负整数。

看到的文档数,指定为非负整数。

对象的功能

编码 将文档编码为单词或n元计数矩阵
tfidf 术语频率-反文档频率(tf-idf)矩阵
topkngrams 最常见的字格
addDocument 将文档添加到bag-of-words或bag-of-n-grams模型
removeDocument 从单词袋或n-gram袋模型中删除文档
removeEmptyDocuments 从标记化的文档数组、单词包模型或n-gram包模型中删除空文档
removeNgrams 从包-n-grams模型中删除n-g
removeInfrequentNgrams 从包-n-grams模型中删除不常见的n-g
加入 组合多个bag-of-words或bag-of-n-grams模型
wordcloud 从文本、词袋模型、词袋-n-grams模型或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-grams模型。

袋= bagOfNgrams(文档)
单词:[" fairrest " "creatures" "desire"…Ngrams: [8799x2 string] ngramlength: 2 NumNgrams: 8799 NumDocuments: 154

使用单词云可视化模型。

图wordcloud(袋);

图中包含一个wordcloud类型的对象。

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

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

创建一个包-n-grams模型。要计数长度为2和3的n-g(双格和三元组),请指定“NgramLengths”作为向量3 [2]

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

查看长度为2的10个最常见的n-g(双格)。

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

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

topkngrams(包10“NGramLengths”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-g和矩阵计数,其中包含n-gram频率计数。

负载sonnetsBigramCounts.mat

查看中的前几个n-guniqueNgrams

uniqueNgrams (1:10,:)
ans =10 x2字符串"最美丽的生物" "渴望" "欲望" "增加" "因此" "因此" "美丽的" "美丽的" "玫瑰" "玫瑰" "可能" "可能" "永远" "永远" "死" "成熟"

创建包-n-grams模型。

袋= bagOfNgrams (uniqueNgrams计数)
单词:[" fairrest " "creatures" "desire"…Ngrams: [8799x2 string] ngramlength: 2 NumNgrams: 8799 NumDocuments: 154
介绍了R2018a