Skip to Content
Python和NLTK实现自然语言处理
book

Python和NLTK实现自然语言处理

by Posts & Telecom Press, Nitin Hardeniya
February 2024
Intermediate to advanced
649 pages
9h 58m
Chinese
Packt Publishing
Content preview from Python和NLTK实现自然语言处理

第9章 解析特定的数据类型

本章将介绍以下内容。

  • 使用dateutil解析日期和时间。
  • 时区的查找和转换。
  • 使用LXML从HTML中提取URL。
  • 清洁和剥离HTML。
  • 使用BeautifulSoup转换HTML实体。
  • 检测和转换字符编码。

本章介绍如何解析特定类型的数据,主要关注日期、时间和HTML。幸运的是,有许多有用的库能够做到这一点,因此我们不必深入了解棘手和过于复杂的正则表达式。以下这些库可以成为NLTK重要的补充。

  • dateutil提供日期时间解析和时区转换。
  • lxml和BeautifulSoup可以解析、清理和转换HTML。
  • charade和UnicodeDammit可以检测并转换文本字符编码。

在将文本传给NLTK对象前,这些库可以用于预处理文本,或者,对于已经使用NLTK处理提取过的文本,进行后期处理。即将到来的示例使用了其中的许多工具。

比如,你需要解析有关餐馆的博客文章。可以使用lxml或BeautifulSoup提取文章文本、外部网站的链接,以及写文章时的日期和时间。使用dateutil,可以将日期和时间解析为Python的datetime对象。一旦得到了文章文本,就可以使用charade确保在清理HTML、进行基于NLTK的词性标注、组块提取和文本分类(以创建关于文章的额外元数据)之前,这是UTF-8格式的。现实世界中的文本处理往往不仅仅需要基于NLTK的自然语言处理,本章介绍的功能可以有助于满足这些额外的要求。

如果需要使用Python解析日期和时间,那么没有比dateutil更好的库了。parser模块可以解析datetime字符串,它所使用的格式比此处所显示的格式还要多,同时tz模块提供了查找时区所需的一切。当结合这两个模块的时候,这些模块可以很容易解析字符串为可识别时区的datetime对象。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java持续交付

Java持续交付

Daniel Bryant, Abraham Marín-Pérez
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
软件开发实践:项目驱动式的Java开发指南

软件开发实践:项目驱动式的Java开发指南

Raoul-Gabriel Urma, Richard Warburton
Spark机器学习实战

Spark机器学习实战

Posts & Telecom Press, Siamak Amirghodsi, Meenakshi Rajendran, Broderick Hall, Shuen Mei

Publisher Resources

ISBN: 9781835083451