ExtractTopWords.py 18.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
import nltk
import string
import collections
from tqdm import tqdm
from typing import List
import numpy as np
import re  
from nltk.tokenize import word_tokenize
import umap
from collections import Counter
import warnings

from typing import List

# make sure the import works even if the package has not been installed and just the files are used
try:
    from topicgpt.GetEmbeddingsOpenAI import GetEmbeddingsOpenAI
except:
    from GetEmbeddingsOpenAI import GetEmbeddingsOpenAI

nltk.download('stopwords', quiet=True)  # download stopwords
nltk.download('punkt', quiet=True) # download tokenizer

class ExtractTopWords:
    
    def extract_centroids(self, embeddings: np.ndarray, labels: np.ndarray) -> dict:
        """
        Extract centroids of clusters.

        Args:
            embeddings (np.ndarray): Embeddings to cluster and reduce.
            labels (np.ndarray): Cluster labels. -1 means outlier.

        Returns:
            dict: Dictionary of cluster labels and their centroids.
        """

        centroid_dict = {}
        for label in np.unique(labels):
            if label != -1:
                centroid_dict[label] = np.mean(embeddings[labels == label], axis = 0)

        return centroid_dict
    
    def extract_centroid(self, embeddings: np.ndarray) -> np.ndarray:
        """
        Extract the single centroid of a cluster.

        Args:
            embeddings (np.ndarray): Embeddings to extract the centroid from.

        Returns:
            np.ndarray: The centroid of the cluster.
        """

        return np.mean(embeddings, axis = 0)
    
    def compute_centroid_similarity(self, embeddings: np.ndarray, centroid_dict: dict, cluster_label: int) -> np.ndarray:
        """
        Compute the similarity of the document embeddings to the centroid of the cluster via cosine similarity.

        Args:
            embeddings (np.ndarray): Embeddings to cluster and reduce.
            centroid_dict (dict): Dictionary of cluster labels and their centroids.
            cluster_label (int): Cluster label for which to compute the similarity.

        Returns:
            np.ndarray: Cosine similarity of the document embeddings to the centroid of the cluster.
        """

        centroid = centroid_dict[cluster_label]
        similarity = np.dot(embeddings, centroid) / (np.linalg.norm(embeddings) * np.linalg.norm(centroid))
        return similarity
    
    def get_most_similar_docs(self, corpus: list[str], embeddings: np.ndarray, labels: np.ndarray, centroid_dict: dict, cluster_label: int, top_n: int = 10) -> List[str]:
        """
        Get the most similar documents to the centroid of a cluster.

        Args:
            corpus (list[str]): List of documents.
            embeddings (np.ndarray): Embeddings to cluster and reduce.
            labels (np.ndarray): Cluster labels. -1 means outlier.
            centroid_dict (dict): Dictionary of cluster labels and their centroids.
            cluster_label (int): Cluster label for which to compute the similarity.
            top_n (int, optional): Number of top documents to extract.

        Returns:
            List[str]: List of the most similar documents to the centroid of a cluster.
        """

        similarity = self.compute_centroid_similarity(embeddings, centroid_dict, cluster_label)
        most_similar_docs = [corpus[i] for i in np.argsort(similarity)[-top_n:][::-1]]
        return most_similar_docs
    
    def compute_corpus_vocab(self, 
                        corpus: list[str],
                        remove_stopwords: bool = True, 
                        remove_punction: bool = True, 
                        min_word_length: int = 3,
                        max_word_length: int = 20, 
                        remove_short_words: bool = True, 
                        remove_numbers: bool = True, 
                        verbose: bool = True,
                        min_doc_frequency: int = 3,
                        min_freq: float = 0.1,
                        max_freq: float = 0.9) -> list[str]:
        """
        Compute the vocabulary of the corpus and perform preprocessing of the corpus.

        Args:
            corpus (list[str]): List of documents.
            remove_stopwords (bool, optional): Whether to remove stopwords.
            remove_punction (bool, optional): Whether to remove punctuation.
            min_word_length (int, optional): Minimum word length to retain.
            max_word_length (int, optional): Maximum word length to retain.
            remove_short_words (bool, optional): Whether to remove short words.
            remove_numbers (bool, optional): Whether to remove numbers.
            verbose (bool, optional): Whether to print progress and describe what is happening.
            min_doc_frequency (int, optional): Minimum number of documents a word should appear in to be considered in the vocabulary.
            min_freq (float, optional): Minimum frequency percentile of words to be considered in the vocabulary.
            max_freq (float, optional): Maximum frequency percentile of words to be considered in the vocabulary.

        Returns:
            list[str]: List of words in the corpus sorted alphabetically.
        """

        stopwords = set(nltk.corpus.stopwords.words('english'))
        
        word_counter = collections.Counter()
        doc_frequency = collections.defaultdict(set)

        for doc_id, doc in enumerate(tqdm(corpus, disable=not verbose, desc="Processing corpus")):
            words = nltk.word_tokenize(doc)
            for word in words:
                if remove_punction and word in string.punctuation:
                    continue
                if remove_stopwords and word.lower() in stopwords:
                    continue
                if remove_numbers and re.search(r'\d', word):  # use a regular expression to check for digits
                    continue
                if not re.search('[a-zA-Z]', word):  # checks if word contains at least one alphabetic character
                    continue
                # remove words that do not begin with an alphabetic character
                if not word[0].isalpha():
                    continue
                if len(word) > max_word_length or (remove_short_words and len(word) < min_word_length):
                    continue
                
                word_lower = word.lower()
                word_counter[word_lower] += 1
                doc_frequency[word_lower].add(doc_id)

        total_words = sum(word_counter.values())
        freq_counter = {word: count / total_words for word, count in word_counter.items()}

        # print most common words and their frequencies
        if verbose:
            print("Most common words in the vocabulary:")
            for word, count in word_counter.most_common(10):
                print(f"{word}: {count}")

        freq_arr = np.array(list(freq_counter.values()))

        min_freq_value = np.quantile(freq_arr, min_freq, method="lower")
        max_freq_value = np.quantile(freq_arr, max_freq, method="higher")
        

        vocab = {}

        for word in freq_counter.keys():
            if min_freq_value <= freq_counter[word] <= max_freq_value and len(doc_frequency[word]) >= min_doc_frequency:
                vocab[word] = freq_counter[word]

        vocab = {word for word in freq_counter.keys() 
                if min_freq_value <= freq_counter[word] <= max_freq_value 
                and len(doc_frequency[word]) >= min_doc_frequency}

        # Sorting the vocabulary alphabetically
        vocab = sorted(list(vocab))
        
        return vocab

    def compute_words_topics(self, corpus: list[str], vocab: list[str], labels: np.ndarray) -> dict:
        """
        Compute the words per topic.

        Args:
            corpus (list[str]): List of documents.
            vocab (list[str]): List of words in the corpus sorted alphabetically.
            labels (np.ndarray): Cluster labels. -1 means outlier.

        Returns:
            dict: Dictionary of topics and their words.
        """


        # Download NLTK resources (only required once)
        nltk.download("punkt")
        vocab = set(vocab)

        words_per_topic = {label: [] for label in np.unique(labels) if label != -1}

        for doc, label in tqdm(zip(corpus, labels), desc="Computing words per topic", total=len(corpus)):
            if label != -1:
                words = word_tokenize(doc)
                for word in words:
                    if word.lower() in vocab:
                        words_per_topic[label].append(word.lower())

        return words_per_topic
                    
    def embed_vocab_openAI(self, client, vocab: list[str], embedder: GetEmbeddingsOpenAI = None) -> dict[str, np.ndarray]:
        """
        Embed the vocabulary using the OpenAI embedding API.

        Args:
            client: Client.
            vocab (list[str]): List of words in the corpus sorted alphabetically.
            embedder (GetEmbeddingsOpenAI, optional): Embedding object.

        Returns:
            dict[str, np.ndarray]: Dictionary of words and their embeddings.
        """

        vocab = sorted(list(set(vocab)))
        if embedder is None: 
            embedder = GetEmbeddingsOpenAI.GetEmbeddingsOpenAI(client)
        result = embedder.get_embeddings(vocab)

        res_dict = {}
        for word, emb in zip(vocab, result["embeddings"]):
            res_dict[word] = emb
        return res_dict
    
    def compute_bow_representation(self, document: str, vocab: list[str], vocab_set: set[str]) -> np.ndarray:
        """
        Compute the bag-of-words representation of a document.

        Args:
            document (str): Document to compute the bag-of-words representation of.
            vocab (list[str]): List of words in the corpus sorted alphabetically.
            vocab_set (set[str]): Set of words in the corpus sorted alphabetically.

        Returns:
            np.ndarray: Bag-of-words representation of the document.
        """

        bow = np.zeros(len(vocab))
        words = word_tokenize(document)
        if vocab_set is None:
            vocab_set = set(vocab)
        for word in words:
            if word.lower() in vocab_set:
                bow[vocab.index(word.lower())] += 1
        return bow   
    
    def compute_word_topic_mat_old(self, corpus: list[str], vocab: list[str], labels: np.ndarray, consider_outliers: bool = False) -> np.ndarray:
        """
        Compute the word-topic matrix.

        Args:
            corpus (list[str]): List of documents.
            vocab (list[str]): List of words in the corpus sorted alphabetically.
            labels (np.ndarray): Cluster labels. -1 means outlier.
            consider_outliers (bool, optional): Whether to consider outliers when computing the top words. I.e. whether the labels contain -1 to indicate outliers.

        Returns:
            np.ndarray: Word-topic matrix.
        """

        if consider_outliers:
            word_topic_mat = np.zeros(len(vocab), len((np.unique(labels))))
        else:
            word_topic_mat = np.zeros((len(vocab), len((np.unique(labels)) - 1)))

        vocab_set = set(vocab)
        for i, doc in tqdm(enumerate(corpus), desc="Computing word-topic matrix", total=len(corpus)):
            if labels[i] > - 0.5:
                bow = self.compute_bow_representation(doc, vocab, vocab_set)
                idx_to_add = labels[i]
                word_topic_mat[:, idx_to_add] += bow

        return word_topic_mat
    
    def compute_word_topic_mat(self, corpus: list[str], vocab: list[str], labels: np.ndarray, consider_outliers=False) -> np.ndarray:
        """
        Compute the word-topic matrix efficiently.

        Args:
            corpus (list[str]): List of documents.
            vocab (list[str]): List of words in the corpus, sorted alphabetically.
            labels (np.ndarray): Cluster labels. -1 indicates outliers.
            consider_outliers (bool, optional): Whether to consider outliers when computing the top words. Defaults to False.

        Returns:
            np.ndarray: Word-topic matrix.
        """


        corpus_arr = np.array(corpus) 

        if consider_outliers:
            word_topic_mat = np.zeros((len(vocab), len((np.unique(labels)))))
        else:
            word_topic_mat = np.zeros((len(vocab), len((np.unique(labels)))))
        
        for i, label in tqdm(enumerate(np.unique(labels)), desc="Computing word-topic matrix", total=len(np.unique(labels))):
            topic_docs = corpus_arr[labels == label]
            topic_doc_string = " ".join(topic_docs)
            topic_doc_words = word_tokenize(topic_doc_string)
            topic_doc_counter = Counter(topic_doc_words)

            word_topic_mat[:, i] = np.array([topic_doc_counter.get(word, 0) for word in vocab])
        
        return word_topic_mat

    def extract_topwords_tfidf(self, word_topic_mat: np.ndarray, vocab: list[str], labels: np.ndarray, top_n_words: int = 10) -> dict:
        """
        Extract the top words for each topic using a class-based tf-idf score.

        Args:
            word_topic_mat (np.ndarray): Word-topic matrix.
            vocab (list[str]): List of words in the corpus sorted alphabetically.
            labels (np.ndarray): Cluster labels. -1 means outlier.
            top_n_words (int, optional): Number of top words to extract per topic.

        Returns:
            dict: Dictionary of topics and their top words.
        """


        if min(labels) == -1:
            word_topic_mat = word_topic_mat[:, 1:]


        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=RuntimeWarning)
            tf = word_topic_mat / np.sum(word_topic_mat, axis=0)
            idf = np.log(1 + (word_topic_mat.shape[1] / np.sum(word_topic_mat > 0, axis=1)))

            tfidf = tf * idf[:, np.newaxis]
        
            # set tfidf to zero if tf is nan (happens if word does not occur in any document or topic does not have any words)
            tfidf[np.isnan(tf)] = 0

        # extract top words for each topic
        top_words = {}
        top_word_scores = {}
        for topic in np.unique(labels):
            if topic != -1:
                indices = np.argsort(-tfidf[:, topic])[:top_n_words]
                top_words[topic] = [vocab[word_idx] for word_idx in indices]
                top_word_scores[topic] = [tfidf[word_idx, topic] for word_idx in indices]


        return top_words, top_word_scores
    
    def compute_embedding_similarity_centroids(self, vocab: list[str], vocab_embedding_dict: dict, umap_mapper: umap.UMAP, centroid_dict: dict, reduce_vocab_embeddings: bool = False, reduce_centroid_embeddings: bool = False) -> np.ndarray:
        """
        Compute the cosine similarity of each word in the vocabulary to each centroid.

        Args:
            vocab (list[str]): List of words in the corpus sorted alphabetically.
            vocab_embedding_dict (dict): Dictionary of words and their embeddings.
            umap_mapper (umap.UMAP): UMAP mapper to transform new embeddings in the same way as the document embeddings.
            centroid_dict (dict): Dictionary of cluster labels and their centroids. -1 means outlier.
            reduce_vocab_embeddings (bool, optional): Whether to reduce the vocab embeddings with the UMAP mapper.
            reduce_centroid_embeddings (bool, optional): Whether to reduce the centroid embeddings with the UMAP mapper.

        Returns:
            np.ndarray: Cosine similarity of each word in the vocab to each centroid. Has shape (len(vocab), len(centroid_dict) - 1).
        """

        embedding_dim = umap_mapper.n_components
        centroid_arr = np.zeros((len(centroid_dict), embedding_dim))
        for i, centroid in enumerate(centroid_dict.values()):
            centroid_arr[i] = centroid
        if reduce_centroid_embeddings:
            centroid_arr = umap_mapper.transform(centroid_arr)
        
        centroid_arr = centroid_arr / np.linalg.norm(centroid_arr, axis=1).reshape(-1,1)
        

        org_embedding_dim = list(vocab_embedding_dict.values())[0].shape[0]
        vocab_arr = np.zeros((len(vocab), org_embedding_dim))
        for i, word in enumerate(vocab):
            vocab_arr[i] = vocab_embedding_dict[word]
        if reduce_vocab_embeddings:
            vocab_arr = umap_mapper.transform(vocab_arr)

        vocab_arr = vocab_arr / np.linalg.norm(vocab_arr, axis=1).reshape(-1,1)
        
        similarity = vocab_arr @ centroid_arr.T # cosine similarity
        return similarity
    
    def extract_topwords_centroid_similarity(self, word_topic_mat: np.ndarray, vocab: list[str], vocab_embedding_dict: dict, centroid_dict: dict, umap_mapper: umap.UMAP, top_n_words: int = 10, reduce_vocab_embeddings: bool = True, reduce_centroid_embeddings: bool = False, consider_outliers: bool = False) -> tuple[dict, np.ndarray]:
        """
        Extract the top words for each cluster by computing the cosine similarity of the words that occur in the corpus to the centroid of the cluster.

        Args:
            word_topic_mat (np.ndarray): Word-topic matrix.
            vocab (list[str]): List of words in the corpus sorted alphabetically.
            vocab_embedding_dict (dict): Dictionary of words and their embeddings.
            centroid_dict (dict): Dictionary of cluster labels and their centroids. -1 means outlier.
            umap_mapper (umap.UMAP): UMAP mapper to transform new embeddings in the same way as the document embeddings.
            top_n_words (int, optional): Number of top words to extract per topic.
            reduce_vocab_embeddings (bool, optional): Whether to reduce the vocab embeddings with the UMAP mapper.
            reduce_centroid_embeddings (bool, optional): Whether to reduce the centroid embeddings with the UMAP mapper.
            consider_outliers (bool, optional): Whether to consider outliers when computing the top words. I.e., whether the labels contain -1 to indicate outliers.

        Returns:
            dict: Dictionary of topics and their top words.
            np.ndarray: Cosine similarity of each word in the vocab to each centroid. Has shape (len(vocab), len(centroid_dict) - 1).
        """

        similarity_mat = self.compute_embedding_similarity_centroids(vocab, vocab_embedding_dict, umap_mapper, centroid_dict, reduce_vocab_embeddings, reduce_centroid_embeddings)
        top_words = {}
        top_word_scores = {}
        
        if word_topic_mat.shape[1] > len(np.unique(list(centroid_dict.keys()))):	
            word_topic_mat = word_topic_mat[:, 1:] #ignore outliers

        for i, topic in enumerate(np.unique(list(centroid_dict.keys()))):
            if topic != -1:
                topic_similarity_mat = similarity_mat[:, topic] * word_topic_mat[:, topic]
                top_words[topic] = [vocab[word_idx] for word_idx in np.argsort(-topic_similarity_mat)[:top_n_words]]
                top_word_scores[topic] = [similarity_mat[word_idx, topic] for word_idx in np.argsort(-similarity_mat[:, topic])[:top_n_words]]

        return top_words, top_word_scores