Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Add topic stop words, closes #352

davidmezzetti9b74d6a7c543
diff --git a/src/python/txtai/graph/topics.py b/src/python/txtai/graph/topics.py
index a84215e8..9770d6c5 100644
--- a/src/python/txtai/graph/topics.py
+++ b/src/python/txtai/graph/topics.py
@@ -25,6 +25,11 @@ class Topics:
self.pattern = re.compile(r"(?u)\b\w\w+\b")
self.tokenizer = Tokenizer()
+ # Additional stopwords to ignore when building topic names
+ self.stopwords = set()
+ if "stopwords" in self.config:
+ self.stopwords.update(self.config["stopwords"])
+
def __call__(self, graph):
"""
Runs topic modeling for input graph.
@@ -122,7 +127,7 @@ class Topics:
for term in terms:
# Add terms that pass tokenization rules
- if self.tokenizer(term):
+ if self.tokenizer(term) and term not in self.stopwords:
topn.append(term)
# Break once topn terms collected
diff --git a/test/python/testgraph.py b/test/python/testgraph.py
index 5c3aaef7..154bab0a 100644
--- a/test/python/testgraph.py
+++ b/test/python/testgraph.py
@@ -40,7 +40,7 @@ class TestGraph(unittest.TestCase):
{"name": "topic", "expression": "graph(indexid, 'topic')"},
{"name": "topicrank", "expression": "graph(indexid, 'topicrank')"},
],
- "graph": {"limit": 5, "minscore": 0.2, "batchsize": 4, "approximate": False, "topics": {"categories": ["News"]}},
+ "graph": {"limit": 5, "minscore": 0.2, "batchsize": 4, "approximate": False, "topics": {"categories": ["News"], "stopwords": ["the"]}},
}
# Create embeddings instance