How can we classify text with FlagReranker

We classified the horoscopes into general topics. Categorizing textual data into predefined groups can provide valuable insights, especially for applications like personalized content delivery or trend analysis. This task is not trivial with horoscopes, as there is no ready-made classification for categorization, and quite often, horoscope categories are subjective. For this, we created a Python script focused on classifying horoscope texts into specific categories using a reranking model. The script uses the FlagReranker model from the FlagEmbedding library, specifically the BAAI/bge-reranker-v2-m3 model. This model is optimized for reranking tasks by comparing textual inputs. Six categories are defined to classify horoscope texts: Love and relationships, Work and career, Health and Emotional Well-Being, Finances, Creativity and innovation, General life advice or timing. The function compares each horoscope text with predefined category descriptions (queries) and then assigns the category with the highest similarity score to each text (each horoscope text is compared against all category descriptions using the compute_score method).

FlagReranker Works? FlagReranker is a component of the FlagEmbedding library, designed to optimize the process of comparing and ranking textual inputs. It is particularly useful for tasks like text classification, retrieval, and ranking, where the goal is to match a query to the most relevant item(s) in a predefined set.


FlagReranker utilizes a pretrained model (e.g., BAAI/bge-reranker-v2-m3) to generate high-dimensional embeddings for text. These embeddings are dense vector representations that encode semantic meaning, allowing for effective comparison between texts. FlagReranker is built on transformer models, which are state-of-the-art for natural language processing (NLP). Models like BAAI/bge-reranker-v2-m3 are fine-tuned specifically for reranking tasks, enhancing their performance on semantic similarity tasks.


The reranker computes similarity scores between pairs of text inputs: input text to be classified or ranked (e.g., a horoscope), and query text, a predefined category description or reference text (e.g., "Horoscope text about love and relationships").


The similarity is typically measured using cosine similarity or other distance metrics. For each input text, the reranker: Compares it against all queries; Assigns a similarity score to each query; Ranks the queries based on their scores; Selects the query (or queries) with the highest score(s) as the best match; This approach ensures that the classification is based on semantic understanding rather than simple keyword matching.