主要内容

wordEncoding

字编码模型,以映射字到索引和返回

描述

单词编码将词汇表中的单词映射到数字索引。

要将文档编码为单词计数或n-gram计数的矩阵,请使用编码

创建

描述

例子

内附= WordEncoding(文档从单词中创建一个编码的单词文档

例子

内附= WordEncoding(单词从单词数组创建一个单词编码。

例子

内附= WordEncoding(文档名称,价值使用一个或多个名称值对参数指定其他选项。例如,'命令','频率'将较低的指数分配给更频繁的单词。

输入参数

展开全部

输入文档,指定为令人畏缩的鳕文数组中。

输入单词,指定为字符串向量,字符向量或字符向量的单元数组。如果您指定单词作为字符向量,该函数将参数视为单个单词。

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

名称值对参数

指定可选的逗号分隔对名称,价值论点。的名字参数名和价值是相应的价值。的名字必须出现在引号内。您可以以任何顺序指定多个名称和值对参数name1,value1,...,namen,valuen

例子:'命令','频率'按文档中的总频率按降序对索引进行排序。

指定指定指定为逗号分隔对'命令'其中之一是:

  • 首次出现的- 以文档中发生的顺序为单词分配索引。

  • “频率”- 以降序排列在文档中的总频率排序的单词中指定指数。

如果'命令'“频率”并且多个单词有相同的频率,那么函数就不会以任何特定的顺序分配索引。

以对编码的最大单词数,指定为正整数或INF..方法首先对索引进行排序'命令'选项然后编码顶部maxnumwords.单词。如果maxnumwords.INF.,然后函数对输入文档中的所有单词进行编码。

属性

展开全部

模型中的单词数,指定为非负整数。

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

数据类型:字符串

对象的功能

ind2word 将编码索引映射到单词
Word2ind. 映射字到编码索引
IsVocabularyWord. 测试单词是否是单词嵌入或编码的成员

例子

全部收缩

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

文件名=“sonnetspreprocessed.txt”;str = extractFileText(文件名);textData =分裂(str,换行符);文件= tokenizedDocument (textData);文档(1:10)
ans = 10x1令叫鳕文件:70令牌:最公平的生物欲望增加,从而使得美女永远不会死于涟漪时间,温柔的继承人可能会忍受你的结束你的明亮眼睛饲料你的灯火般的丰富谎言谎言你的自我残忍你艺术世界新鲜装饰秀丽春天你的萌芽胸部春天的伤害伤害怜悯世界其他恐惧世界坟墓坟墓:四十次冬天应该围攻你的年轻人骄傲的杂草鸟类小值得举行问你的美女谎言你的愚蠢的日子说,你的深深沉没的日子归零耻辱令人羞耻的赞美赞美赞美你的好感你你应该答复普通的孩子矿素数算起旧的借口证明美女继承你的新款制作的你的血腥感到寒冷的65令牌:看看你的玻璃告诉脸,你是另一个脸的脸H修复你更新你的令人沮丧的母亲公平的放弃母亲散发耕种你的畜牧坟墓自我停止后遗如塞后遗症你玻璃母亲杯子叫回电话的可爱的4月份你的Windows thine thine thine thine thine thine thine shative thy thy thy thy thy thy thy thy thy thy thy remedd die thine thy die thined图像Dies Dies Oree 71令牌:未持续的爱情为什么要在你的自我遗产遗产贝雷斯特没有给予Nothing Doth Lend Frank Leends免费美丽的黑鬼为什么Dost你滥用的浪费大型吉斯·吉尔斯·你好,为什么Dost你很大的概念自我孤独你是你的甜蜜自我欺骗自然叫做你已经消失了审计概念你离开了你的未使用的美容玉米的玉米生命的人寿人61令牌:小时温和工作框架可爱的凝视每只眼睛doth居住的暴君同样不公平相同的excel excel overlest the newlese lower夏天可怕的冬季混淆藏皮切检查了霜冻叶子相当黯淡的酸萨默夏天左液体囚犯墙玻璃美容效果美容局势和纪念花朵蒸馏出来虽然冬季遇见莱塞秀物质仍然生活甜蜜的68令牌:让冬天的夏天奥斯特·斯蒂利德制作甜蜜的瓶子宝贝,让你放置甜美的宝贝Ere Selfickilld禁止的高利风仍然付出愿意贷款,这是你的自我养殖另一个人十次十次十岁时你的自我快乐Thou art ten thy thy thy thy thy the ten the the the death你应该离开留下的生活后遗症selfilld thou art fair deals征服征服虫子征服了虫子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

创建一个字编码。

内附= wordEncoding(文档)
enc = wordencoding属性:numwords:3092词汇:[1x3092字符串]

要创建从嵌入的单词编码的单词,请输入嵌入词汇的单词wordEncoding用作单词列表。

加载预训练的单词嵌入。

emb = fastTextWordEmbedding;

提取词汇。

话说= emb.Vocabulary;

使用词汇创建一个单词编码。

内附= wordEncoding(字)
enc = wordencoding属性:numwords:999994词汇表:[1×999994字符串]

利用单词嵌入权值初始化深度学习网络中相应的单词嵌入层,使用Word2vec.提取层权重的功能并设置“重量”名称 - 值对WordembeddingLayer.函数。单词嵌入层需要单词向量的列,因此必须对输出进行转置Word2vec.函数。

尺寸= emb.dimension;numwords = numel(单词);tallay = wordembeddinglayer(维度,numword,...“重量”word2vec (emb)”)
Layer = WordembeddingLayer具有属性:名称:''''''''QuideParameters维数:300 NUM字:999994学习参数权重:[300×999994单个]显示所有属性

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

文件名=“sonnetspreprocessed.txt”;str = extractFileText(文件名);textData =分裂(str,换行符);文件= tokenizedDocument (textData);文档(1:10)
ans = 10x1令叫鳕文件:70令牌:最公平的生物欲望增加,从而使得美女永远不会死于涟漪时间,温柔的继承人可能会忍受你的结束你的明亮眼睛饲料你的灯火般的丰富谎言谎言你的自我残忍你艺术世界新鲜装饰秀丽春天你的萌芽胸部春天的伤害伤害怜悯世界其他恐惧世界坟墓坟墓:四十次冬天应该围攻你的年轻人骄傲的杂草鸟类小值得举行问你的美女谎言你的愚蠢的日子说,你的深深沉没的日子归零耻辱令人羞耻的赞美赞美赞美你的好感你你应该答复普通的孩子矿素数算起旧的借口证明美女继承你的新款制作的你的血腥感到寒冷的65令牌:看看你的玻璃告诉脸,你是另一个脸的脸H修复你更新你的令人沮丧的母亲公平的放弃母亲散发耕种你的畜牧坟墓自我停止后遗如塞后遗症你玻璃母亲杯子叫回电话的可爱的4月份你的Windows thine thine thine thine thine thine thine shative thy thy thy thy thy thy thy thy thy thy thy remedd die thine thy die thined图像Dies Dies Oree 71令牌:未持续的爱情为什么要在你的自我遗产遗产贝雷斯特没有给予Nothing Doth Lend Frank Leends免费美丽的黑鬼为什么Dost你滥用的浪费大型吉斯·吉尔斯·你好,为什么Dost你很大的概念自我孤独你是你的甜蜜自我欺骗自然叫做你已经消失了审计概念你离开了你的未使用的美容玉米的玉米生命的人寿人61令牌:小时温和工作框架可爱的凝视每只眼睛doth居住的暴君同样不公平相同的excel excel overlest the newlese lower夏天可怕的冬季混淆藏皮切检查了霜冻叶子相当黯淡的酸萨默夏天左液体囚犯墙玻璃美容效果美容局势和纪念花朵蒸馏出来虽然冬季遇见莱塞秀物质仍然生活甜蜜的68令牌:让冬天的夏天奥斯特·斯蒂利德制作甜蜜的瓶子宝贝,让你放置甜美的宝贝Ere Selfickilld禁止的高利风仍然付出愿意贷款,这是你的自我养殖另一个人十次十次十岁时你的自我快乐Thou art ten thy thy thy thy thy the ten the the the death你应该离开留下的生活后遗症selfilld thou art fair deals征服征服虫子征服了虫子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

创建一个字编码。按频率排序索引,只编码前100个单词。

内附= wordEncoding(文件,...'命令'“频率”...'maxnumwords', 100)
enc = wordEncoding with properties: NumWords: 100词汇表:[1x100字符串]

查看使用索引1,2和3对应的单词ind2word函数。

Idx = [1 2 3];单词= IND2WORD(ENC,IDX)
话说=1x3字符串“你的”“你”“爱”

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

文件名=“sonnetspreprocessed.txt”;str = extractFileText(文件名);textData =分裂(str,换行符);文件= tokenizedDocument (textData);文档(1:10)
ans = 10x1令叫鳕文件:70令牌:最公平的生物欲望增加,从而使得美女永远不会死于涟漪时间,温柔的继承人可能会忍受你的结束你的明亮眼睛饲料你的灯火般的丰富谎言谎言你的自我残忍你艺术世界新鲜装饰秀丽春天你的萌芽胸部春天的伤害伤害怜悯世界其他恐惧世界坟墓坟墓:四十次冬天应该围攻你的年轻人骄傲的杂草鸟类小值得举行问你的美女谎言你的愚蠢的日子说,你的深深沉没的日子归零耻辱令人羞耻的赞美赞美赞美你的好感你你应该答复普通的孩子矿素数算起旧的借口证明美女继承你的新款制作的你的血腥感到寒冷的65令牌:看看你的玻璃告诉脸,你是另一个脸的脸H修复你更新你的令人沮丧的母亲公平的放弃母亲散发耕种你的畜牧坟墓自我停止后遗如塞后遗症你玻璃母亲杯子叫回电话的可爱的4月份你的Windows thine thine thine thine thine thine thine shative thy thy thy thy thy thy thy thy thy thy thy remedd die thine thy die thined图像Dies Dies Oree 71令牌:未持续的爱情为什么要在你的自我遗产遗产贝雷斯特没有给予Nothing Doth Lend Frank Leends免费美丽的黑鬼为什么Dost你滥用的浪费大型吉斯·吉尔斯·你好,为什么Dost你很大的概念自我孤独你是你的甜蜜自我欺骗自然叫做你已经消失了审计概念你离开了你的未使用的美容玉米的玉米生命的人寿人61令牌:小时温和工作框架可爱的凝视每只眼睛doth居住的暴君同样不公平相同的excel excel overlest the newlese lower夏天可怕的冬季混淆藏皮切检查了霜冻叶子相当黯淡的酸萨默夏天左液体囚犯墙玻璃美容效果美容局势和纪念花朵蒸馏出来虽然冬季遇见莱塞秀物质仍然生活甜蜜的68令牌:让冬天的夏天奥斯特·斯蒂利德制作甜蜜的瓶子宝贝,让你放置甜美的宝贝Ere Selfickilld禁止的高利风仍然付出愿意贷款,这是你的自我养殖另一个人十次十次十岁时你的自我快乐Thou art ten thy thy thy thy thy the ten the the the death你应该离开留下的生活后遗症selfilld thou art fair deals征服征服虫子征服了虫子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

创建一个字编码。

内附= wordEncoding(文档)
enc = wordencoding属性:numwords:3092词汇:[1x3092字符串]

查看使用索引1,3和5对应的单词ind2word函数。

Idx = [1 3 5];单词= IND2WORD(ENC,IDX)
话说=1x3字符串“最公平”“欲望”“从而”

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

文件名=“sonnetspreprocessed.txt”;str = extractFileText(文件名);textData =分裂(str,换行符);文件= tokenizedDocument (textData);文档(1:10)
ans = 10x1令叫鳕文件:70令牌:最公平的生物欲望增加,从而使得美女永远不会死于涟漪时间,温柔的继承人可能会忍受你的结束你的明亮眼睛饲料你的灯火般的丰富谎言谎言你的自我残忍你艺术世界新鲜装饰秀丽春天你的萌芽胸部春天的伤害伤害怜悯世界其他恐惧世界坟墓坟墓:四十次冬天应该围攻你的年轻人骄傲的杂草鸟类小值得举行问你的美女谎言你的愚蠢的日子说,你的深深沉没的日子归零耻辱令人羞耻的赞美赞美赞美你的好感你你应该答复普通的孩子矿素数算起旧的借口证明美女继承你的新款制作的你的血腥感到寒冷的65令牌:看看你的玻璃告诉脸,你是另一个脸的脸H修复你更新你的令人沮丧的母亲公平的放弃母亲散发耕种你的畜牧坟墓自我停止后遗如塞后遗症你玻璃母亲杯子叫回电话的可爱的4月份你的Windows thine thine thine thine thine thine thine shative thy thy thy thy thy thy thy thy thy thy thy remedd die thine thy die thined图像Dies Dies Oree 71令牌:未持续的爱情为什么要在你的自我遗产遗产贝雷斯特没有给予Nothing Doth Lend Frank Leends免费美丽的黑鬼为什么Dost你滥用的浪费大型吉斯·吉尔斯·你好,为什么Dost你很大的概念自我孤独你是你的甜蜜自我欺骗自然叫做你已经消失了审计概念你离开了你的未使用的美容玉米的玉米生命的人寿人61令牌:小时温和工作框架可爱的凝视每只眼睛doth居住的暴君同样不公平相同的excel excel overlest the newlese lower夏天可怕的冬季混淆藏皮切检查了霜冻叶子相当黯淡的酸萨默夏天左液体囚犯墙玻璃美容效果美容局势和纪念花朵蒸馏出来虽然冬季遇见莱塞秀物质仍然生活甜蜜的68令牌:让冬天的夏天奥斯特·斯蒂利德制作甜蜜的瓶子宝贝,让你放置甜美的宝贝Ere Selfickilld禁止的高利风仍然付出愿意贷款,这是你的自我养殖另一个人十次十次十岁时你的自我快乐Thou art ten thy thy thy thy thy the ten the the the death你应该离开留下的生活后遗症selfilld thou art fair deals征服征服虫子征服了虫子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

创建一个字编码。

内附= wordEncoding(文档)
enc = wordencoding属性:numwords:3092词汇:[1x3092字符串]

将单词“rose”,“love”和“beauty”映射到编码索引中Word2ind.函数。

单词= [“玫瑰”“爱”“美丽”];话说idx = word2ind (enc)
idx =1×37 387 79.

加载出厂报告数据并创建一个令人畏缩的鳕文数组中。

文件名=“factoryreports.csv”;data = readtable(文件名,“TextType”'细绳');textData = data.Description;文件= tokenizedDocument (textData);

创建一个字编码。

内附= wordEncoding(文件);

将文档转换为单词索引序列。

序列= doc2sequence (enc,文档);

查看前10个序列的大小。每个序列都是1-by-年代矢量,在那里年代是序列中单词索引的数目。因为序列是填充的,年代是恒定的。

序列(1:10)
ans =.10×1个单元阵列{[0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10]}{[0 0 0 0 0 0 2 16 17 18 19 11 12 13 14 15 10]}{[0 0 0 0 0 0 20 2 7 7 21日22日23日24日25日26日10]}{[0 0 0 0 0 0 0 0 0 0 0 27 28 6 7 18 10]}{[0 0 0 0 0 0 0 0 0 0 0 0 29 30 7 31 10]}{[0 0 0 0 0 0 0 32 33 6 7 34 35 36 37 38 10]}{[0 0 0 0 0 0 0 0 0 39 40 36 41 6 7 42 10]}{[0 0 0 0 0 0 0 0 43 44 22 45 46 47岁7 48 10]} {[ 0 0 0 0 0 0 0 0 0 0 49 50 17 7 51 48 10]} {[0 0 0 0 52 8 53 36 54 55 56 57 58 59 22 60 10]}
介绍了R2018b