对于仅限医学相关术语搜索维基百科XML转储感兴趣

2024-04-20 13:28:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我想自动定义医学术语。然而,标准的医学词典和WordNet还不够。因此我使用维基百科语料库来代替。然而,当我下载enwiki-latest-pages-articles.xml(顺便说一下,它以“无政府主义”一词开头——为什么不下载“AA”之类的东西呢?)由于文件的大小,我立即用grep失败,于是开始在线查找。我发现了我认为已经为此编写的库,比如Perl的MediaWiki::DumpFile(我确实知道一些Perl,但我更喜欢Python,因为我的脚本是用Python编写的),但看起来他们中的大多数人都创建或需要某种数据库(我只想(尽管模糊地)匹配一个单词并抓取其介绍性段落的前几句话;例如,搜索“salmonella”会返回:

Salmonella /ˌsælməˈnɛlə/ is a genus of rod-shaped (bacillus) bacteria of the Enterobacteriaceae family. There are only two species of Salmonella, Salmonella bongori and Salmonella enterica, of which there are around six subspecies and innumerable serovars. The genus Escherichia, which includes the species E.coli belongs to the same family.Salmonellae are found worldwide in both cold-blooded and warm-blooded animals, and in the environment. They cause illnesses such as typhoid fever, paratyphoid fever, and food poisoning.[1].

就我的目的而言(只是将其作为一种术语表使用),这些脚本是我想要的吗(如果没有示例,我发现文档很难理解)?例如,我想:

  1. 为了减少搜索内容,删除所有与医学无关的内容(我用category过滤器进行了尝试,因为Wikipedia允许导出特定的类别,但是它们并不是我想要的那样工作;例如,'Medicine'只返回大约20页,所以我更愿意以某种方式处理xml文件)。

  2. 允许我的Python脚本快速搜索Wikipedia语料库(例如,如果我想匹配CHOLERAE),我希望它能把我带到Vibrio cholerae的定义,就像Wikipedia搜索函数一样(带我到顶部选项)。我已经写了一种搜索引擎,可以做到这一点,但它会很慢与这么大的文件(40 GB)。

提前为一个很天真的问题道歉。在


Tags: and文件ofthe脚本定义xmlwikipedia
1条回答
网友
1楼 · 发布于 2024-04-20 13:28:33

这里有一种方法可以在不下载全部内容的情况下查询Wikipedia数据库。在

import requests
import argparse

parser = argparse.ArgumentParser(description='Fetch wikipedia extracts.')
parser.add_argument('word', help='word to define')
args = parser.parse_args()

proxies = {
    # See http://www.mediawiki.org/wiki/API:Main_page#API_etiquette
    # "http": "http://localhost:3128",
}

headers = {
    # http://www.mediawiki.org/wiki/API:Main_page#Identifying_your_client
    "User-Agent": "Definitions/1.0 (Contact rob@example.com for info.)"
}

params = {
    'action':'query',
    'prop':'extracts',
    'format':'json',
    'exintro':1,
    'explaintext':1,
    'generator':'search',
    'gsrsearch':args.word,
    'gsrlimit':1,
    'continue':''
}

r = requests.get('http://en.wikipedia.org/w/api.php',
                 params=params,
                 headers=headers,
                 proxies=proxies)
json = r.json()
if "query" in json:
    result = json["query"]["pages"].items()[0][1]["extract"]
    print result.encode('utf-8')
else:
    print "No definition."

以下是一些结果。注意即使单词拼写错误,它仍然返回结果。在

^{pr2}$

相关问题 更多 >