这是用于PHP的网络文章解析和语言检测库。该库从网页读取文章内容,删除所有HTML并仅提供原始文本,适用于语音或机器学习过程的文本。
对于我开发的项目,我发现了许多现有的开源解决方案良好的起点,但是每个开源解决方案都有独特的失败。该库将三种不同的方法汇总到单个解决方案中,同时添加语言检测的其他功能。
该库是通过packagist.org分发的,因此您可以使用作曲家检索依赖关系
composer require crscheid/php-article-extractor
该库将尝试为您检索HTML。您只需创建一个ArtendExtractor类,并在其上调用parseURL函数,传递所需的URL。
use Cscheide ArticleExtractor ArticleExtractor ;
$ extractor = new ArticleExtractor ();
$ response = $ extractor -> processURL ( " https://www.fastcompany.com/3067246/innovation-agents/the-unexpected-design-challenge-behind-slacks-new-threaded-conversations " );
var_dump ( $ response );函数processURL返回包含与请求关联的标题,文本和元数据的数组。如果文本为null ,则表示解析失败。以下应该是上述代码的输出。
如果库遵循重定向,则字段result_url将有所不同。该字段表示重定向后实际检索的最后一页。
array(5) {
["parse_method"]=>
string(11) "readability"
["title"]=>
string(72) "The Unexpected Design Challenge Behind Slack’s New Threaded Conversations"
["text"]=>
string(8013) "At first blush, threaded conversations sound like one of the most thoroughly mundane features a messaging app could introduce.After all, the idea of neatly bundling up a specific message and its replies in ..."
["language_method"]=>
string(7) "service"
["language"]=>
string(2) "en"
["result_url"]=>
string(126) "https://www.fastcompany.com/3067246/innovation-agents/the-unexpected-design-challenge-behind-slacks-new-threaded-conversations"
}
如果您已经拥有HTML,则可以使用parseHTML功能,并使用通过相同逻辑处理的HTML。
use Cscheide ArticleExtractor ArticleExtractor ;
$ extractor = new ArticleExtractor ();
$ myHTML = <load from some source>;
$ response = $ extractor -> processHTML ( $ myHTML );
var_dump ( $ response );该函数parseHTML返回包含与请求关联的标题,文本和元数据的数组。如果文本为null ,则表示解析失败。以下应该是上述代码的输出。
在这种情况下,将不包括字段result_url ,因为我们在过程呼叫过程中没有尝试获取HTML。
array(5) {
["parse_method"]=>
string(11) "readability"
["title"]=>
string(72) "The Unexpected Design Challenge Behind Slack’s New Threaded Conversations"
["text"]=>
string(8013) "At first blush, threaded conversations sound like one of the most thoroughly mundane features a messaging app could introduce.After all, the idea of neatly bundling up a specific message and its replies in ..."
["language_method"]=>
string(7) "service"
["language"]=>
string(2) "en"
}
您还可以通过传递语言检测服务的密钥以及自定义的用户代理字符串来创建ArticleExtractor类。请参阅下面的更多信息。
语言检测是通过在HTML元数据中寻找语言指定符或使用检测语言服务来处理语言检测。
如果可以检测文章的语言,则分别在字段language和language_method中返回ISO 639-1格式中的语言代码以及检测方法。如果成功发现的language_method字段可以是html或service 。
如果语言检测失败或不可用,则这两个字段都将作为空返回。
检测语言需要使用可以注册的API键。但是,您也可以不用它使用此库。如果HTML元数据不包含有关文章语言的信息,则language和language_method将以null值返回。
要利用此库利用语言检测服务,请通过传递API键以检测语言来创建ArticleExtractor对象。
use Cscheide ArticleExtractor ArticleExtractor ;
$ extractor = new ArticleExtractor ( ' your api key ' );可以为传出请求设置用户代理。为此
use Cscheide ArticleExtractor ArticleExtractor ;
$ myUserAgent = " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36 " ;
$ extractor = new ArticleExtractor ( null , $ myUserAgent );可以强制尝试使用我们的自定义处理的可读性,鹅或鹅尝试阅读的方法。如果可读性或鹅在特定的网站上存在特定问题,这可能会派上用场。
要强制该方法,只需向构造函数提供第三个论点。这四种有效的方法是readability , goose , goosecustom或custom 。
$ extractor = new ArticleExtractor ( null , null , " goose " );从版本1.0开始,已经更改了输出格式,以提供标题的新线路中断。这对于确定句子边界时的自然语言处理应用非常重要。如果不需要此行为,只需在需要的地方剥离其他新线即可。
这一更改的原因是,当标题和段落html元素被简单地删除时,通常会出现标题和诉讼句子之间没有分离的问题。
文本字段输出格式的示例
n
A database containing 250 million Microsoft customer records has been found unsecured and onlinen
NurPhoto via Getty Imagesn
A new report reveals that 250 million Microsoft customer records, spanning 14 years, have been exposed online without password protection.n
Microsoft has been in the news for, mostly, the wrong reasons recently. There is the Internet Explorer zero-day vulnerability that Microsoft hasn't issued a patch for, despite it being actively exploited. That came just days after the U.S. Government issued a critical Windows 10 update now alert concerning the "extraordinarily serious" curveball crypto vulnerability. Now a newly published report, has revealed that 250 million Microsoft customer records, spanning an incredible 14 years in all, have been exposed online in a database with no password protection.n
What Microsoft customer records were exposed online, and where did they come from?n
单位测试包含在此分布中,可以在安装依赖项后使用PHPUNIT运行。建议的方法是将Docker用于此目的,因此您甚至不需要在系统上安装依赖项。
注意:请使用您的检测语言密钥设置环境变量
DETECT_LANGUAGE_KEY,以便在单元测试中进行语言检测以正常工作。
这将使用作曲家Docker图像下载要求。请注意--ignore-platform-reqs的使用,因为我们的某些依赖项尚未支持PHP 8。
docker run --rm --interactive --tty --volume $PWD:/app composer --ignore-platform-reqs install
这运行了我们在PHP 7.4命令行环境中下载的PHPUNIT依赖性。
docker run -v $(pwd):/app -w /app -e DETECT_LANGUAGE_KEY=<yourapikey> --rm php:7.4-cli ./vendor/phpunit/phpunit/phpunit