HTML文档操作
HTML文档的解析提取等操作主要使用的是NLTK工具包中的许多函数。
实验流程如下:
- 使用open函数打开一个HTML文档;
- 使用BeautifulSoup中的get_text函数清洗提取文档的文本正文;
- 使用nltk中的regexp_tokenize函数对清洗过后的正文提取标记;
- 使用nltk中的stopwords对提取后的token删除英文中的停用词;
- 使用nltk中的FreqDist对清洗过的token统计输出出现频率最高的前20个词;
- 使用nltk中的PorterStemmer对清洗后的某一个token进行词干提取;
- 使用nltk中的WordNetLemmatizer对清洗后的某一个token进行词形还原;
实验代码
from bs4 import BeautifulSoup
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.stem import WordNetLemmatizer
class HTML():
def __init__(self, file_path):
self.file_path = file_path
def read_file(self):
# 读取HTML文件
response = open(self.file_path, 'r', encoding="utf-8")
html = response.read()
return html
def cl