Skip to content

Commit

Permalink
Batch extractor context queries, closes #120
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmezzetti committed Oct 7, 2021
1 parent 5ca9e76 commit 71783c2
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/python/txtai/pipeline/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def query(self, queries, texts):
list of (id, text, score)
"""

if not queries:
return []

# Tokenize text
segments, tokenlist = [], []
for text in texts:
Expand All @@ -97,25 +100,24 @@ def query(self, queries, texts):
# Add index id to segments to preserver ordering after filters
segments = list(enumerate(segments))

# Run batch queries for performance purposes
if isinstance(self.similarity, Similarity):
# Get list of (id, score) - sorted by highest score per query
scores = self.similarity(queries, [t for _, t in segments])
else:
# Assume this is an embeddings instance, tokenize and run similarity queries
scores = self.similarity.batchsimilarity([self.tokenizer.tokenize(x) for x in queries], tokenlist)

# Build question-context pairs
results = []
for query in queries:
for i, query in enumerate(queries):
# Get list of required and prohibited tokens
must = [token.strip("+") for token in query.split() if token.startswith("+") and len(token) > 1]
mnot = [token.strip("-") for token in query.split() if token.startswith("-") and len(token) > 1]

# List of matches
matches = []

# Get list of (id, score) - sorted by highest score
if isinstance(self.similarity, Similarity):
scores = self.similarity(query, [t for _, t in segments])
else:
# Assume this is an embeddings instance, tokenize and run similarity query
query = self.tokenizer.tokenize(query)
scores = self.similarity.similarity(query, tokenlist)

for x, score in scores:
for x, score in scores[i]:
# Get segment text
text = segments[x][1]

Expand Down

0 comments on commit 71783c2

Please sign in to comment.