主要内容

从文件中提取文本数据

这个例子展示了如何从文本、HTML、Microsoft®Word、PDF、CSV和Microsoft Excel®文件中提取文本数据,并将其导入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".

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

对于包含由换行符分隔的多个文档的文本文件,使用readline函数。

文件名=“multilineSonnets.txt”;str = readline(文件名)
str =3×1的字符串“我们希望从最美丽的生灵身上生长,这样,美之玫瑰就永远不会凋谢;可是你,只盯着自己明亮的眼睛,把自己的物质燃料填满你的火焰,把富足的地方变成饥荒,把你自己当作敌人,把你可爱的自己弄得太残酷:你现在是这世界的新鲜的装饰品,是这绚丽春天的唯一使者,把你的满足埋在你的花蕾里,温柔的粗人把你的吝啬浪费掉:可怜这世界吧,否则这个贪吃的人,要用坟墓和你来吃掉这世界的本分。”“当四十个冬天将围攻你的额头,在你美丽的田野上掘出深深的壕沟,你年轻时的骄傲的制服现在将被如此注视,变成一株无用的破草:那时有人问,你的全部美貌在哪里,你青春的全部财富在哪里;在你深陷的眼眸里说,这是一种吞噬一切的耻辱,和挥霍无度的赞美。如果你能这样回答:“我这美丽的孩子将算一算我的账,用我的老借口,”证明他的美丽继承了你的美貌,那该多值得赞美啊!当你年老时,这将是新的,当你感到寒冷时,看到你的血液温暖。"“照照镜子,告诉你所见的那张脸。如果你现在不重新修复它,你就是在欺骗世界,剥夺某个母亲的幸福。她的未出世的子宫蔑视你的耕作,哪里有她如此美丽? Or who is he so fond will be the tomb, Of his self-love to stop posterity? Thou art thy mother's 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, remember'd not to be, Die single and thine image dies with thee."

Microsoft Word文档

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

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

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

开始=“二世”+换行;鳍=“三世”;sonnet2 = extractBetween (str,开始,翅片)
sonnet2 = "当四十冬天围攻你的眉毛,你的美丽的领域深入挖掘战壕,你青春的骄傲制服现在凝视着,将小价值的碎布会杂草:然后被要求,所有你的美丽谎言,所有的珍惜你的精力充沛的天;在你深陷的眼眸里说,这是一种吞噬一切的耻辱,和挥霍无度的赞美。如果你能这样回答:“我这美丽的孩子将算一算我的账,用我的老借口,”证明他的美丽继承了你的美貌,那该多值得赞美啊!当你年老时,这将是新的,当你感到寒冷时,看到你的血液温暖。"

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

Sonnet2 = replace(Sonnet2,[newline newline],newline)
sonnet2 = "当四十冬天围攻你的眉毛,你的美丽的领域深入挖掘战壕,你青春的骄傲制服现在凝视着,将小价值的碎布会杂草:然后被要求,所有你的美丽谎言,所有的珍惜你的精力充沛的天;在你深陷的眼眸里说,这是一种吞噬一切的耻辱,和挥霍无度的赞美。如果你能这样回答:“我这美丽的孩子将算一算我的账,用我的老借口,”证明他的美丽继承了你的美貌,那该多值得赞美啊!当你年老时,这将是新的,当你感到寒冷时,看到你的血液温暖。"

PDF文件

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

PDF文档

将文本从sonnets.pdf使用extractFileText.该文件exampleSonnets.pdf包含莎士比亚的十四行诗的PDF文件。

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

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

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

PDF表单

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

文件名=“weatherReportForm1.pdf”;data = readPDFFormData(文件名)
data =结构体字段:event_type:“雷暴风”event_narrative:“Plantersville和Nettleton之间的大树倒下了。”

超文本标记语言

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

HTML文件

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

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

通过抽取两个标题之间的文本来查看第四首十四行诗“四”“V”

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

HTML代码

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

代码="

THE十四行诗

by William Shakespeare

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

从网上

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

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

解析HTML代码

要找到HTML代码中的特定元素,请使用htmlTree和使用findElement.解析HTML代码并找到所有超链接。超链接是带有元素名的节点“一个”

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

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

子树(1:10)
ans = 10×1 htmlTree:<类= " skip_link sr-only " href = " # content_container " >跳到内容< / > < A href = " //www.tatmou.com?s_tid=gn_logo " class = " svg_link navbar-brand”> < IMG src = " /图片/响应/全球/ pic-header-mathworks-logo.svg”类=“mw_logo”alt = " MathWorks " / > < / > < A href = " //www.tatmou.com/pro下载188bet金宝搏ducts.html?s_tid=gn_ps " > < / > <一个产品href = " https://www.mathworks.c金宝搏官方网站om/solutions.html?s_tid=gn_sol " >解决方案< / > < A href = " //www.tatmou.com/academia.html?s_tid=gn_acad " >学术界< / > < A href = " https://www.金宝appmathworks.com/support.html?s_tid=gn_supp " >支持< / > < A href = " //www.tatmou.com/matlabcentral/?s_tid=gn_mlc " > < / > <社区事件href = " //www.tatmou.com/company/events.html?s_tid=gn_ev " > < / > < A href = " https://www.mathwor下载188bet金宝搏ks.com/products/get-matlab.html?s_tid=gn_getml " >得到MATLAB < / > < A href = " //www.tatmou.com?s_tid=gn_logo " class = " svg_link pull-left”> < IMG src = " /图片/响应/全球/ pic-header-mathworks-logo.svg”类=“mw_logo”alt = " MathWorks " / > < / >
str = extractHTMLText(子树);

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

str (1:10)
ans =10×1的字符串"跳到内容" "" "产品""解决方案""学下载188bet金宝搏术界""支持""社区"金宝搏官方网站"事件""获取MATLAB" ""金宝app

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

attr =“href”;str = getAttribute(子树(1:10),attr)
str =10×1的字符串“# content_container”“//www.tatmou.com?s_tid=gn_logo”“//www.tatmou.com/p下载188bet金宝搏roducts.html?s_tid=gn_ps”“//www.tatmou.com/so金宝搏官方网站lutions.html?s_tid=gn_sol”“//www.tatmou.com/academia.html?s_tid=gn_acad”“https://www.金宝appmathworks.com/support.html?s_tid=gn_supp”“//www.tatmou.com/matlabcentral/?s_tid=gn_mlc”“//www.tatmou.com/company/events.html?s_tid=gn_ev”“//www.tatmou.com/p下载188bet金宝搏roducts/get-matlab.html?s_tid=gn_getml”“//www.tatmou.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 "混合器内保险丝熔断"“电子故障”“低”“更换部件”“东西不断从传送带上掉下来。”“机械故障”“低”“重新调整机

的文本数据提取event_narrative列并查看前几个字符串。

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

从多个文件提取文本

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

为示例十四行诗文本文件创建文件数据存储。示例文件名为“exampleSonnetN.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金宝appf" "tiff" "wav" "flac" "ogg" "mp4" "m4a"] ReadFcn: @extractFileText AlternateFileSystemRoots: {}

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

str = [];hasdata(fds) textData = read(fds);str = [str;textData];结束

查看提取的文本。

str
str =5×1的字符串“从最公平的生物我们渴望增加,从而从而玫瑰可能永远不会死,但随着涟漪应该随着时间的评价,他的温柔继承人可能会忍受他的记忆:↵,但是你,签约自己明亮的眼睛,↵饲料'ST Thy Light的火焰与自我大量的燃料,↵在丰富的地方撒谎,↵你的自我你的敌人,对你的甜蜜自我太残忍:↵你现在是世界上新鲜的装饰,♪和只有先驱到俗丽的春天,↵在你自己的萌芽中,↵和温柔的Churl Mak'st污染物中的浪费:↵怜悯世界,否则这个贪吃,↵在坟墓和你的坟墓。““当四十冬季应该围攻你的眉头,↵和挖掘你的美丽的领域的深沟,你现在所凝视的年轻人骄傲的衣服,♥将是一个破烂的杂草的小值得举行:↵然后被问到,你的所有人都被问到了秀丽谎言,↵在你的愚蠢的日子的所有宝藏;↵在你自己深沉的沉没的眼睛里,♥都是一个饮食耻辱,而且填近的赞美。你可以回答'这个公平的小孩↵↵应该总结一下我的伯爵,并通过继承的借口来总结一下,并通过继承来证明他的美丽!♥这是新的,当你艺术老,↵看到你的血液温暖感到寒冷。““看看你的玻璃,告诉你的脸,你现在应该形成另一个人的时间;♥谁的清新修复如果你现在不早更新,那么你渴望世界,不遵循一些母亲。谁的失败谁欺骗了你的耕作?她的素质的4月“不可能的可爱,为什么要花费你的自我遗产?↵自然的贝斯斯特没有给出任何东西,但是借出,♥和坦率,她借给那些有空的人:↵然后,美丽的黑鬼,为什么要滥用的,为什么要滥用吉盛欲绝的是你的利润?为什么要使用它这么伟大的总和,但才能没有活? 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…"

另请参阅

|||

相关的话题