主要内容

从文件中提取文本数据

此示例显示了如何从文本,HTML,Microsoft®Word,PDF,CSV和MicrosoftExcel®文件中提取文本数据,并将其导入Matlab®进行分析。

通常,将文本数据导入到MATLAB中最简单的方法是使用extractFileText函数。这个函数从文本、PDF、HTML和Microsoft Word文件中提取文本数据。要从CSV和Microsoft Excel文件中导入文本,请使用readtable.要从HTML代码中提取文本,请使用extractHTMLText.要从PDF表格中读取数据,请使用readPDFFormData

文本文件

从中提取文本sonnets.txt.使用extractFileText.该文件sonnets.txt.包含莎士比亚的十四行诗的纯文本。

文件名=“sonnets.txt”;str=extractFileText(文件名);

通过抽取两个标题之间的文本来查看第一首十四行诗。“和”2".

开始=“ 一世”+换行;鳍=“II”;SONNET1 =提取物(str,start,fin)
十四行诗我们希望从最美丽的生灵身上生长,这样美的玫瑰就永远不会凋谢;可是你,只盯着自己明亮的眼睛,把自己的物质燃料填满你的火焰,把富足的地方变成饥荒,把你自己当作敌人,把你可爱的自己弄得太残酷:你现在是这世界的新鲜的装饰品,是这绚丽春天的唯一使者,把你的满足埋在你的花蕾里,温柔的粗人把你的吝啬浪费掉:可怜这世界吧,否则这个贪吃的人,要用坟墓和你来吃掉这世界的本分。”

对于包含多个以换行符分隔的文档的文本文件,请使用READLINES.函数。

文件名=“multilindinesonnets.txt”;str = readline(文件名)
str =3×1串“从最美丽的生物中我们渴望增加,因此美丽的玫瑰可能永远不会死,但随着涟漪应该随着时间的评估,他的温柔继承人可能会忍受他的记忆:但是你,签约自己明亮的眼睛,喂养你的光明的火焰自我大量的燃料,在丰富的地方撒谎,你的自我你的敌人,你的甜蜜的自我太残忍了:你现在是世界上新的装饰品,而且只有先驱到华丽的春天,在你自己的萌芽中,才能在你自己的萌芽中,和温柔的Churl Mak'st浪费在琐事中:遗憾的是世界,否则这个贪吃是,在坟墓和你身边吃世界的到期。““当四十冬天围攻你的眉头,并在你的美丽领域挖掘深沟,你现在所凝视的骄傲的衣服如此凝视着,将是一个破烂的杂草的小值得举行:然后被问到,所有你的美丽谎言,在哪里你疯狂的日子的所有宝藏;说,在你自己深沉的眼睛里,是一种难以置信的耻辱,一定令人羞耻,一致好评。如果你可以回答这个公平的孩子应该总结一下,并使我的老借口“,通过继承来证明他的美丽!这是为了艺术老,当你感到寒冷时,看到你的血液温暖。”“看看你的玻璃杯,告诉你现在是脸部应该形成另一个人的时候;如果你现在没有更新,你的新修理,你渴望世界,脱一些母亲。她在哪里是她如此公平的那个子宫蔑视你的耕作者的耕作?或者是谁是如此喜欢,他的自爱停止后遗症?你妈妈的玻璃杯,她在你的春天的可爱4月份;所以你通过窗户虽然皱纹,但是,尽管你的黄金时间皱纹,但如果你活着,那就记得不成为,死于单身和你的图像与你一起死亡。“

Microsoft Word文档

从中提取文本Sonnets.docx.使用extractFileText.该文件exampleSonnets.docx微软Word文档中包含了莎士比亚的十四行诗。

文件名=“idessonnets.docx”;str=extractFileText(文件名);

通过提取两个标题之间的文本来查看第二个十四行诗“2“和”3".

开始=“II”+换行;鳍=“三世”;SONNET2 =提取物(str,start,fin)
Sonnet2 =“当四十冬季围攻你的眉头,并在你们美丽的领域挖掘深沟,现在的骄傲的衣服如此凝视着现在,将是一个杂草的小杂草:然后被问到,所有你的美丽谎言,在你疯狂的一天的所有宝藏;说,在你自己深沉的沉没的眼睛里,是一种饮食般的耻辱,一定令人羞耻,一致好评。如果你能回答这个公平的孩子我的伯爵队,并通过继承的借口来达成我的伯爵,并通过继承来证明他的美丽!这是为了艺术老的时候成为新的制作,当你感到寒冷时,看到你的血液温暖。“

示例Microsoft Word文档在每行之间使用两个换行符。要用一个换行符替换这些字符,请使用代替函数。

SONNET2 =替换(SONNET2,[Newline Newline],Newline)
Sonnet2 =“当四十冬季围攻你的眉头,并在你们美丽的领域挖掘深沟,现在的骄傲的衣服如此凝视着现在,将是一个杂草的小杂草:然后被问到,所有你的美丽谎言,在你疯狂的一天的所有宝藏;说,在你自己深沉的沉没的眼睛里,是一种饮食般的耻辱,一定令人羞耻,一致好评。如果你能回答这个公平的孩子我的伯爵队,并通过继承的借口来达成我的伯爵,并通过继承来证明他的美丽!这是为了艺术老的时候成为新的制作,当你感到寒冷时,看到你的血液温暖。“

PDF文件

从PDF文档中提取文本,从PDF表单中提取数据。

PDF文档

从中提取文本sonnets.pdf使用extractFileText.该文件例子.pdf.pdf.pdf.pdf.pdf.包含PDF中的莎士比亚的十四行诗。

文件名=“exampleSonnets.pdf”;str=extractFileText(文件名);

通过抽取两个标题之间的文本来查看第三首十四行诗"3“和”4".这个PDF在每个换行符之前都有一个空格。

开始=“三”+换行;鳍=“四”;sonnet3 = extractBetween (str,开始,翅片)
十四行诗"照照镜子,告诉你所见的那张脸如果你现在不重新修复它,你就是在欺骗世界,剥夺某个母亲的幸福。她的未出世的子宫蔑视你的耕作,哪里有她如此美丽?或者谁将是他的坟墓,他的自爱阻止子孙后代?你是你母亲的镜子,她在你身上唤回她美丽的春日;这样,你将透过岁月的窗户看到,尽管皱纹多,这是你的黄金时光。但如果你活着,不愿被人记住,就独自死去,你的肖像与你一同死去。”

PDF形式

要从PDF格式读取文本数据,请使用readPDFFormData. 函数返回一个包含PDF表单字段数据的结构。

文件名=“weatherrereportform1.pdf”;data = readpdfformdata(文件名)
数据=结构体字段:event_type:“雷暴风”event_narrative:“大树在大花坛和涅特雷顿之间。”

HTML.

从HTML文件、HTML代码和web中提取文本。

HTML文件

要从已保存的HTML文件中提取文本数据,请使用extractFileText

文件名=“idesidonsets.html”;str=extractFileText(文件名);

通过在两个标题之间提取文本来查看SONNET“四”“V”

Start = newline +“四”+换行;FIN =换行符+“V”+换行符;十四行诗4=extractBetween(str、start、fin)
十四行诗:“可爱的浪子啊,你为什么把美貌的遗产浪费在自己身上?大自然的遗赠不给予任何东西,只是借给别人,她是坦率地借给那些自由的人:那么,美丽的吝啬鬼,你为什么滥用给你的慷慨馈赠呢?无利可图的高利贷者,你为什么用了这么一笔款子还不能生活呢?因为只跟你自己做买卖,你就是在欺骗你自己,你那可爱的自己:那么,当造化唤你去的时候,你能留下什么可接受的帐目呢?你未用过的美将和你一起进坟墓,一旦用去,就活着为你执行遗嘱。”

HTML代码

要从包含HTML代码的字符串中提取文本数据,请使用extractHTMLText

代码="

THE十四行诗

by William Shakespeare

"
; str=extractHTMLText(代码)
莎士比亚十四行诗

从网上

要从网页中提取文本数据,首先阅读HTML代码使用网络阅读,然后使用extractHTMLText

URL =."//www.tianjin-qmedu.com/help/textanalytics";代码= Webrabread(URL);str = extracthtmltext(代码)
发布说明PDF文档发布说明PDF文档文本分析工具箱™为文本数据的预处理、分析和建模提供了算法和可视化。使用工具箱创建的模型可用于情感分析、预测维护和主题建模等应用程序。文本分析工具箱包括用于处理来自设备日志、新闻源、调查、运营商报告和社交媒体等来源的原始文本的工具。您可以从流行的文件格式中提取文本,预处理原始文本,提取单个单词,将文本转换为数字表示,并构建统计模型。使用机器学习技术,如LSA、LDA和单词嵌入,您可以从高维文本数据集找到聚类并创建特征。使用Text Analytics Toolbox创建的功能可以与来自其他数据源的功能相结合,以构建利用文本、数字和其他类型数据的机器学习模型。开始学习基本的文本分析工具箱文本数据准备文本数据导入MATLAB®和预处理分析建模和预测开发预测模型使用主题模型和文字嵌入显示和演示可视化文本数据和模型使用词云分散图和文本语言支持万博1manbetx关于文本分析工具箱中的语言支持的信息万博1manbetx

解析HTML代码

要查找HTML代码的特定元素,请使用parsehtmlTree和使用Fedelement..解析HTML代码并找到所有的超链接。超链接是具有元素名称的节点“一种”

树= htmltree(代码);选择器=“一种”;子树= findElement(树,选择器);

查看前10个子树并使用extractHTMLText

子树(1:10)
str = extracthtmltext(子树);

查看前10个超链接的提取文本。

str (1:10)
ans =10×1字符串“跳到内容”“”产品“”解决方案“”Acads manbetx 845emia“”支持“”社万博 尤文图斯区“”事件“”得到Matlab“”“万博1manbetx

要获取链接目标,请使用getAttributes并指定属性“href”(超链接引用)。获取前10个子树的链接目标。

attr =“href”;str = getAttribute(子树(1:10),attr)
str =10×1字符串“#content_container”“//www.tianjin-qmedu.com?s_tid=gn_logo”“//www.tianjin-qmedu.com/prs manbetx 845oducts.html?s_tid=gn_ps”“//www.tianjin-qmedu.com/sol万博 尤文图斯utions.html?s_tid = gn_sol“//www.tianjin-qmedu.com/academia.html?s_tid=gn_acad”“https://www.tianjin-qmedu.com/s万博1manbetxupport.html?s_tid=gn_supp”https://www.tianjin-qmedu.com/matlabcentral/?s_tid=gn_mlc“//www.tianjin-qmedu.com/company/events.html?s_tid=gn_ev”“//www.tianjin-qmedu.com/pros manbetx 845ducts/get-matlab.html?s_tid = gn_getml“//www.tianjin-qmedu.com?s_tid=gn_logo”

CSV和Microsoft Excel文件

要从CSV和Microsoft Excel文件中提取文本数据,请使用readtable并从其返回的表中提取文本数据。

从中提取表数据factoryReposts.csv使用readtable函数并查看表的前几行。

T = readtable ('factoryreports.csv',“TextType”,“字符串”);头(T)
ans =8×5表类别描述紧急解决成本  _____________________________________________________________________ ____________________ ________ ____________________ _____ " 项目是偶尔陷入扫描仪卷。”“机械故障”、“中等”、“重新调整机器”、“组装器的活塞发出响亮的咔嗒咔嗒和砰砰的声音。”“机械故障”“中等”“调整机器”“启动工厂时电源被切断”“电子故障”“高”“完全更换”“16200”“组装器内电容器烧毁”“电子故障”“高”“更换元件”“352”“混频器跳闸保险丝。”“电子故障”“低”“列入观察名单”“55”施工剂中爆管正在喷洒冷却剂。"泄漏" "高" "更换部件" 371 "混合器内保险丝熔断"“电子故障”“低”“更换部件”“东西不断从传送带上掉下来。”“机械故障”“低”“重新调整机

从中提取文本数据事件叙述列并查看前几个字符串。

str = t.description;str (1:10)
ans =10×1字符串“物品偶尔会卡在扫描仪的线轴上。”“组装器的活塞发出响亮的咔嗒咔嗒和砰砰的声音。”“启动核电站时,电力会被切断。”“组装器里的电容器被炸了。”“搅拌机把保险丝弄坏了。”"爆破管道中施工剂正在喷洒冷却剂""搅拌机里的保险丝烧断了"“事情继续从腰带上滑落。”“从传送带上掉下来的东西。”扫描卷轴一旦分开,很快就会开始弯曲。

从多个文件提取文本

如果您的文本数据包含在文件夹中的多个文件中,则可以使用文件数据存储将文本数据导入MATLAB。

为示例十四行诗文本文件创建文件数据存储。示例文件名为“示例entn.txt.”,N是十四行诗的编号。使用通配符“*”指定文件名,以查找此结构的所有文件名。指定要读取的函数extractFileText,输入此函数fileDatastore使用函数句柄。

位置= fullfile(matlabroot,“例子”,“textanalytics”,“数据”,“exampleSonnet * . txt”);fds = filedataStore(位置,“ReadFcn”@extractFileText)
fds = FileDatastore与属性:Files:{'…\matlab\examples\textanalytics\data\exampleSonnet1.txt';“…\ matlab \ \ textanalytics \ \ exampleSonnet2.txt数据的例子;“…\ matlab \ \ textanalytics \ \ exampleSonnet3.txt数据的例子…和2个更多的}文件夹:{'…ReadFcn: @extractFileText SupportedOutputFormats: ["txt" "csv" "xlsx" "xls" "parquet" "parq" "png" "jpg" "jpeg" "ti万博1manbetxf" "tiff" "wav" "flac" "ogg" "mp4" "m4a"] ReadFcn: @extractFileText AlternateFileSystemRoots: {}

循环遍历数据存储中的文件并读取每个文本文件。

str = [];尽管hasdata(fds)textData=读取(fds);str=[str;textData];结束

查看提取的文本。

str
str =5×1串“我们渴望从最美丽的生物身上得到更多,↵ 因此,美丽的玫瑰也许永远不会凋谢,↵ 但随着时间的流逝,成熟者也会逐渐成熟,↵ 他的温柔继承人可能会记住他:↵ 但是你,与你自己明亮的眼睛签约,↵ 用自足的燃料喂养你的光之火焰,↵ 在富足的地方制造饥荒,↵ 你自己是你的敌人,对你甜蜜的自己太残忍了:↵ 你现在是世界上最新的装饰,↵ 也是花哨春天的唯一使者,↵ 在你自己的花蕾里埋藏着你的内容,↵ 而娇嫩的小气最浪费:↵ 可怜这个世界,否则这个贪吃的人,↵ “当四十个冬天将围困你的额头,↵ 在你美丽的土地上挖深沟,↵ 你的青春的骄傲的制服现在如此凝视,↵ 将是一株破烂的杂草,价值不菲:↵ 然后被问到,你所有的美在哪里,↵ 在那里,所有的财富,你的辉煌的日子;↵ 说,在你深沉的眼睛里,↵ 都是吃不消的羞耻和吝啬的赞美。↵ 你的美貌更值得赞美,↵ 如果你能回答“我这个漂亮的孩子↵ 我要算算我的总数,并提出我以前的借口,'↵ 用你的继承证明他的美丽!↵ 当你年老时,这将是新的,↵ 当你觉得冷的时候,看着你的血液温暖。”“看着你的镜子,告诉你看到的脸↵ 现在是时候让脸变成另一张了;↵ 如果你现在不更新,谁的新修复,↵ 你欺骗了整个世界,不伤害一些母亲。↵ 因为她未出生的子宫在哪里如此美丽↵ 鄙视你的耕作?↵ 或者他这么喜欢谁会是坟墓,↵ 他的自爱阻止了后代?↵ 你是你母亲的镜子,她在你里面↵ 回忆起她青春的四月;↵ 所以你将透过你这个年龄的窗户看到,↵ 尽管有皱纹,这是你的黄金时代。↵ 但如果你活着,请记住你不应该,↵ 独自一人死去,你的形象与你一同死去。”“无情的可爱,你为什么要花费↵ 在你自己身上,你美丽的遗产?↵ 大自然的遗赠什么也不给,但会借给你什么,↵ 坦率地说,她借给那些免费的人:↵ 那么,漂亮的小气鬼,你为什么辱骂我↵ 给你的慷慨的施舍?↵ 无利可图的高利贷者,你为什么用↵ 这么多钱,却不能活下去?↵ 因为你独自一人与自己来往,↵ 你的自我,你甜蜜的自我欺骗了你:↵ 那么,当大自然召唤你离开时,↵ 你还能留下什么?↵ 你未用过的美丽必须与你相伴,↵ 过去,它一直是遗嘱执行人。”"从最美丽的生物身上,我们渴望美丽的玫瑰永不凋谢,但随着成熟者的逝去,他温柔的继承人可能会记住他的记忆,但你却与自己明亮的眼睛签约,用自己丰富的燃料喂养你的光芒,制造饥荒,在富足的地方,你自己,你的敌人,你甜蜜的自我,你太残忍了艺术现在是世界上最新鲜的装饰,是你自己花蕾中花哨春天的唯一预告,你的内容最厚重,温柔最娇嫩,在吝啬的怜悯中浪费世界,否则这个贪吃的人会吃掉坟墓和你应得的世界↵当四十个冬天将围困你的额头,在你美丽的田野上挖出深深的战壕时,你的年轻人骄傲的身躯现在所凝视的将是一株破烂的杂草,价值很小,当被问到你所有的美丽在哪里,在你深陷的眼睛里,你所有的光辉岁月的财富在哪里,都是一种无耻和吝啬的赞美如果你能回答我的问题,你的美貌就值得更多的赞美。我的这个美丽的孩子将计算我的总数,用我的旧借口证明他的美貌。你的美貌将在你年老的时候被创造出来,当你感到寒冷的时候,你的血液会变得温暖↵看看你的镜子,告诉我你现在看到的那张脸,是时候让那张脸变成另一张了。如果你现在不更新的话,那张脸会重新修复。你会欺骗这个世界的,不带偏见的一些母亲,因为她在哪里如此美丽,她的未出生的子宫蔑视你的耕作,或者他如此喜爱的人会成为他的自爱的坟墓,阻止你的后代你的母亲举杯,她在你心中唤回她青春的四月,这样你就可以透过你这个年龄的窗户看到你的黄金时代,尽管有皱纹,但如果你活着,请记住不要单身死去,你的形象也会随着你一起死去↵你为什么要把你美丽的遗产花在自己身上?自然的遗产只会借给那些自由的人,而坦率地说,她借给那些美丽的吝啬鬼。你为什么滥用给你的慷慨大方,给无利可图的高利贷者?你为什么要用这么多的钱,却不能与他们来往你的自我你的自我你的自我你的甜蜜的自我欺骗了那么当大自然召唤你离开的时候,你能留下什么可以接受的审计你未使用的美丽必须与你在一起↵那些用温柔的工作勾勒出每一只眼睛所驻扎的可爱的凝视的时间,将对同样的暴君和那些不公平的暴君施展暴君的伎俩airly doth excel for neverresting time leads summer on to hideous winter and confounds him there sap checked with frost and lusty leaves quite gone beauty oersnowed and bareness every where then were not summers distillation left a liquid prisoner pent in walls of glass beautys effect with beauty were bereft nor it nor no remembrance what it was but flowers distilld though they with winter meet leese but their show their substance still lives sweet↵then let not winters ragged hand deface in thee thy summer ere thou be distilld make sweet some vial treasure thou some place with beautys treasure ere it be selfkilld that use is not forbidden usury which happies those that pay the willing loan thats for thy self to breed another thee or ten times happier be it ten for one ten times thy self were happier than thou art if ten of thine ten times refigurd thee then what could death do if thou shouldst depart leaving thee living in posterity be not selfwilld for thou art much too fair to be deaths conquest and make worms thine heir↵lo in the orient when the gracious light lifts up his burning head each under eye doth homage to his newappearing sight serving with looks his sacred majesty and having climbd the steepup heavenly hill resembling strong youth in his middle age yet mortal looks adore his beauty still attending on his golden pilgrimage but when from highmost pitch with weary car like feeble age he reeleth from the day the eyes fore duteous now converted are from his low tract and look another way so thou thyself outgoing in thy noon unlookd on diest unless thou get a son↵music to hear why hearst thou music sadly sweets with sweets war not joy delights in joy why lovst thou that which thou receivst not gladly or else receivst with pleasure thine annoy if the true concord of welltuned sounds by unions married do offend thine ear they do but sweetly chide thee who confounds in singleness the parts that thou shouldst bear mark how one string sweet husband to another strikes each in each by mutual ordering resembling sire and child and happy mother who all in one one pleasing note do sing whose speechless song being many seeming one sings this to thee thou single wilt prove none↵is it for fear to wet a widows eye that thou consumst thy self in single life ah if thou issueless shalt hap to die the world will wail thee like a makeless wife the world will be thy widow and still weep that thou no form of thee hast left behind when every private widow well may keep by childrens eyes her husbands shape in mind look what an unthrift in the world doth spend shifts but his place for still the world enjoys it but beautys waste hath in the world an end and kept unused the user so destroys it no love toward others in that bosom sits that on himself such murdrous shame commits↵for shame deny that thou bearst love to any who for thy self art so unprovident grant if thou wilt thou art belovd of many but that thou none lovst is most evident for thou art so possessd with murderous hate that gainst thy self thou stickst not to conspire seeking that beauteous roof to ruinate which to repair should be thy chief desire o change thy thought that i may change my mind shall hate be fairer lodgd than gentle love be as thy presence is gracious and kind or to thyself at least kindhearted prove make thee another self for love of me that beauty still may live in thine or thee↵as fast as thou shalt wane so fast thou growst in one of thine from that which thou departest and that fresh blood which youngly thou bestowst thou mayst call thine when thou from youth convertest herein lives wisdom beauty and increase without this folly age and cold decay if all were minded so the times should cease and threescore year would make the world away let those whom nature hath not made for store harsh featureless and rude barrenly perish look whom she best endowd she gave thee more which bounteous gift thou shouldst in bounty cherish she carvd thee for her seal and meant thereby thou shouldst print more not let that copy die↵when i do count the clock that tells the time and see the brave day sunk in hideous night when i behold the violet past prime and sable curls all silvered oer with white when lofty trees i see barren of leaves which erst from heat did canopy the herd and summers green all girded up in sheaves borne on the bier with white and bristly beard then of thy beauty do i question make that thou among the wastes of time must go since sweets and beauties do themselves forsake and die as fast as they see others grow and nothing gainst times scythe can make defence save breed to brave him when he takes thee hence↵o that you were your self but love you are no longer yours than you your self here live against this coming end you should prepare and your sweet semblance to some other give so should that beauty which you hold in lease find no determination then you were yourself again after yourselfs decease when your sweet issue your sweet form should bear who lets so fair a house fall to decay which husbandry in honour might uphold against the stormy gusts of winters day and barren rage of deaths eternal cold o none but unthrifts dear my love you know you had a father let your son say so↵not from the stars do i my judgement pluck and yet methinks i have astronomy but not to tell of good or evil luck of plagues of dearths or seasons quality nor can i fortune to brief minutes tell pointing to each his thunder rain and wind or say with princes if it shall go well by oft predict that i in heaven find but from thine eyes my knowledge i derive and constant stars in them i read such art as truth and beauty shall together thrive if from thyself to store thou wouldst convert or else of thee this i prognosticate thy end is truths and beautys doom and date↵when i consider every thing that grows holds in perfection but a little moment that this huge stage presenteth nought but shows whereon the stars in secret influence comment when i perceive that men as plants increase cheered and checked even by the selfsame sky vaunt in their youthful sap at height decrease and wear their brave state out of memory then the conceit of this inconstant stay sets you most rich in youth before my sight where wasteful time debateth with decay to change your day of youth to sullied night and all in war with time for love of you as he takes from you i engraft you new↵but wherefore do not you a mightier way make war upon this bloody tyrant time and fortify your self in your decay with means more blessed than my barren rhyme now stand you on the top of happy hours and many maiden gardens yet unset with virtuous wish would bear you living flowers much liker than your painted counterfeit so should the lines of life that life repair which this times pencil or my pupil pen neither in inward worth nor outward fair can make you live your self in eyes of men to give away yourself keeps yourself still and you must live drawn by your own sweet skill↵who will believe my verse in time to come if it were filld with your most high deserts though yet heaven knows it is but as a tomb which hides your life and shows not half your parts if i could write the beauty of your eyes and in fresh numbers number all your graces the age to come would say this poet lies such heavenly touches neer touchd earthly faces so should my papers yellowd with their age be scornd like old men of less truth than tongue and your true rights be termd a poets rage and stretched me…"

另请参阅

|||

相关话题