主要内容

从文件中提取文本数据

这个例子展示了如何从文本中提取文本数据,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(文件名);

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

开始=“我”+换行;鳍=“二世”;sonnet1 = extractBetween (str,开始,翅片)
sonnet1 = "从美丽的生物,我们渴望增加,从而美丽的玫瑰可能永远不会死,但随着成熟应该随着时间流逝,他的他的记忆交给娇嫩的后嗣:但你承包你的明亮的眼睛,饲料刻画你的光的火焰定情,燃料,饥荒丰富所在,你的自我你的敌人,你的甜蜜的自我太残忍:你现在艺术世界的新鲜的点缀,只有春天的华而不实的使者,在你自己的花蕾埋葬你的内容,和温柔的乡下人mak浪费在吝啬鬼:遗憾的世界,否则这贪吃的人,吃的世界,由你和坟墓。”

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

文件名=“multilineSonnets.txt”;str = readline(文件名)
str =3×1的字符串“从美丽的生物我们欲望增加,因此美丽的玫瑰可能永远不会死,但随着成熟应该随着时间流逝,他的他的记忆交给娇嫩的后嗣:但你承包你的明亮的眼睛,给花光的火焰定情,燃料,饥荒丰富所在,你的自我你的敌人,你的甜蜜的自我太残忍:你现在艺术世界的新鲜的点缀,只有春天的华而不实的使者,在你自己的花蕾埋葬你的内容,和温柔的乡下人mak浪费在吝啬鬼:遗憾的世界,否则这贪吃的人,吃世界上,由你和坟墓。””When forty winters shall besiege thy brow, And dig deep trenches in thy beauty's field, Thy youth's proud livery so gazed on now, Will be a tatter'd 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 all-eating shame, and thriftless praise. How much more praise deserv'd thy beauty's 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 feel'st 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 unear'd womb Disdains the tillage of thy husbandry? 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 = "当四十冬天围攻你的眉毛,你的美丽的领域深入挖掘战壕,你青春的骄傲制服现在凝视着,将小价值的碎布会杂草:然后被要求,所有你的美丽谎言,所有的珍惜你的精力充沛的天;说,在你自己的深凹的眼睛,是一个所有吃的羞耻和浪费的赞美。多少deserv会赞美你的美丽的,如果你能够回答“这个公平的孩子我要和我的统计,并使我的借口,“证明他继承你的美丽!这是新的当你老了,看看你的血温暖当君觉得冷。”

Microsoft Word文档的例子使用了两个换行字符每一行之间。用一个换行符,来取代这些角色使用取代函数。

sonnet2 =取代(sonnet2换行换行,换行符)
sonnet2 = "当四十冬天围攻你的眉毛,你的美丽的领域深入挖掘战壕,你青春的骄傲制服现在凝视着,将小价值的碎布会杂草:然后被要求,所有你的美丽谎言,所有的珍惜你的精力充沛的天;说,在你自己的深凹的眼睛,是一个所有吃的羞耻和浪费的赞美。多少deserv会赞美你的美丽的,如果你能够回答“这个公平的孩子我要和我的统计,并使我的借口,“证明他继承你的美丽!这是新的当你老了,看看你的血温暖当君觉得冷。”

PDF文件

从PDF文件中提取文本和数据从PDF表单。

PDF文档

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

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

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

开始=“三世”+换行;鳍=“四”;sonnet3 = extractBetween (str,开始,翅片)
sonnet3 = "看你的玻璃和告诉你面对视图现在面临应该形成另一个;新鲜的修复如果现在你不是17,你欺骗世界,unbless一些母亲。她那么公平的unear子宫会鄙视你饲养的耕作?或者他是谁所以喜欢将坟墓,他的自恋停止后人?你是你母亲的玻璃,她的可爱的4月她'你打电话;所以通过窗户你年龄必看,尽管皱纹这你的黄金时间。但你若活着,记得不,死单死与你和你的形象。”

PDF表单

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

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

HTML

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

HTML文件

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

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

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

开始=换行符+“四”+换行;鳍=换行符+“V”+换行;sonnet4 = extractBetween (str,开始,翅片)
sonnet4 = " Unthrifty可爱,为什么你花在你的自我你美丽的遗产吗?大自然的遗赠给了什么,实则放贷,而弗兰克她借给那些都是免费的:然后,美丽的吝啬鬼,你为什么滥用慷慨的赠品给你给吗?无益的高利贷者,为什么你用很大一笔资金,但不能生活?因为交通独自与你的自我,你的自我你甜蜜的自我欺骗,那么当你内急了,接受审计你能留下什么?你未用完的美必须与你的坟墓,使用,生活的遗嘱执行人。”

HTML代码

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

代码=“< html > <身体> < h1 >十四行诗由威廉·莎士比亚< / h1 > < p > < / p > < /身体> < / html >”;str = extractHTMLText(代码)
str =“威廉·莎士比亚的十四行诗”

从网上

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

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

解析HTML代码

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

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

查看前十子树和提取文本使用extractHTMLText

子树(1:10)
ans = 10×1 htmlTree: < class = " skip_link sr-only " href = " # skip_link_anchor " >跳到内容< / > < A href = " //www.tianjin-qmedu.com?s_tid = gn_logo " class = " svg_link navbar-brand " > < IMG src = " /图片/响应/全球/ pic-header-mathworks-logo。svg”类= " mw_logo " alt = " MathWorks " / > < / > < A href = " https://www.maths manbetx 845works.com/products.html?产品s_tid = gn_s manbetx 845ps " > < / > < A href = " https://www.mathworks.万博 尤文图斯com/solutions.html?s_tid = gn_sol万博 尤文图斯 " >解决方案< / > < A href = " //www.tianjin-qmedu.com/academia.html?学术界s_tid = gn_acad " > < / > < A href = " https://www.mathworks万博1manbetx.com/support.html?s_tid支持= gn_sup万博1manbetxp " > < / > < A href = " //www.tianjin-qmedu.com/matlabcentral/?社区s_tid = gn_mlc " > < / > < A href = " //www.tianjin-qmedu.com/company/events.html?事件s_tid = gn_ev " > < / > < A href = " https://www.mathworks manbetx 845s.com/products/get-matlab.html?s_tid = gn_getml " >得到MATLAB < / > < A href = " //www.tianjin-qmedu.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的字符串“跳到内容”“”“产品”“解决方案”“学术界s manbetx 845”“支持”“社会”“事万博 尤文图斯件”“把MATLAB”“万博1manbetx

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

attr =“href”;str = getAttribute(子树(1:10),attr)
str =10×1的字符串“# skip_link_anchor”“//www.tianjin-qmedu.com?s_tid = gn_logo " https://www.mathworks.cos manbetx 845m/products.html?s_tid = gn_ps " https://www.mathworks.co万博 尤文图斯m/solutions.html?s_tid = gn_sol " //www.tianjin-qmedu.com/academia.html?s_tid = gn_acad " https://www.mathworks.co万博1manbetxm/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 " https://www.mathworks.cos manbetx 845m/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)
类别描述紧急解决成本_____________________________________________________________________ ____________________ ________ ____________________ _____”项目是偶尔陷入扫描仪卷。””Mechanical Failure" "Medium" "Readjust Machine" 45 "Loud rattling and banging sounds are coming from assembler pistons." "Mechanical Failure" "Medium" "Readjust Machine" 35 "There are cuts to the power when starting the plant." "Electronic Failure" "High" "Full Replacement" 16200 "Fried capacitors in the assembler." "Electronic Failure" "High" "Replace Components" 352 "Mixer tripped the fuses." "Electronic Failure" "Low" "Add to Watch List" 55 "Burst pipe in the constructing agent is spraying coolant." "Leak" "High" "Replace Components" 371 "A fuse is blown in the mixer." "Electronic Failure" "Low" "Replace Components" 441 "Things continue to tumble off of the belt." "Mechanical Failure" "Low" "Readjust Machine" 38

提取的文本数据event_narrative列和视图最初几个字符串。

str = T.Description;str (1:10)
ans =10×1的字符串“项目是偶尔陷入扫描仪卷。””Loud rattling and banging sounds are coming from assembler pistons." "There are cuts to the power when starting the plant." "Fried capacitors in the assembler." "Mixer tripped the fuses." "Burst pipe in the constructing agent is spraying coolant." "A fuse is blown in the mixer." "Things continue to tumble off of the belt." "Falling items from the conveyor belt." "The scanner reel is split, it will soon begin to curve."

从多个文件中提取文本

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

创建一个数据存储的文件示例十四行诗的文本文件。示例文件被命名为“exampleSonnetN.txt”,N十四行诗的数量。使用通配符“*”指定文件名找到这个结构的所有文件名。指定阅读功能extractFileText这个函数,输入fileDatastore使用一个函数处理。

位置=“exampleSonnet * . txt”;fds = fileDatastore(位置,“ReadFcn”,@extractFileText);

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

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

视图提取的文本。

str
str =4×1的字符串“从美丽的生物我们欲望增加,↵从而美丽的玫瑰可能永远不会死,↵但随着成熟应该随着时间流逝,他他的记忆交给娇嫩的后嗣↵:↵但你承包你的明亮的眼睛,↵饲料刻画你的光的火焰定情,燃料,↵饥荒丰富所在,↵你的自我你的敌人,你的甜蜜的自我太残忍:↵你现在艺术世界的新鲜的点缀,↵只有春天的华而不实的使者,↵在你自己的花蕾埋葬你的内容,↵温柔乡下人mak浪费在吝啬鬼:↵遗憾的世界,否则这贪吃的人,↵吃世界,由你和坟墓。””When forty winters shall besiege thy brow,↵ And dig deep trenches in thy beauty's field,↵ Thy youth's proud livery so gazed on now,↵ Will be a tatter'd 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 all-eating shame, and thriftless praise.↵ How much more praise deserv'd thy beauty's 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 feel'st 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 unear'd womb↵ Disdains the tillage of thy husbandry?↵ 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." " Unthrifty loveliness, why dost thou spend↵ Upon thy self thy beauty's legacy?↵ Nature's 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."

另请参阅

||||

相关的话题