自然语言处理作业A1

这篇博客介绍了自然语言处理的作业,包括将HTML格式转化为JSON数据并利用NLTK的word_tokenize函数处理文本,查找CMU字典中的押韵词并添加发音,以及实现一个启发式方法判断诗歌的押韵。作业中提到,要处理HTML中的非标准标签,分离带连字符的单词,并在转换为JSON时保持可读性。此外,还探讨了未在CMU字典中找到的押韵词数量以及启发式押韵判断的不完美之处。
摘要由CSDN通过智能技术生成

作业地址
参考资料

  1. 任务1:把HTML格式转为JSON数据,再用python的JSON包,把JSON数据转为python能使用的数据结构(dicts, lists…)(chaos2json.py

Your implementation should have at least one regular expression (to extract the textual content of each line), and use NLTK’s word_tokenize function as the tokenizer. You may also use built-in string methods/operations and write your own helper functions.
The word_tokenize function does not separate hyphens, but this text uses hyphens in place of dashes, so your code should separate them.

Hint 1: The HTML contains (nonstandard) tags like at the beginning of each line. The number is the line within the stanza (between 1 and 4). Ignore ellipsis lines indicating removed stanzas.
Hint 2: When converting to JSON, use the indent argument to make it more human-readable.
(This script should not take extremely long to implement, but it will probably take you longer than you expect.)

from urllib import request
from bs4 import BeautifulSoup
from nltk import word_tokenize
import re
import json

url = 'file:///E:/学习文档/数据集/a1/chaos.html'

# 打开URL,返回HTML信息
def open_url(url):
    # 根据当前URL创建请求包
    req = request.Request(url)
    # 添加头信息,伪装成浏览器访问
    req.add_header('User-Agent',
                   'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36')
    # 发起请求
    response = request.urlopen(req)
    # 返回请求到的HTML信息
    return response.read()

# 用正则定位
def find_tag(url, regex = '<xxx.>(.*?)(<br>|</p>)'):
    # (.*?)
    # .是除了\n的任意字符
    # *是取之前字符的0个或者n个
    # ?是去之前字符的0个或者1个;也可以解释为非贪婪模式
    # ()圆括号,举例说明,eg: a(b)c
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值