主要内容

从文件中提取文本数据

这个例子说明了如何从文本,HTML的Microsoft®Word,PDF,CSV和Microsoft Excel档案提取文本数据,并将其导入MATLAB®进行分析。

通常情况下,导入文本数据到MATLAB最简单的方法是使用extractFileText功能。这个函数提取从文本,PDF,HTML,和Microsoft Word文件中的文本数据。若要从CSV和Microsoft Excel文件导入文本,使用readtable。从HTML代码,使用提取文本extractHTMLText。为了读取PDF表单,使用数据readPDFFormData

文本文件

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

filename =.“sonnets.txt”;str =提取文件(文件名);

通过提取两个冠军“之间的文本视图中的第一首十四行诗一世“ 和 ”II“。

开始=“我”+换行符;鳍=“II”; 十四行诗1=extractBetween(str、start、fin)
sonnet1 =“从最美丽的生物,我们希望增加,以便美的玫瑰永远不会枯死,但在比较成熟的应时间去世,他的温柔的继承人可能会承担他的记忆:但你,以你自己的明眸,Feed'st你的光的火焰与自我实质性的燃料,使一片丰谎言,你的自我你的敌人,以你的甜美自己未免太狠:你认为现在是大地的清新的点缀,只有预示到锦绣阳春,在你自己的芽buriest你的内容用吝啬,嫩嫩的吝啬鬼mak'st废料:可怜这个世界吧,要不然,贪夫,就吞噬世界的份,由你和坟墓“。

对于包含换行符分隔的多个文档的文本文件,请使用阅读线功能。

filename =.“multilesonnets.txt”;STR = readlines方法(文件名)
海峡=3×1字符串“从最美丽的生物,我们希望增加,以便美的玫瑰永远不会枯死,但在比较成熟的应时间去世,他的温柔的继承人可能会承担他的记忆:但你,以你自己的明眸,Feed'st你的光的火苗自实质性的燃料,使一片丰谎言,你的自我你的敌人,以你的甜美自己未免太狠:你认为现在是大地的清新的点缀,只有预示到锦绣阳春,在你自己的芽buriest你的内容,并招标吝啬鬼mak'st浪费在吝啬:可怜这个世界吧,要不然,贪夫,就吞噬世界的份,由你和坟墓“。“当四十个冬天围攻你的朱颜,在你美的领域深挖战壕,你青春的华服弄到这么盯着现在,将举行小价值的tatter'd杂草:然后被要求,所有你的美丽谎言,哪里是你那少壮年华的宝藏;也就是说,内你自己深深凹陷的眼睛,是一个全贪婪的羞耻,和无益的颂扬多少更多的赞誉deserv'd你的美的用途,如果你couldst回答,“我这公平孩子。要总结我的账,并让我的老迈,”通过继承你证实他的美!这是当你是老了,看你的血液温暖,当你feel'st是冷是新的制造“。“看在你的玻璃,并告诉你的脸现在viewest是一次面应该形成另一个;谁的新鲜修复,如果你现在不renewest,就欺骗世界,剥掉母亲的幸福对于她在哪儿那么公平,其unear'd。?胎不愿被你饲养的耕作或者谁是他这么喜欢将坟墓,他的自爱,不要子孙中你是你母亲的玻璃,她在你身上唤回她的盛年的芳菲四月透过窗户那么你祢的暮年看到,尽管皱纹你这黄金时间。但是,如果你活着,一想起毁灭,模具单,你的形象模具与你同在“。

Microsoft Word文档

从中提取文本十四行诗使用extractFileText。文件exampleSonnets.docx包含莎士比亚的一个Microsoft Word文档中的十四行诗。

filename =.“示例十四行诗.docx”;str =提取文件(文件名);

通过提取两个标题之间的文本查看第二首十四行诗“II“ 和 ”III“。

开始=“II”+换行符;鳍=“III”; 十四行诗2=extractBetween(str、start、fin)
十四行诗2:“当四十个冬天将围困你的额头,在你美丽的田野里挖深沟时,你现在如此凝视着的青春的骄傲服饰,将是一株破烂的杂草,价值不高。那么有人问,你所有的美丽在哪里,你旺盛岁月的所有财富在哪里;可以说,在你深陷的眼睛里,有一种吃不消的羞耻和吝啬的赞美。如果你能回答“我这个漂亮的孩子会算计我的数目,给我一个老借口,”用你的继承来证明他的美貌,那你的美貌是多么值得称赞啊!当你年老时,这将是新的,当你感到寒冷时,你将看到你的血液温暖。”

这个例子Microsoft Word文档使用各行之间的两个换行符。用单个换行符替换这些字符,请使用代替功能。

十四行诗2=替换(十四行诗2、[newline-newline],newline)
十四行诗2:“当四十个冬天将围困你的额头,在你美丽的田野里挖深沟时,你现在如此凝视着的青春的骄傲服饰,将是一株破烂的杂草,价值不高。那么有人问,你所有的美丽在哪里,你旺盛岁月的所有财富在哪里;可以说,在你深陷的眼睛里,有一种吃不消的羞耻和吝啬的赞美。如果你能回答“我这个漂亮的孩子会算计我的数目,给我一个老借口,”用你的继承来证明他的美貌,那你的美貌是多么值得称赞啊!当你年老时,这将是新的,当你感到寒冷时,你将看到你的血液温暖。”

PDF文件

从PDF文档和来自PDF表单中的数据中提取文本。

PDF文件

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

filename =.“exampleSonnets.pdf”;str =提取文件(文件名);

通过提取两个冠军“之间的文本查看第三十四行诗III“ 和 ”IV”这PDF都有每个换行符前的空格。

开始=“iii”+换行符;鳍=“IV”;sonnet3 = extractBetween(STR,开始,鳍)
sonnet3 =“看在你的镜子,告诉脸你现在viewest是面对应形成另一个时间;谁的新鲜修复,如果你现在不renewest,就欺骗世界,剥掉母亲的幸福对于她在哪儿那么公平,其unear“d胎不愿被你饲养的耕作或者谁是他这么喜欢将坟墓,他的自爱,不要子孙中你是你母亲的玻璃,她在你身上唤回了她的芳菲四月黄金;?所以你通过祢年龄的窗户要看见,尽管皱纹你这黄金时间。但是,如果你活着,一想起毁灭,模具单,你的形象模具与你同在。“

PDF格式

要阅读从PDF表单,使用文本数据readPDFFormData。该函数返回包含PDF表单字段中数据的结构。

filename =.“weatherReportForm1.pdf”; data=readPDFFormData(文件名)
资料=结构与字段:EVENT_TYPE:“雷雨风” event_narrative:“大树Plantersville和内特尔顿之间上下。”

HTML

从HTML文件,HTML代码,网页提取文本。

HTML文件

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

filename =.“exampleSonnets.html”;str =提取文件(文件名);

通过提取两个标题之间的文本查看第四首十四行诗“IV”“v”

开始=换行符+“IV”+新线;fin=换行符+“v”+换行符;SONNET4 =提取物(str,start,fin)
sonnet4 =“Unthrifty可爱,你为什么要花在你的自我你的美丽的遗产自然的遗产给什么,但这话放贷,坦率,她借给那些都是免费的:那么,美丽的鄙夫,为什么你滥用的厚礼给你?给赔本的高利贷者,你为什么使用资金的那么一笔大款,还不能住了正独自用你自己的流量,你你的自我的你甜蜜的自我等于欺骗?那怎么当造化唤你可以走了,拿什么账目阴间,你还能走吗?你的未使用的美容一定要与你同在,其中,使用tombed,生活日”执行人是“。

HTML代码

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

代码=

十四行诗

莎士比亚 ”;str = extracthtmltext(代码)

STR =“莎士比亚十四行诗”

从网上

若要从网页中提取文本数据,采用先读取HTML代码韦布雷德,然后使用extractHTMLText

网址=“//www.tianjin-qmedu.com/help/textanalytics”; 代码=网络阅读(url);str=extractHTMLText(代码)
海峡=“文本分析工具箱分析和示范文本数据发布说明PDF文档版本说明PDF文档的文本分析工具箱™提供的算法和可视化的预处理,分析和建模的文本数据。用工具箱中创建的模型可以在应用程序中,如情感分析,预测性维护和主题建模使用。文本分析工具箱包括来自诸如设备日志,新闻,调查,经营者报告,以及社交媒体处理原始文本的工具。您可以提取流行的文件格式,预处理原始文本,提取单个单词,文本转换成文本数值表示,建立统计模型。使用机器学习技术,如LSA,LDA,文字的嵌入,你可以找到集群和创建高维数据集文本功能。特点与文本分析工具箱创建可以与其他数据源的特性来构建机器学习是采取文字,数字和其他类型的数据模型的优点结合起来。开始了解文本分析工具箱文本数据准备导入文本数据的基础知识到MATLAB®和它进行预处理分析模拟和预测使用主题模型和文字的嵌入显示和可视化演示文本数据和模型使用文字云和文本散点图开发预测模型在文本分析工具箱语万博1manbetx言支持语言支持的信息”

解析HTML代码

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

tree=htmlTree(代码);选择器=“A”;子树= findElement(树,选择器);

查看前10个子树并使用中提取文本extractHTMLText

子树(1:10)
str=提取HtmlText(子树);

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

STR(1:10)
ans =.10×1串跳到内容“产品”“解决方案”“学术界”“支持s manbetx 845”“社区”“事件”“获万博 尤文图斯取MATLAB”万博1manbetx

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

属性=“href”;STR =的getAttribute(子树(1:10),ATTR)
海峡=10×1串“#内容容器”//www.tianjin-qmedu.com?s_tid=gn_logo" "//www.tianjin-qmedu.com/products.html?ss manbetx 845_tid=gn_ps" "//www.tianjin-qmedu.com/solutions.html?万博 尤文图斯s_tid=gn_sol" "//www.tianjin-qmedu.com/academia.html?s_tid=gn_acad" "https://www.mathworks万博1manbetx.com/support.html?s_tid=gn_supp" "//www.tianjin-qmedu.com/matlabcentral/?s_tid=gn_mlc" "//www.tianjin-qmedu.com/company/events.html?s_tid=gn_ev" "//www.tianjin-qmedu.com/products/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表说明类别紧急分辨率成本_____________________________________________________________________ ____________________ ____________________ ________ _____“的相关文件偶尔会卡在扫描器卷轴。”“机械故障”,“中等”,“重新调整机” 45“大声剑拔弩张和敲打的声音从汇编活塞都来了。”“机械故障”,“中等”,“重新调整机” 35“有削减电力启动设备时。”“电子故障”“高”“完全替代” 16200“在汇编炒电容器。”“电子故障”“高”“替换组件” 352“混音器跳闸保险丝。”“电子故障”“低”“添加到收藏” 55“在构建代理爆管被喷洒冷却液。”“泄漏”“高”“替换组件” 371“A熔丝在混合器吹”。“电子故障”“低”“替换组件” 441“事情继续翻滚带了。”“机械故障”,“低”,“重新调整机” 38

从数据库中提取文本数据event_narrative.列,并查看第一弦数。

STR = T.Description;STR(1:10)
ans =.10×1串“项目是偶尔被陷在扫描仪卷轴。”“大声剑拔弩张和敲打的声音从汇编活塞的到来。”“有削减电力启动设备时。”“在汇编炒电容器。”“混色器跳闸保险丝。”“在构建剂突发管喷射冷却剂”。“A熔丝在混合器吹”。“事情继续翻滚带了。”“从输送带落下项目”。“扫描仪卷轴是分裂,它很快就会开始曲线”。

从多个文件中提取文本

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

为示例SONNET文本文件创建文件数据存储。示例文件名为“示例sonnetn.txt“, 在哪里N是十四行诗的数量。指定使用通配符的文件名“*”找到这个结构的所有文件名。要指定读功能是extractFileText,输入此功能fileDatastore使用功能句柄。

位置=完整文件(matlabroot,“例子”“textanalytics”“数据”“exampleSonnet * .TXT”); fds=文件数据存储(位置,'readfcn'@ extractFileText)
FDS = FileDatastore具有属性:文件:{ '... \ MATLAB \例子\ textanalytics \数据\ exampleSonnet1.txt';'... \ MATLAB \例子\ textanalytics \数据\ exampleSonnet2.txt';'... \ MATLAB \例子\ textanalytics \数据\ exampleSonnet3.txt' ...和2更}文件夹:{ '... \ MATLAB \例子\ textanalytics \数据'} UniformRead:0 ReadMode: '文件' BLOCKSIZE:Inf文件PreviewFcn:@extractFileText S万博1manbetxupportedOutputFormats:[ “TXT” “CSV” “XLSX” “XLS” “镶木”, “parq” “PNG” “JPG”, “JPEG”, “TIF”, “TIFF” “WAV” “后手”“OGG“ ”MP4“ ”M4A“] ReadFcn:@extractFileText AlternateFileSystemRoots:{}

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

STR = [];虽然Hasdata(FDS)TextData =读取(FDS);str = [str;textdata];结尾

查看提取的文本。

str.
海峡=5×1字符串“从最公平的生物,我们渴望增加,↵那么美丽的玫瑰可能永远不会死,但随着涟漪应该随着时间的评价,他的温柔继承人可能会忍受他的记忆:↵,但是你,签约自己明亮的眼睛,↵饲料'ST Thy Light的火焰与自我大量的燃料,↵在丰富的地方撒谎,↵你的自我你的敌人,对你的甜蜜的自我太残忍:↵你现在是世界上新鲜的装饰,♪而且只有先驱向格劳德春天,↵在你自己的萌芽中的内容中,↵和温柔的Churl Mak'st浪费在琐事中:↵怜悯世界,否则这个贪吃,♪在坟墓和你的坟墓吃世界。““当四十冬季围攻你的眉头,↵和挖掘深沟在你的美丽的领域,你现在所凝视的骄傲的衣服如此凝视着,♥将是一个杂草的小值得举行:↵然后被问到,所以秀丽谎言,♥所有的愚蠢的日子的宝藏;↵在你自己深沉的沉没的眼睛里,♥都是一个难以羞辱,而且填近的赞美。↵哥们的好评,你的美丽有多好评,如果你可以回答'这个公平的小孩↵↵应该总结一下我的伯爵,并造成我的老借口,'♥通过继承来证明他的美丽!↵这是新的,当你艺术老,↵看你的血液温暖感觉很冷。““看着你的玻璃,告诉你的脸,现在是那个面孔应该形成另一个的时候;♪谁的新修复如果你现在不早更新,那么让你渴望世界,不遵守一些母亲。谁的武器的解开了你的耕作?↵或者他如此喜欢是坟墓,他的自爱停止后代?↵你艺术你的母亲的玻璃,她在你的玻璃杯里呼吁可爱她的素质的4月;所以你通过Thine的Windows Shalt看到了,尽管你的黄金时间皱起了皱纹。但是如果你活着,记得不作为,↵死于单身和你的图像模具。““不持续的可爱,为什么让你花在你的自我的遗产上?♥大自然的贝斯特没有什么,但是借给那些是自由的,而不是坦率,她借给那些有空的:↵然后,美丽的黑鬼,为什么让你滥用的吉盛欲绝的是你的利润,为什么要使用它这么伟大的总和,但才能没有生活?↵对于你的自我交通,你是你的自己,你是甜蜜的自我dost欺骗:↵ when nature calls thee to be gone,↵ What acceptable audit canst thou leave?↵ Thy unused beauty must be tombed with thee,↵ Which, used, lives th' executor to be." "from fairest creatures we desire increase that thereby beautys rose might never die but as the riper should by time decease his tender heir might bear his memory but thou contracted to thine own bright eyes feedst thy lights flame with selfsubstantial fuel making a famine where abundance lies thy self thy foe to thy sweet self too cruel thou that art now the worlds fresh ornament and only herald to the gaudy spring within thine own bud buriest thy content and tender churl makst waste in niggarding pity the world or else this glutton be to eat the worlds due by the grave and thee↵when forty winters shall besiege thy brow and dig deep trenches in thy beautys field thy youths proud livery so gazed on now will be a tatterd weed of small worth held then being asked where all thy beauty lies where all the treasure of thy lusty days to say within thine own deep sunken eyes were an alleating shame and thriftless praise how much more praise deservd thy beautys use if thou couldst answer this fair child of mine shall sum my count and make my old excuse proving his beauty by succession thine this were to be new made when thou art old and see thy blood warm when thou feelst it cold↵look in thy glass and tell the face thou viewest now is the time that face should form another whose fresh repair if now thou not renewest thou dost beguile the world unbless some mother for where is she so fair whose uneard womb disdains the tillage of thy husbandry or who is he so fond will be the tomb of his selflove to stop posterity thou art thy mothers glass and she in thee calls back the lovely april of her prime so thou through windows of thine age shalt see despite of wrinkles this thy golden time but if thou live rememberd not to be die single and thine image dies with thee↵unthrifty loveliness why dost thou spend upon thy self thy beautys legacy natures bequest gives nothing but doth lend and being frank she lends to those are free then beauteous niggard why dost thou abuse the bounteous largess given thee to give profitless usurer why dost thou use so great a sum of sums yet canst not live for having traffic with thy self alone thou of thy self thy sweet self dost deceive then how when nature calls thee to be gone what acceptable audit canst thou leave thy unused beauty must be tombed with thee which used lives th executor to be↵those hours that with gentle work did frame the lovely gaze where every eye doth dwell will play the tyrants to the very same and that unfair which fairly 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…"

也可以看看

|||

相关话题