這是用於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