【问题标题】:How to identify wikipedia categories in python如何在python中识别维基百科类别
【发布时间】:2019-06-28 19:46:35
【问题描述】:

我目前使用pywikibot 来获取给定维基百科页面的类别(例如support-vector machine),如下所示。

import pywikibot as pw

print([i.title() for i in list(pw.Page(pw.Site('en'), 'support-vector machine').categories())])

我得到的结果是:

[
  'Category:All articles with specifically marked weasel-worded phrases',
  'Category:All articles with unsourced statements',
  'Category:Articles with specifically marked weasel-worded phrases from May 2018',
  'Category:Articles with unsourced statements from June 2013',
  'Category:Articles with unsourced statements from March 2017',
  'Category:Articles with unsourced statements from March 2018',
  'Category:CS1 maint: Uses editors parameter',
  'Category:Classification algorithms',
  'Category:Statistical classification',
  'Category:Support vector machines',
  'Category:Wikipedia articles needing clarification from November 2017',
  'Category:Wikipedia articles with BNF identifiers',
  'Category:Wikipedia articles with GND identifiers',
  'Category:Wikipedia articles with LCCN identifiers'
]

如您所见,我得到的结果包括许多维基百科的跟踪和维护类别,例如:

  • 类别:所有带有明确标记的黄鼠狼短语的文章
  • 分类:所有带有未来源陈述的文章
  • 类别:CS1 维护:使用编辑器参数

但是,我只感兴趣的类别是;

  • 类别:分类算法
  • 类别:统计分类
  • 类别:支持向量机

我想知道是否有办法获取所有 tracing or maintenance 维基百科类别,以便我可以从结果中删除它们以仅获取信息类别。

或者,如果有任何其他方法可以从结果中消除它们,请建议我。

如果需要,我很乐意提供更多详细信息。

【问题讨论】:

  • tracingmaintenance 是您正在使用的实际库还是您自己的术语?如果库不提供进一步的类别识别,您可以简单地根据列表理解中的已知关键字进行过滤。例如。 [cat for cat in categories if not any(exclude_keyword in cat for exclude_keyword in ['disputed', 'maintenance', ...])]
  • @jpriebe 非常感谢您的评论。实际上trackingmaintenance 是我从维基百科中摘录的(看看维基百科的这个链接:en.wikipedia.org/wiki/…)。似乎维基百科已将它们的类别链接分类为tracking/maintenance

标签: python mediawiki pywikibot


【解决方案1】:

pywikibot 目前不提供部分API features 用于过滤隐藏类别。您可以通过在categoryinfo 中搜索hidden 键来手动执行此操作:

import pywikibot as pw

site = pw.Site('en', 'wikipedia')
print([
    cat.title()
    for cat in pw.Page(site, 'support-vector machine').categories()
    if 'hidden' not in cat.categoryinfo
])

给予:

['Category:Classification algorithms', 
 'Category:Statistical classification', 
 'Category:Support vector machines']

请参阅https://www.mediawiki.org/wiki/Help:Categories#Hidden_categorieshttps://en.wikipedia.org/wiki/Wikipedia:Categorization#Hiding_categories 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2017-08-06
    相关资源
    最近更新 更多