自然语言处理 – 文本解析
介绍
自然语言处理 (NLP) 作为人工智能的一个分支领域,已经获得了很大的关注。它专注于使计算机能够理解和处理人类语言。一些常见的应用包括聊天机器人、情感分析、翻译、垃圾邮件分类等等。
然而,NLP 与传统机器学习任务之间存在显著差异,前者处理非结构化文本数据,而后者处理结构化表格数据。因此,在将机器学习技术应用于文本之前,有必要了解如何处理文本。这就是文本解析发挥作用的地方。
那么,什么是文本解析?简单来说,它是一种常见的编程任务,根据一些规则将给定的一系列文本分成更小的部分。其应用范围从文档解析到深度学习 NLP。
在本指南中,我们将应用 Python 中提供的丰富功能来进行文本解析。两种常用的选项是正则表达式和单词标记化。
正则表达式
正则表达式,或称 Regex,是具有特殊语法的字符串,允许我们匹配其他字符串中的模式。在 Python 中,有一个名为re 的模块可用于处理正则表达式。下面显示了一些常见的正则表达式模式及其用法:
'\d':匹配任意十进制数字;例如 5。它的变体是 '\D',它匹配任意非数字字符。
'\s':匹配任何空白字符;例如,''。它的变体是 '\S',它匹配任何非空白字符。
'\w':匹配任何字母数字字符;例如“Pluralsight”。它的变体是“\W”,它匹配任何非字母数字字符。
'+ 或 *':执行贪婪匹配。例如,'eeeeee'。
'[az]':匹配小写字母组。
'[A-Za-z]':匹配大小写英文字母。
'[0-9]':匹配从 0 到 9 的数字。
这些只是众多可用的正则表达式模式中的一小部分。在本指南的后续部分中,我们将借助示例更好地理解正则表达式。我们将从导入re模块开始,这在下面的第一行代码中完成。我们还需要一个文本对象或语料库,我们使用热门电影《复仇者联盟》的简短描述。我们将此文本存储在语料库“regex_example”中。这是在第二行代码中完成的。
import re
regex_example = "Avengers: Infinity War was a 2018 American superhero film based on the Marvel Comics superhero team the Avengers. It is the 19th film in the Marvel Cinematic Universe (MCU). The running time of the movie was 149 minutes and the box office collection was around 2 billion dollars. (Source: Wikipedia)"
print(regex_example)
输出:《复仇者联盟:无限战争》是一部 2018 年美国超级英雄电影,改编自漫威漫画超级英雄团队复仇者联盟。这是漫威电影宇宙 (MCU) 的第 19 部电影。电影放映时间为 149 分钟,票房收入约为 20 亿美元。(来源:维基百科)
常见的 Python Regex 方法
重新查找所有()
re.findall() 方法返回字符串中的所有模式。如果不匹配,则返回空列表。
下面的第一行代码从我们之前创建的文本“regex_example”中提取数字。
现在我们将处理单词并尝试找出文本中元音的数量。第二行和第三行代码执行此任务。文本中出现了 87 个元音。
假设你想找出“复仇者”这个词在语料库中使用的次数。这可以通过第四行代码实现,答案是 2。
我们还可以找出所有大写单词并打印结果,这在代码的第五行和第六行完成。输出仅包含语料库的大写单词。
print(re.findall('\d+', regex_example)) #line1
vowels = re.findall('[aeiou]', regex_example) #line2
print(len(vowels)) #line 3
print(len(re.findall('Avengers', regex_example))) #line 4
capitalwords = "[A-Z]\w+" #line 5
print(re.findall(capitalwords, regex_example)) #line 6
输出:
'2018'
重新分割()
另一个有用的方法是 re.split(),它在匹配的情况下拆分字符串。如果不匹配,它将返回一个包含空字符串的列表。
在我们的示例中,让我们应用此方法并按数字模式拆分语料库。下面的代码块执行此任务并打印输出。
print(re.split(r"\d+", regex_example))
输出:
'Avengers: Infinity War was a ', ' American superhero film based on the Marvel Comics superhero team the Avengers. It is the ', 'th film in the Marvel Cinematic Universe (MCU). The running time of the movie was ', ' minutes and the box office collection was around ', ' billion dollars. (Source: Wikipedia)'
上面的输出显示拆分是在数字上完成的。可以向 re.split() 方法添加“maxsplit”参数,该参数表示将发生的最大拆分次数。默认值为零。下面的代码使用 maxsplit 值作为 2,并且该方法仅对前几个数字进行拆分。
print(re.split(r"\d+", regex_example,2))
输出:
'Avengers: Infinity War was a ', ' American superhero film based on the Marvel Comics superhero team the Avengers. It is the ', 'th film in the Marvel Cinematic Universe (MCU). The running time of the movie was 149 minutes and the box office collection was around 2 billion dollars. (Source: Wikipedia)'
另一个应用是按空格分割语料库。以下代码可实现此目的。
print(re.split(r"\s+", regex_example))
输出: '复仇者联盟:'、'无限'、'战争'、'是'、'一部'、'2018'、'美国'、'超级英雄'、'电影'、'基于'、'the'、'漫威'、'漫画'、'超级英雄'、'团队'、'the'、'复仇者联盟。'、'它'、'是'、'the'、'19'、'电影'、'在'、'the'、'漫威'、'电影'、'宇宙'、'(MCU)。'、'The'、'正在运行'、'时间'、'of'、'the'、'电影'、'是'、'149'、'分钟'、'和'、'the'、'盒子', '办公室', '收藏', '是', '大约', '2', '十亿', '美元。', '(来源:', '维基百科)'
重新.sub()
此方法用于用 replace 变量中的内容替换匹配的文本。如果未找到模式,则返回原始字符串。
在我们的示例中,让我们将单词“Avengers”替换为“A”。下面的代码块执行此任务。
print(re.sub("Avengers", "A", regex_example))
输出:
A: Infinity War was a 2018 American superhero film based on the Marvel Comics superhero team the A. It is the 19th film in the Marvel Cinematic Universe (MCU). The running time of the movie was 149 minutes and the box office collection was around 2 billion dollars. (Source: Wikipedia)
研究()
该方法用于在字符串中搜索模式,成功则返回匹配对象,失败则返回 None。
让我们在下面的例子中理解这一点,我们将尝试查找单词“Python”是否位于句子“Scikit Learn 是一个很棒的 Python 库”的开头。在我们的例子中,由于搜索失败,输出为“无”。
example = "Scikit Learn is a great Python library"
match = re.search('\APython', example)
print(match)
输出:无
正则表达式是一个庞大的领域,不可能在一个指南中涵盖所有内容。但是,我们已经对最常用的正则表达式方法及其在 Python 中的工作有了基本的了解,这在大多数情况下都会很有用。现在让我们转向另一种重要的文本解析技术,即单词标记化。
词语标记
标记化是将文本或语料库转换为标记(较小片段)的过程。转换为标记是根据某些规则进行的。使用正则表达式,也可以创建自己的规则。标记化有助于文本预处理任务,例如映射词性、查找和匹配常用词、清理文本以及为情感分析等高级文本分析技术准备数据。
Python 有一个非常流行的自然语言工具包库,称为“nltk”,它具有一组丰富的函数,可用于执行许多 NLP 作业。它可以通过 pip 下载,也包含在 Anaconda 发行版中。
我们将使用 nltk 来研究单词标记化的常见应用。首先,我们应该导入 nltk 库。然后,我们导入 sent_tokenize 和 word_tokenize 函数。这在下面的代码中完成。
# Import necessary modules
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
现在,我们将获得一些文本。我们将使用以下文本作为示例:
textdata = "Pluralsight is the technology skills platform. It has more than 6000 courses and 1100+ employees. Pluralsight is headquartered in Utah and has over 1500 experts authoring high quality courses. Pluralsight grew 161 percent between 2014 and 2017, earning the company a spot in the Deloitte Technology Fast 500 list for five consecutive years."
print(textdata)
输出:
Pluralsight is the technology skills platform. It has more than 6000 courses and 1100+ employees. Pluralsight is headquartered in Utah and has over 1500 experts authoring high quality courses. Pluralsight grew 161 percent between 2014 and 2017, earning the company a spot in the Deloitte Technology Fast 500 list for five consecutive years.
我们现在将对此文本数据执行单词标记任务。最常见的标记形式是单词和句子标记。
下面的第一段代码将文本标记为单词并打印输出。第二段代码也进行标记,但这次是在句子上进行。
print(word_tokenize(textdata))
输出:
'Pluralsight', 'is', 'the', 'technology', 'skills', 'platform', '.', 'It', 'has', 'more', 'than', '6000', 'courses', 'and', '1100+', 'employees', '.', 'Pluralsight', 'is', 'headquartered', 'in', 'Utah', 'and', 'has', 'over', '1500', 'experts', 'authoring', 'high', 'quality', 'courses', '.', 'Pluralsight', 'grew', '161', 'percent', 'between', '2014', 'and', '2017', ',', 'earning', 'the', 'company', 'a', 'spot', 'in', 'the', 'Deloitte', 'Technology', 'Fast', '500', 'list', 'for', 'five', 'consecutive', 'years', '.'
print(sent_tokenize(textdata))
输出:
“Pluralsight 是技术技能平台。”,“它拥有 6000 多门课程和 1100 多名员工。”,“Pluralsight 总部位于犹他州,拥有 1500 多名专家编写高质量课程。”,“Pluralsight 在 2014 年至 2017 年间增长了 161%,连续五年跻身德勤技术快速 500 强榜单。”
了解“word_tokenize”和“sent_tokenize”这两个函数之间的区别非常重要。在上面的输出中,我们看到这两个函数为相同的输入(文本数据)创建了不同的输出(标记),这是因为它们的功能不同。
现在,我们将在下面的两行代码中查看使用这两种方法生成的唯一标记。在下面的第一个输出中,术语“Pluralsight”仅出现一次,因为它是单词级标记。然而,在第二个输出中,该术语出现多次,因为标记化发生在句子级别。
print(set(word_tokenize(textdata)))
输出:
{'technology', 'headquartered', '.', 'quality', '161', 'five', '2017', 'It', 'spot', 'over', 'Technology', 'skills', 'percent', 'years', '500', 'platform', 'consecutive', 'has', '1500', 'earning', ',', 'the', 'Deloitte', '1100+', 'grew', '6000', 'in', 'between', 'than', 'courses', 'Pluralsight', '2014', 'authoring', 'Utah', 'a', 'list', 'is', 'and', 'Fast', 'more', 'for', 'experts', 'high', 'employees', 'company'}
print(set(sent_tokenize(textdata)))
输出:
{'Pluralsight grew 161 percent between 2014 and 2017, earning the company a spot in the Deloitte Technology Fast 500 list for five consecutive years.', 'Pluralsight is the technology skills platform.', 'It has more than 6000 courses and 1100+ employees.', 'Pluralsight is headquartered in Utah and has over 1500 experts authoring high quality courses.'}
让我们计算使用“word_tokenize”和“sent_tokenize”这两个函数生成的唯一标记的数量,这在下面的两行代码中完成。有 45 个唯一的“单词”标记,而只有 4 个唯一的“句子”标记。
print(len(set(word_tokenize(textdata))))
print(len(set(sent_tokenize(textdata))))
输出:
免责声明:本内容来源于第三方作者授权、网友推荐或互联网整理,旨在为广大用户提供学习与参考之用。所有文本和图片版权归原创网站或作者本人所有,其观点并不代表本站立场。如有任何版权侵犯或转载不当之情况,请与我们取得联系,我们将尽快进行相关处理与修改。感谢您的理解与支持!
请先 登录后发表评论 ~