연구개발동아리에서 '복잡한 쿼리에서의 검색 성능 저하 문제'를 해결하기 위한 새로운 RAG 방식을 팀원들과 함께 고안해보고있다. 우리 팀이 Query에서 키워드를 추출할 때 사용한 기본 알고리즘은 그래프 기반의 랭킹 알고리즘인 TextRank이다. 우리의 방식을 적용했을 때의 성능과 다른 키워드 추출 알고리즘들을 적용했을 때의 성능을 비교하기 위해 4가지 방법들(NLTK, SpaCy, Rake, Yake)에 대해 알아보았다. (단, Encoder 계열 모델은 제외하였다.)
1. NLTK
- Natural Language ToolKit
- 텍스트 처리를 위한 다양한 모듈을 제공. TF-IDF 및 TextRank 기반 키워드 추출을 위한 루틴이 포함되어 있음.
- 텍스트 전처리
- 토큰화
- 품사 태깅 (Part-of-Speech Tagging)
- Top-k개 키워드 선별
※ 사용 예시
import nltk
# 텍스트 전처리
text = "This is a sample text for keyword extraction."
text = text.lower().replace(".", "")
# 토큰화
tokens = nltk.word_tokenize(text)
# Part-of-speech(POS) tagging을 사용하여 명사 식별
tags = nltk.pos_tag(tokens)
nouns = [word for (word, tag) in tags if tag == "NN"]
# TF-IDF를 사용하여 랭킹
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer()
tfidf = vectorizer.fit_transform([text])
# Top-k개 키워드 선별(k=3)
top_nouns = sorted(vectorizer.vocabulary_, key=lambda x: tfidf[0, vectorizer.vocabulary_[x]], reverse=True)[:3]
print(top_nouns)
※ Github: https://github.com/nltk/nltk
2. SpaCy
- 고급 자연어 처리(NLP)를 위한 오픈소스 라이브러리
- 빠르고 효율적이며 사용자 친화적으로 설계되어 대량의 텍스트를 처리하고 이해하는 애플리케이션을 구축할 수 있음.
- 토큰들을 기준으로 텍스트 분리
- 토큰 리스트로부터 Hot word 추출: Hot word는 pos tag("PROPN", "ADJ", NOUN")를 기준으로 설정
- Hot word 목록에서 가장 일반적인 top-k개 키워드 선별
※ 사용 예시 (1)
import spacy
# SpaCy 모델 로드 및 새 document 생성
nlp = spacy.load("en_core_web_sm")
doc = nlp("This is a sample text for keyword extraction.")
# noun_chunks 특성을 사용하여 명사구 식별
noun_phrases = [chunk.text for chunk in doc.noun_chunks]
# TF-IDF 분석을 사용하여 명사구 랭킹
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer()
tfidf = vectorizer.fit_transform([doc.text])
# top-k개 명사구 (k=3)
top_phrases = sorted(vectorizer.vocabulary_, key=lambda x: tfidf[0, vectorizer.vocabulary_[x]], reverse=True)[:3]
※ 사용 예시 (2)
import spacy
from collections import Counter
from string import punctuation
nlp = spacy.load("en_core_web_sm")
def get_hotwords(text):
result = []
pos_tag = ['PROPN', 'ADJ', 'NOUN']
doc = nlp(text.lower())
for token in doc:
if(token.text in nlp.Defaults.stop_words or token.text in punctuation):
continue
if(token.pos_ in pos_tag):
result.append(token.text)
return result
new_text = """
When it comes to evaluating the performance of keyword extractors, you can use some of the standard metrics in machine learning: accuracy, precision, recall, and F1 score. However, these metrics don’t reflect partial matches. they only consider the perfect match between an extracted segment and the correct prediction for that tag.
Fortunately, there are some other metrics capable of capturing partial matches. An example of this is ROUGE.
"""
output = set(get_hotwords(new_text))
most_common_list = Counter(output).most_common(10)
for item in most_common_list:
print(item[0])
※ Github: https://github.com/explosion/spaCy
3. Rake
- Rapid Automatic Keyword Extraction algorithm
- 텍스트에 포함된 단어의 통계적 속성을 사용하여 키워드 추출을 위해 설계된 간단하고 효과적인 알고리즘
- 키워드를 추출하기 전에 구두점이나 불용어와 같은 불필요한 문자 제거 (전처리)
- 구두점, 공백 등을 기준으로 텍스트를 개별 단어와 후보 키워드로 분리
- (단어-동시 발생) 행렬 생성
- 후보 키워드의 텍스트 내 출현 빈도, 다른 단어들과의 동시 출현 빈도 등을 고려하여 점수 매기기(word-scoring)
- 개별 단어 점수의 합을 기준으로 top-k 키워드 랭킹
※ 사용 예시
from rake_nltk import Rake
# Rake instance 생성
r = Rake()
text = "RAKE (Rapid Automatic Keyword Extraction) is a keyword extraction algorithm that automatically identifies relevant keywords and phrases in a text document."
r.extract_keywords_from_text(text)
keywords = r.get_ranked_phrases_with_scores()
for score, kw in keywords:
print("Keyword:", kw, "Score:", score)
※ Github: https://github.com/csurfer/rake-nltk
4. Yake
- Yet Another Keyword Extractor
- 단일 텍스트의 텍스트 통계 데이터를 사용하여 통계적 유의성과 맥락적 관련성을 기반으로 텍스트에서 가장 관련성 있는 키워드를 식별하는 비지도 자동 키워드 추출 방법
- 문서(말뭉치)의 크기, 도메인 또는 언어의 종류에 의존하지 않고 키워드를 추출하며, 특정 문서 집합에 대한 학습이 필요하지 않음.
- 키워드를 추출하기 전에 구두점이나 불용어와 같은 불필요한 문자 제거 (전처리)
- 핵심 키워드 및 구문 선별(다중 단어 표현 및 결합어 고려)
- TF-IDF와 같은 통계적 측정 방법을 사용하여 각 용어들의 관련성을 결정한 후 점수 매기기
- 후보 키워드들 중 top-k개 선별
※ 사용 예시
from yake import KeywordExtractor
doc = """
Supervised learning is the machine learning task of learning a function that
maps an input to an output based on example input-output pairs. It infers a
function from labeled training data consisting of a set of training examples.
In supervised learning, each example is a pair consisting of an input object
(typically a vector) and a desired output value (also called the supervisory signal).
A supervised learning algorithm analyzes the training data and produces an inferred function,
which can be used for mapping new examples. An optimal scenario will allow for the
algorithm to correctly determine the class labels for unseen instances. This requires
the learning algorithm to generalize from the training data to unseen situations in a
'reasonable' way (see inductive bias)."""
kw_extractor = KeywordExtractor()
keywords = kw_extractor.extract_keywords(doc)
for kw in keywords:
print("Keyword:", kw[0], "Score:", kw[1])
※ Github: https://github.com/LIAAD/yake
5. 방법들 간 비교
Algorithm | 주요 특징 | 장점 | 단점 |
NLTK | 규칙 및 빈도 기반의 통계적 분석 | 사용자 정의가 용이함. | 통계적 분석/측정이 어려움. |
SpaCy | 언어적 특징, 문법 기반 키워드 추출 | 언어적 정확성을 반영함. | 특정 품사가 아닌 키워드 누락 및 속도가 느림. |
Rake | 단순 인터페이스, 도메인-독립적, 단어 빈도 수 기반의 통계적 분석 |
경량화, 여러 단어로 구성된 구문에서 추출 가능함. |
문맥을 반영하기 어려움. |
Yake | 말뭉치/도메인/언어-독립적, 통계적 분석 |
빠르고 다양한 언어에 적응 가능함. |
언어적 이해가 제한됨. |
아래는 일부 Multi-Hop 데이터셋 샘플들에 대해 4가지 방법론들을 적용하여 실험한 결과이다.
▶ 3개의 쿼리 샘플들에 대해서는 SpaCy의 키워드 추출 성능이 가장 높았다. (정성적 평가)
▶ SpaCy에서 명사구(doc. noun_chunks )를 추출하는 방법과 Dependency Sparsing, Entity Linking 방법을 공부해볼 예정이다. ("en_core_web_sm"에 대해 효과적인 종속성 구문 분석과 Entity 인식이 가능하게 구현되어있기에 성능이 높을 것으로 추측)
(+) TextRank
- 그래프 기반의 ranking 알고리즘으로, PageRank 알고리즘을 텍스트 데이터에 적용하여 그래프 내 단어의 관련성을 파악할 수 있도록 함. (단어-동시 발생)을 바탕으로 관련 용어를 식별하여 자주 함께 발견되는 단어일수록 강하게 연결됨.
- 기본적으로 unweighted, directed graph를 구성하여 Vertex의 중요도를 계산하는 PageRank의 방식에서 개념을 확장하여, weighted graph와 undirected graph도 중요도를 계산할 수 있도록 함.
- 키워드 추출과 텍스트 요약에 사용되는 PyTextRank 패키지 존재(SpaCy pipeline plugin으로 통합)
References
https://www.geeksforgeeks.org/keyword-extraction-methods-in-nlp/#keyword-extraction-using-spacy
https://spotintelligence.com/2022/12/13/keyword-extraction/#1_NLTK_keyword_extraction
'Research & Study Notes' 카테고리의 다른 글
Medical-LLM Evaluation: Methods and Metrics (0) | 2025.04.17 |
---|---|
처음부터 RAG pipeline 구현하기 - (2) (1) | 2024.12.17 |
처음부터 RAG pipeline 구현하기 - (1) (2) | 2024.12.12 |
Hugging Face를 이용한 Transformer 모델 활용 실습 (6) | 2024.12.11 |
KSS 데이터셋으로 TorToise-tts 한국어 Fine-tuning (6) | 2024.11.05 |