单词词义、词性、例句查询python代码

该代码使用Python的requests和beautifulsoup4库实现从有道词典批量获取单词的发音、词义、词性和例句,并将结果保存到words.txt文件中。用户可在命令行输入多个单词,用斜杠分隔,程序会依次查询并显示结果。
摘要由CSDN通过智能技术生成
单词发音、词义、词性、例句查询、输出结果更简洁,一次可查多个单词

运行该代码,命令窗口输入单词,单词用“/”分开,例如:noisy/problem/community/neighbor 可以更多。先安装两个python包requests、 beautifulsoup4,前者爬取网页内容,后者分析提取需要的内容。拷贝这一行命令,在命令窗口(终端)里运行,需联网

pip install requests beautifulsoup4 -i http://pypi.douban.com/simple
import requests
from bs4 import BeautifulSoup
def query(word):
    # 获取有道词典网页的HTML代码
    url = 'http://dict.youdao.com/w/%s' % word
    html = requests.get(url).text

    # 使用BeautifulSoup解析HTML代码,并获取单词读音、词义和词性及例句
    soup = BeautifulSoup(html, 'html.parser')
    phonetic = soup.find(class_='phonetic').get_text().strip()   # 获取单词的读音
    trans_container = soup.find(id='phrsListTab')   # 获取词义和例句的容器
    trans = trans_container.find(class_='trans-container').get_text().strip() # 获取词义
    examples = soup.find_all(class_='examples')  # 获取例句

    # 输出结果
    with open('words.txt', 'a+', encoding="UTF-8") as f:
        f.write(word+"\n")
        f.write(phonetic+"\n")
        f.write(trans+"\n")
        for example in examples:
            f.write(example.get_text().strip()+"\n")

    print('%s %s' % (word, phonetic))
    print(trans)
    print("-" * 30)
    print("例句:")
    for example in examples:
        print(example.get_text().strip())

 #输入要查询的英语单词
Input_Word = input("请输入要查询的单词(单词用'/'分开):")
list_word = Input_Word.split('/')
for lst in list_word:
    query(lst)

查询结果:
noisy
[ˈnɔɪzi]
adj. 嗓门大的,聒噪的;嘈杂的,充满噪音的;吵吵嚷嚷的,哗众取宠的;(尤指电子设备的)干扰,干扰信息

[
比较级
noisier或more noisy
最高级
noisiest或most noisy
]
…my noisy old typewriter.
…我的噪音大的旧打字机。
The students on the grass bank cheered noisily.
学生们在草堤上喧闹地欢呼。
It’s a noisy place with film clips showing constantly on one of the cafe’s giant screens.
这是一个嘈杂的地方,电影片断不停地在咖啡厅其中一个大屏幕上放映。
The baggage hall was crowded and noisy.
行李大厅拥挤而嘈杂。
It might, at last, silence the small but noisy intellectual clique.
它或许最终能让那个人数不多却哗众取宠的知识分子群体安静下来。
problem
[ˈprɒbləm]
n. 问题,难题,困难;疾病;(通过算数或仔细思考而解决的)题;(感觉上)不喜欢,不情愿;(尤指国际象棋等)棋式,排局
adj. 难对付的,成问题的

[
复数
problems
]
…the economic problems of the inner city.
…内城的经济问题。
I do not have a simple solution to the drug problem.
我并没有一个简单的解决毒品问题的方法。
With mathematical problems, you can save time by approximating.
对于数学题,你可以用近似法省时间。
community
[kəˈmjuːnəti]
n. 社区,社会;(由同国籍、同宗教等构成的)群体,界;(多个国家的)共同体;归属感;(动植物的)群落

[
复数
communities
]
He’s well liked by people in the community.
他很受社区人们的喜爱。
The police haven’t really done anything for the black community in particular.
警方并没有特别为黑人团体做什么。
Two of our greatest strengths are diversity and community.
我们的两个最大优点是多样性和团体精神。
neighbor
[ˈneɪbə®]
n. 邻居
adj. 邻近的
vi. 友好;毗邻而居
vt. 邻接
n. (Neighbor)人名;(英)内伯

[
复数
neighbors
第三人称单数
neighbors
现在分词
neighboring
过去式
neighbored
过去分词
neighbored
]

win11 python3.9  Pycharm调试通过

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Python代码实现词义分布式表示法: ```python import numpy as np import nltk from nltk.corpus import brown # 加载 Brown 语料库 nltk.download('brown') sentences = brown.sents() # 构建单词词袋 word_bag = set() for sentence in sentences: for word in sentence: word_bag.add(word) # 构建单词-索引映射 word2index = {} for i, word in enumerate(word_bag): word2index[word] = i # 构建单词共现矩阵 co_matrix = np.zeros((len(word_bag), len(word_bag))) window_size = 2 for sentence in sentences: for i, word in enumerate(sentence): for j in range(max(0, i - window_size), min(len(sentence), i + window_size + 1)): if i != j: co_matrix[word2index[word]][word2index[sentence[j]]] += 1 # 使用 SVD 进行降维 U, S, V = np.linalg.svd(co_matrix) # 取前 100 个特征向量作为词义表示 word_embeddings = U[:, :100] # 将单词和其对应的词义表示存储到字典中 word2embedding = {} for word, index in word2index.items(): word2embedding[word] = word_embeddings[index] ``` 这段代码的流程如下: 1. 加载 Brown 语料库。 2. 构建单词词袋,即所有出现过的单词的集合。 3. 构建单词-索引映射,将每个单词映射到一个唯一的整数索引。 4. 构建单词共现矩阵,其中第 i 行第 j 列的元素表示单词 i 和单词 j 在所有句子中共同出现的次数。 5. 使用 SVD 进行降维,将单词共现矩阵从高维空间降到低维空间,得到每个单词词义表示。 6. 将单词和其对应的词义表示存储到字典中,在实际应用中可以将其存储到数据库中以便快速查询。 需要注意的是,这只是一个简单的实现,实际应用中可能需要进行更多的优化和调整,例如使用更大的语料库、调整窗口大小和特征数量等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值