MaartenGr / MaartenGr/BERTopic

Error when setting chain representation models as main

Open
#1,846 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.8k
Forks
920
Avg merge
22h 24m
Merged PRs (30d)
5

Description

When chain models are named anything else, it works fine. But if I want to use a chain model as the main representation, it will produce an error.

```python
representation_model = {
"Main": [KeyBERT, MMR],
# "ChatGPT": aspect_model1
"KeyBERT": KeyBERT,
"BERT MMR": [KeyBERT, MMR],
"POS": aspect_model2,
"POS MMR": [aspect_model2, MMR]
}```

#The error produced:

```python

File ~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:433, in BERTopic.fit_transform(self, documents, embeddings, images, y)
430 self._save_representative_docs(custom_documents)
431 else:
432 # Extract topics by calculating c-TF-IDF
--> 433 self._extract_topics(documents, embeddings=embeddings, verbose=self.verbose)
435 # Reduce topics
436 if self.nr_topics:

File ~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:3637, in BERTopic._extract_topics(self, documents, embeddings, mappings, verbose)
3635 documents_per_topic = documents.groupby(['Topic'], as_index=False).agg({'Document': ' '.join})
3636 self.c_tf_idf_, words = self._c_tf_idf(documents_per_topic)
-> 3637 self.topic_representations_ = self._extract_words_per_topic(words, documents)
3638 self._create_topic_vectors(documents=documents, embeddings=embeddings, mappings=mappings)
3639 self.topic_labels_ = {key: f"{key}_" + "_".join([word[0] for word in values[:4]])
3640 for key, values in
3641 self.topic_representations_.items()}

File ~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:3925, in BERTopic._extract_words_per_topic(self, words, documents, c_tf_idf, calculate_aspects)
3923 elif isinstance(self.representation_model, dict):
3924 if self.representation_model.get("Main"):
-> 3925 topics = self.representation_model["Main"].extract_topics(self, documents, c_tf_idf, topics)
3926 topics = {label: values[:self.top_n_words] for label, values in topics.items()}
3928 # Extract additional topic aspects

AttributeError: 'list' object has no attribute 'extract_topics'
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in _bertopic.py at _extract_words_per_topic, especially the handling of the representation_model dictionary and its "Main" entry. Reproduce the shown configuration with fit_transform and confirm that a chain representation model can be used as the main representation without the reported AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.