이것은 PHP 용 웹 기사 구문 분석 및 언어 탐지 라이브러리입니다. 이 라이브러리는 웹 페이지에서 기사 내용을 읽고 모든 HTML을 제거하고 음성 또는 기계 학습 프로세스에 적합한 원시 텍스트 만 제공합니다.
내가 개발 한 프로젝트의 경우 기존의 많은 오픈 소스 솔루션이 좋은 출발점을 발견했지만 각각 고유 한 실패가있었습니다. 이 라이브러리는 언어 탐지의 추가 기능을 추가하면서 세 가지 다른 접근 방식을 단일 솔루션으로 집계합니다.
이 라이브러리는 packagist.org를 통해 배포되므로 작곡가를 사용하여 종속성을 검색 할 수 있습니다.
composer require crscheid/php-article-extractor
이 라이브러리는 HTML을 검색하려고 시도합니다. 당신은 단순히 article extractor 클래스를 만들고 원하는 URL을 통과하여 parseURL 함수를 호출해야합니다.
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 ); FUCTION processURL 요청과 관련된 제목, 텍스트 및 메타 데이터를 포함하는 배열을 반환합니다. 텍스트가 null 인 경우 이것은 실패한 구문 분석을 나타냅니다. 아래는 위 코드의 출력이어야합니다.
라이브러리가 리디렉션을 따르는 경우 Field 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 인 경우 이것은 실패한 구문 분석을 나타냅니다. 아래는 위 코드의 출력이어야합니다.
프로세스 호출 중에 HTML을 얻으려고하지 않기 때문에 Field 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"
}
또한 언어 탐지 서비스의 키와 사용자 정의 사용자 에이전트 문자열을 전달하여 ArticleExtractor 클래스를 만들 수도 있습니다. 아래의 자세한 내용을 참조하십시오.
언어 탐지는 HTML 메타 데이터 내에서 언어 지정자를 찾고 언어 탐지를 사용하여 처리됩니다.
기사의 언어를 감지 할 수있는 경우, ISO 639-1 형식의 언어 코드와 탐지 방법은 각각 필드 language 및 language_method 에서 반환됩니다. language_method 필드는 성공적으로 발견되면 html 또는 service 일 수 있습니다.
언어 탐지가 실패하거나 사용할 수없는 경우이 두 분야는 모두 NULL로 반환됩니다.
언어 감지하려면 가입 할 수있는 API 키를 사용해야합니다. 그러나이 라이브러리가 없으면이 라이브러리를 사용할 수도 있습니다. HTML 메타 데이터에 기사의 언어에 대한 정보가 포함되어 있지 않으면 language 및 language_method 널 값으로 반환됩니다.
언어 탐지 서비스를 사용 하여이 라이브러리를 활용하려면 언어 감지를 위해 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 );맞춤형 처리로 가독성, 거위 또는 거위로 독서가 시도되는 방법을 강요 할 수 있습니다. 이는 가독성이나 거위가 특정 웹 사이트에 특정 문제가있는 경우에 유용 할 수 있습니다.
이 방법을 강요하려면 단순히 생성자에게 세 번째 인수를 제공하십시오. 4 가지 유효한 방법은 readability , goose , goosecustom 또는 custom 입니다.
$ extractor = new ArticleExtractor ( null , null , " goose " );버전 1.0 기준으로 출력 형식이 변경되어 제목에 대한 Newline 브레이크를 제공했습니다. 이것은 특히 문장 경계를 결정하는 자연어 처리 응용 프로그램에 중요합니다. 이 동작이 필요하지 않은 경우 필요한 경우 추가 신약을 제거하십시오.
이 변경은 헤더와 단락 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설정하십시오.
Composer Docker Image를 사용하여 요구 사항을 다운로드합니다. 일부 의존성 중 일부는 아직 PHP 8을 지원하지 않기 때문에 --ignore-platform-reqs 의 사용에 유의하십시오.
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