2023-06-09    Share on: Twitter | Facebook | HackerNews | Reddit

Extracting Keywords From the User Query

When it comes to extracting keywords or key terms from a user query, there are several approaches that can be used. Each approach has its own set of pros and cons, which I will discuss below:

Rule-Based Approach

  • Pros: This approach involves defining a set of rules or patterns to identify keywords based on specific criteria. It can be effective for simple queries and known patterns, allowing for precise keyword extraction.
  • Cons: Rule-based approaches can be limited in their flexibility and scalability. They require manual effort to create and maintain the rules, making them less suitable for handling complex or evolving queries. Additionally, they may not perform well when faced with ambiguous or unstructured input.

Linguistic Analysis

  • Pros: Linguistic analysis techniques utilize natural language processing (NLP) algorithms to analyze the grammatical structure and semantics of a query. By considering parts of speech, syntactic relationships, and semantic associations, they can extract relevant keywords effectively.
  • Cons: This approach can be computationally expensive and may require substantial linguistic resources such as parsers, lexicons, and ontologies. Handling languages with complex grammar or processing highly contextual queries can be challenging. It might also struggle with ambiguous phrases or idiomatic expressions.

Machine Learning (ML) and Statistical Methods

  • Pros: ML techniques, such as supervised or unsupervised learning, can automatically learn patterns and extract keywords based on training data. They can adapt to different query types and improve over time with more data. Statistical methods, such as term frequency-inverse document frequency (TF-IDF), can also identify important keywords based on their prevalence and relevance within a dataset.
  • Cons: Building ML models requires labeled training data, which can be time-consuming and expensive to create. Models may struggle with rare or domain-specific queries if not adequately trained. They can also be susceptible to biases present in the training data, and their performance may degrade when faced with queries significantly different from the training distribution.

Hybrid Approaches:

  • Pros: Hybrid approaches combine multiple techniques, leveraging the strengths of each to improve keyword extraction. For example, combining rule-based methods with ML models can enhance accuracy and handle a wider range of queries.
  • Cons: Designing and implementing hybrid approaches can be complex and require expertise in multiple areas. Combining different techniques may introduce additional computational overhead, impacting performance and response time.

It's important to note that the effectiveness of these approaches can vary depending on factors such as the nature of the queries, available resources, and the desired level of accuracy. A well-designed solution often involves a combination of techniques to achieve the best results.

What about using (large) language models?

Using language models, such as GPT-3.5, can be a powerful approach for extracting keywords or key terms from a user query. Language models are trained on vast amounts of text data and have the ability to understand and generate human-like language.

Here are the pros and cons of using language models for keyword extraction:


  1. Contextual Understanding: Language models can capture the contextual meaning of words and phrases in a query. They can consider the surrounding words and sentences to extract keywords that are most relevant to the overall query.
  2. Handling Ambiguity: Language models can handle ambiguous queries by considering the broader context. They can interpret the query based on available information and generate keywords that make the most sense in the given context.
  3. Generalization: Language models have the ability to generalize from the training data and can extract keywords effectively even for queries that are slightly different from what they have seen before.
  4. Continuous Learning: Language models can be fine-tuned on specific domains or datasets to improve their keyword extraction capabilities. This allows them to adapt to specific contexts and improve their accuracy over time.


  1. Lack of Control: Language models generate keywords based on their learned patterns and training data, which may not always align with specific user requirements or domain-specific terminology. They may produce keywords that are technically correct but not exactly what the user intended.
  2. Over-reliance on Training Data: Language models heavily depend on the data they were trained on. If the training data contains biases or limitations, the model may exhibit the same biases or struggle with specific types of queries that were underrepresented in the training data.
  3. Computational Overhead: Language models can be computationally expensive to run, especially for real-time applications. The time required for keyword extraction using a language model might not be suitable for scenarios that demand low latency or high throughput.
  4. Lack of Explanation: Language models can provide keyword outputs, but they may not offer clear explanations for why certain words were selected as keywords. This lack of interpretability can make it challenging to understand the reasoning behind the chosen keywords.

While language models can be effective for keyword extraction, it's important to consider these pros and cons and carefully evaluate the trade-offs before integrating them into a production system. It may be necessary to fine-tune the language model or combine it with other techniques to address specific limitations or requirements.

More on Machine Learning and statistical Methods for Keywords Extraction

There are several machine learning and statistical methods commonly used for keyword extraction from text. Here are some popular techniques:

  1. Term Frequency-Inverse Document Frequency (TF-IDF): TF-IDF is a statistical method that measures the importance of a term within a document and across a collection of documents. It calculates a weight for each term based on its frequency in the document and inversely proportional to its frequency in the entire document collection. Keywords with higher TF-IDF scores are considered more significant.

  2. TextRank: TextRank is an algorithm inspired by Google's PageRank algorithm for ranking web pages. It applies a graph-based ranking approach to identify important keywords in a text. In this method, the text is represented as a graph, where each word is a node, and edges represent the co-occurrence or semantic similarity between words. TextRank assigns scores to words based on their centrality in the graph, with higher scores indicating more important keywords.

  3. Latent Dirichlet Allocation (LDA): LDA is a generative probabilistic model that represents a collection of documents as a mixture of topics. It assumes that each document contains a distribution of topics, and each topic is characterized by a distribution of words. LDA can be used for keyword extraction by identifying the most probable words associated with each topic. Keywords are then selected based on their relevance to the document's topics.

  4. Support Vector Machines (SVM): SVM is a supervised learning algorithm that can be used for keyword extraction by treating it as a binary classification problem. Training data is labeled with keywords and non-keywords, and SVM learns a decision boundary to separate the two classes. New text can be classified using the trained SVM model, and the words contributing most to the classification decision are considered keywords.

  5. Neural Networks: Various neural network architectures can be employed for keyword extraction, such as recurrent neural networks (RNNs), convolutional neural networks (CNNs), and transformers. These models can learn representations of words and capture complex relationships between them. They can be trained using labeled data or trained in an unsupervised manner by formulating the problem as an autoencoder or sequence-to-sequence learning.

  6. Rule-based methods: Rule-based approaches define a set of linguistic rules or patterns to identify keywords based on specific criteria such as part-of-speech tags, syntactic structures, or domain-specific rules. These methods can be effective when the domain or language has well-defined patterns for keywords.

Exemplary implementation

One state-of-the-art solution for keyword extraction from short texts is the TextRank algorithm, which is an unsupervised approach based on the PageRank algorithm. It has been proven to be highly effective in identifying important keywords in a text.

Here's a Python implementation using the nltk library, which provides an implementation of the TextRank algorithm:

import nltk
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk.tag import pos_tag
from nltk.corpus import wordnet as wn
from collections import defaultdict

def preprocess_text(text):
    # Tokenize the text into sentences
    sentences = sent_tokenize(text)

    # Tokenize each sentence into words and perform part-of-speech tagging
    tagged_words = []
    for sentence in sentences:
        words = word_tokenize(sentence)

    # Lemmatize the words and remove stopwords
    lemmatizer = WordNetLemmatizer()
    stop_words = set(stopwords.words('english'))
    preprocessed_words = []
    for word, tag in tagged_words:
        # Consider only nouns, verbs, adjectives, and adverbs
        if tag.startswith('NN') or tag.startswith('VB') or tag.startswith('JJ') or tag.startswith('RB'):
            # Lemmatize the word
            lemma = lemmatizer.lemmatize(word, get_wordnet_pos(tag))

            # Convert to lowercase and remove stopwords
            if lemma.lower() not in stop_words:

    return preprocessed_words

def get_wordnet_pos(tag):
    if tag.startswith('N'):
        return wn.NOUN
    elif tag.startswith('V'):
        return wn.VERB
    elif tag.startswith('J'):
        return wn.ADJ
    elif tag.startswith('R'):
        return wn.ADV
        return None

def calculate_similarity(word1, word2):
    synsets1 = wn.synsets(word1)
    synsets2 = wn.synsets(word2)
    if synsets1 and synsets2:
        max_sim = max((wn.path_similarity(s1, s2) or 0) for s1 in synsets1 for s2 in synsets2)
        return max_sim
    return 0

def textrank_keywords(text, top_n=5):
    # Preprocess the text
    words = preprocess_text(text)

    # Build the word co-occurrence graph
    graph = defaultdict(list)
    for i, word1 in enumerate(words):
        for j, word2 in enumerate(words):
            if i != j:
                similarity = calculate_similarity(word1, word2)
                if similarity > 0:
                    graph[word1].append((word2, similarity))

    # Apply the TextRank algorithm
    scores = defaultdict(float)
    damping_factor = 0.85
    max_iterations = 100
    for _ in range(max_iterations):
        prev_scores = dict(scores)
        for word1 in words:
            score = (1 - damping_factor) + damping_factor * sum(prev_scores[word2] * weight for word2, weight in graph[word1])
            scores[word1] = score

    # Get the top keywords
    top_keywords = sorted(scores.items(), key=lambda x: x[1], reverse=True)[:top_n]
    return top_keywords

# Example usage
text = "What are the benefits of exercise for mental health?"
keywords = textrank_keywords(text)

NOTE: before you can start using it you will need to download certain data resources from NLTK (Natural Language Toolkit) in order to use it for keyword extraction. Specifically, you will need to download the stopwords corpus and WordNet data.

To download the necessary data, you can use the following code snippet:

import nltk