java - 有什么方法可以只返回维基百科文章中的(干净的)文本吗?

标签 java formatting wikipedia

我的总体目标是只返回维基百科文章中没有任何标记的干净句子。显然,有返回 JSON、XML 等的方法,但这些都是标记。到目前为止,我最好的方法是返回 Wikipedia 所称的 raw。例如,以下链接返回页面“Iron Man”的 raw 格式:

http://en.wikipedia.org/w/index.php?title=Iron%20Man&action=raw

这是返回内容的片段:

...//I am truncating some markup at the beginning here. 
|creative_team_month =
|creative_team_year =
|creators_series =
|TPB =
|ISBN =
|TPB# =
|ISBN# =
|nonUS =
}}
'''Iron Man''' is a fictional character, a [[superhero]] that appears in\\
[[comic book]]s published by [[Marvel Comics]]. 
...//I am truncating here everything until the end. 

我坚持使用 raw 格式,因为我发现它最容易清理。虽然到目前为止我用 Java 编写的内容很好地解决了这个问题,但仍有很多情况会被忽视。这些案例包括维基百科时间轴标记、维基百科图片和其他未出现在所有文章中的维基百科属性。同样,我正在使用 Java(特别是,我正在开发 Tomcat Web 应用程序)。

问题:是否有更好的方法从维基百科文章中获取清晰、人类可读的句子?也许有人已经为此建立了一个我找不到的库?

如果不清楚,我很乐意编辑我的问题,以详细说明我所说的干净和人类可读的含义。

我当前清理 raw 格式文本的 Java 方法如下:

public String cleanRaw(String input){
    //Next three lines attempt to get rid of references.
    input= input.replaceAll("<ref>.*?</ref>","");
    input= input.replaceAll("<ref .*?</ref>","");
    input= input.replaceAll("<ref .*?/>","");

    input= input.replaceAll("==[^=]*==", "");
    //I found that anything between curly braces is not needed. 
    while (input.indexOf("{{") >= 0){
        int prevLength= input.length();
        input= input.replaceAll("\\{\\{[^{}]*\\}\\}", "");
        if (prevLength == input.length()){
            break;
        }
    }
    //Next line gets rid of links to other Wikipedia pages.
    input= input.replaceAll("\\[\\[([^]]*[|])?([^]]*?)\\]\\]", "$2");
    input= input.replaceAll("<!--.*?-->","");
    input= input.replaceAll("[^A-Za-z0-9., ]", "");

    return input;
}

最佳答案

我找到了几个可能有帮助的项目。您可以通过在 Java 代码中包含 Javascript 引擎来运行第一个。

txtwiki.js 将 MediaWiki 标记转换为纯文本的 javascript 库。 https://github.com/joaomsa/txtwiki.js

维基提取器 从维基百科数据库转储中提取和清理文本的 Python 脚本 http://medialab.di.unipi.it/wiki/Wikipedia_Extractor

来源: http://www.mediawiki.org/wiki/Alternative_parsers

关于java - 有什么方法可以只返回维基百科文章中的(干净的)文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20339240/

相关文章:

Java String For循环不读取if语句

java - 在 drl 文件中定义集合

java - 格式化输出文件的问题

api - 我怎样才能像通过 Mediawiki API 获取信息一样阅读维基百科转储文件?

java - Java 的动态代理的.Net 等价物是什么?

Java SNI 无法识别的名称

mysql - 更改MySQL中的数字格式(欧洲格式)

language-agnostic - 发送包含图片的 HTML 邮件的正确方法 : Use Server or Embedded images?

sql - 按 pageid 的维基百科页面到页面链接

api - 维基百科的 MediaWiki api - 是否可以按标题搜索所有语言?