主要内容

从文件中提取文本数据

这个例子展示了如何从文本、HTML、Microsoft®Word、PDF、CSV和Microsoft Excel®文件中提取文本数据,并将其导入MATLAB®进行分析。

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

文本文件

将文本从sonnets.txt使用extractFileText.档案sonnets.txt收录了莎士比亚的十四行诗。

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

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

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

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

文件名=“multilineSonnets.txt”;str=读取行(文件名)
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在Microsoft Word文档中包含莎士比亚的十四行诗。

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

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

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

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

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

PDF文件

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

PDF文件

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

文件名=“示例十四行诗.pdf”;str = extractFileText(文件名);

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

开始=“三世”+换行符=“四”;十四行诗3=extractBetween(str、start、fin)
十四行诗3=“看着你的镜子,告诉你现在看到的那张脸是时候让那张脸变成另一张了;如果你现在不更新,你就欺骗了世界,欺骗了一些母亲。因为她在哪里如此美丽,她未出生的子宫蔑视你的耕种?还是他如此钟爱的人会成为他的坟墓,阻止他自爱后代?你是你母亲的镜子,她在你心中唤回她青春的四月;因此,透过你这个年龄的窗户,你将看到,尽管这是你的黄金时代。但如果你活着,记住你不该单身,你的形象也会随着你一起死去。"

PDF表单

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

文件名=“weatherReportForm1.pdf”;data = readPDFFormData(文件名)
data =带字段的结构:event_type:“雷暴风”event_narrative:“大花园之间的大树和塔特勒顿之间。”

超文本标记语言

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

HTML文件

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

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

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

Start = newline +“四”+换行符=newline +“V”+换行;sonnet4 = extractBetween (str,开始,翅片)
十四行诗4=“无情的可爱,你为什么把你美丽的遗产花在自己身上?大自然的遗赠什么也不给,只是借给人,而且她坦率地借给那些自由的人:那么,美丽的吝啬鬼,你为什么滥用给予你的慷慨大方呢?无利可图的高利贷者,你为什么用这么多的钱却活不下去?因为你独自与自己来往,你的自我你甜蜜的自我欺骗了你:那么,当大自然召唤你离开时,你还能留下什么可以接受的审计呢?你那未被利用的美必须与你同床,而你的美,曾经是你的遗嘱执行人。"

HTML代码

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

代码="

THE十四行诗

by William Shakespeare

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

从网上

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

url =“//www.tatmou.com/help/textanalytics”;代码= webread (url);str = extractHTMLText(代码)
str='文本分析工具箱™ 提供用于预处理、分析和建模文本数据的算法和可视化。使用工具箱创建的模型可用于情绪分析、预测性维护和主题建模等应用程序。文本分析工具箱包括用于处理来自设备日志、新闻提要、调查、操作等源的原始文本的工具ator报告和社交媒体。您可以从流行的文件格式中提取文本,预处理原始文本,提取单个单词,将文本转换为数字表示,并构建统计模型。使用机器学习技术(如LSA、LDA和单词嵌入),您可以从高维文本数据中查找群集并创建特征ets。使用文本分析工具箱创建的功能可以与其他数据源的功能相结合,以构建利用文本、数字和其他类型数据的机器学习模型。”

解析HTML代码

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

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

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

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

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

str(1:10)
ans=10×1的字符串“跳过内容" "" "产品”“解决方案”“学下载188bet金宝搏术界”“支持”“社区”金宝搏官方网站“活动”“联系我们”“获取MATLAB”金宝app

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

attr =“href”;str=getAttribute(子树(1:10),attr)
str =10×1的字符串“#内容容器”//www.tatmou.com?s_tid=gn_logo" "//www.tatmou.com/products.html?s下载188bet金宝搏_tid=gn_ps" "//www.tatmou.com/solutions.html?金宝搏官方网站s_tid=gn_sol" "//www.tatmou.com/academia.html?s_tid=gn_acad" "https://www.mathworks.c金宝appom/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/company/aboutus/contact_us.html?s_tid=gn_cntus" "//www.tatmou.com/products/get-matlab.html?s_tid=gn_getml"

CSV和Microsoft Excel文件

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

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

T=可读性(“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,将此函数输入到文件数据存储使用函数句柄。

位置= fullfile (matlabroot,“例子”“文本分析”“数据”“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月;所以你通过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…"

另请参阅

|||

相关的话题