Skip to content

CHICPRO

  • Life Log
  • Cycling Log
  • Photo Log
    • Portrait
    • Landscape
    • Flower
    • Etc
  • Coding Log
  • Information

[Python] rake-nltk 모듈을 이용한 키워드 추출

2017-05-18 by 편리

서비스 개발에 앞서 텍스트에서 주요한 키워드를 추출할 수 있는 기능을 테스트해보고 있다. RAKE (Rapid Automatic Keyword Extraction) 알고리즘을 이용했는데 파이썬에서는 쉽게 사용할 수 있는 모듈이 있어서 쉽게 구현할 수 있다. 사용한 모듈은 rake-nltk 이다. 그 외에 python-rake 모듈도 있으나 rake-nltk 모듈은 nltk 가 적용돼서 뭔가 좀 더 좋은 게 아닌가 싶어 사용했다. 사용은 PHP 에서 .py 파일을 실행한 후 결과를 받아서 출력하는 것으로 정했다. 아래 코드들은 기능 테스트에 사용하기 위한 것이므로 실제 사용에는 무리가 있을 수 있다.

<?php
$content = "Samsung Galaxy S8 64GB Unlocked Phone - International Version (Midnight Black) The revolutionary design of the Galaxy S8 begins from the inside out. We rethought every part of the phone's layout to break through the confines of the smartphone screen. So all you see is pure content and no bezel. It's the biggest, most immersive screen on a Galaxy smartphone of this size. And it's easy to hold in one hand. Meet the Galaxy S8 - Infinitely Brilliant. The Galaxy S8 has the world's first Infinity Screen, A screen without limits. The expansive display stretches from edge to edge, giving you the most amount of screen in the least amount of space. The revolutionary design of the Galaxy S8 begins from the inside out. We rethought every part of the phone's layout to break through the confines of the smartphone screen. So all you see is pure content and no bezel. It's the biggest, most immersive screen on a Galaxy smartphone of this size. And it's easy to hold in one hand. The Infinity Display has an incredible end-to-end screen that spills over the phone’s sides, forming a completely smooth, continuous surface with no bumps or angles. It’s pure, pristine, uninterrupted glass. And it takes up the entire front of the phone, flowing seamlessly into the aluminum shell. The result is a beautifully curved, perfectly symmetrical, singular object. Capture life as it happens with the Galaxy S8 cameras. The 12MP rear camera and the 8MP front camera are so accurate and fast that you won't miss a moment, day or night. Prying eyes are not a problem when you have iris scanning on the Galaxy S8. No two irises have the same pattern, not even yours, and they're nearly impossible to replicate. That means with iris scanning, your phone and its contents open to your eyes only. And when you need to unlock really fast, face recognition is a handy option. You never really stop using your phone. That's why Galaxy S8 is driven by the world's first 10nm processor. It's fast and powerful and increases battery efficiency. Plus, there's the ability to expand storage, and to work through rain and dust with IP68-rated performance. The Infinity Display sets a new standard for uninterrupted, immersive experiences. It enables an expanded screen size without necessitating a larger phone. So while the view is grander, Galaxy S8 feels small in your hand, making them easy to hold and use. You'll immediately notice the comfortable grip of the smooth curves, which allow you to hold on easily while you watch movies on the larger screen. And important short cuts are a swipe away, as the edge screen is available on Galaxy S8. Complimenting the uniform front is the equally smooth silhouette on the back. The rear camera sits perfectly flushed with the surface for a visually tranquil profile.";

$content = preg_replace('#[^a-z0-9\'\\s\.\?\,\!\*\&\$]#i', '', $content);

exec('cd /home/keyword');
exec('python3 keywordExtract.py "'.$content.'"', $output);

foreach($output as $out) {
    print_r(json_decode($out));
}
?>

위 코드는 keywordExtract.php 파일의 코드이다. 아래는 keywordExtract.py 파일의 코드이다.

# _*_ coding: utf-8 _*_

from rake_nltk import Rake
import json
import sys

r = Rake()

words = []

if(len(sys.argv) > 1) :
    content = sys.argv[1];

    r.extract_keywords_from_text(content)

    #words = r.get_ranked_phrases_with_scores()
    words = r.get_ranked_phrases()

print(json.dumps(words))

php 의 exec 함수를 이용해 keywordExtract.py 파일을 실행하고 그 결과를 받아서 화면에 출력한다. 출력 결과는 아래와 같다.

Array
(
    [0] => samsung galaxy s8 64gb unlocked phone international version midnight black
    [1] => rear camera sits perfectly flushed
    [2] => expanded screen size without necessitating
    [3] => galaxy s8 infinitely brilliant
    [4] => galaxy s8 feels small
    [5] => never really stop using
    [6] => galaxy s8 cameras
    [7] => galaxy s8 begins
    [8] => 12mp rear camera
    [9] => screen without limits
    [10] => visually tranquil profile
    [11] => rethought every part
    [12] => increases battery efficiency
    [13] => important short cuts
    [14] => first 10nm processor
    [15] => 8mp front camera
    [16] => expansive display stretches
    [17] => incredible endtoend screen
    [18] => unlock really fast
    ...............
)

실제 결과는 위 내용보다 더 많이 출력된다. php 파일의 exec 함수 관련 코드에서 경로는 적절히 수정해야 한다.

Post navigation

Previous Post:

[PHP] curl POST 전송 때 파라미터 전달되지 않는 경우 해결법

Next Post:

[PhantomJS] Ubuntu 16.04에서 QXcbConnection: Could not connect to display 오류 해결

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • SK 세븐모바일 유심 셀프교체
  • php 배열 연산에서 + 와 array_merge 의 차이
  • pcntl_fork 를 이용한 다중 프로세스 실행
  • 아이폰 단축어를 이용하여 주중 공휴일엔 알람 울리지 않게 하기
  • 구글 캘린더 전체일정 재동기화
  • OpenLiteSpeed 웹서버에 HTTP 인증 적용
  • OpenLiteSpeed 웹어드민 도메인 연결
  • WireGuard를 이용한 VPN 환경 구축
  • Ubuntu 22.04 서버에 OpenLiteSpeed 웹서버 세팅
  • 맥 vim 세팅

Recent Comments

  • 편리 on 업무관리용 그누보드 게시판 스킨
  • 임종섭 on 업무관리용 그누보드 게시판 스킨
  • 캐논 5D 펌웨어 | Dslr 펌웨어 업그레이드 방법 82 개의 베스트 답변 on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • Top 5 캐논 5D 펌웨어 Top 89 Best Answers on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • 편리 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 임팀장 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 편리 on Notepad++ NppFTP 플러그인 수동 설치
  • paul-j on Notepad++ NppFTP 플러그인 수동 설치
  • YS on Windows 10 iCloud 사진 저장 폴더 변경
  • 편리 on Docker를 이용한 Centos7 + httpd + php 5.4 개발환경 구축

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2025 CHICPRO | Built using WordPress and SuperbThemes