Skip to content

Commit

Permalink
fix(clustering)
Browse files Browse the repository at this point in the history
  • Loading branch information
leandro-driguez committed Dec 21, 2022
1 parent 22fee63 commit 6bb5838
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ function App() {
count={100}
page={page}
onChange={async (_, value) => {
setPage(value);
await axios.get('http://localhost:8000/search?' +
var url = 'http://localhost:8000/search?' +
'model='+encodeURIComponent(model)+
'&dataset=' + encodeURIComponent(dataset) +
'&query=' + encodeURIComponent(sessionStorage['query']) +
'&pag=' + encodeURIComponent(value))
'&pag=' + encodeURIComponent(value);
console.log(sessionStorage['model'])
if (sessionStorage['model'] === 'clustering')
url = 'http://localhost:8000/clustering?' +
'dataset=' + encodeURIComponent(dataset) +
'&query=' + encodeURIComponent(sessionStorage['query']) +
'&cluster=' + encodeURIComponent(value);

setPage(value);
await axios.get(url)
.then((resp) => {
setDocumentDtos(resp.data.results);
setShowResults(true);
Expand Down
13 changes: 11 additions & 2 deletions src/client/src/components/QueryBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ function QueryBar(props: QueryBarProps) {
className="searchButton"
onClick={async () => {
sessionStorage[ 'query'] = query
await axios.get('http://localhost:8000/search?' +

var url = 'http://localhost:8000/search?' +
'model='+encodeURIComponent(sessionStorage['model'])+
'&dataset=' + encodeURIComponent(sessionStorage['dataset']) +
'&query=' + encodeURIComponent(query))
'&query=' + encodeURIComponent(sessionStorage['query']) +
'&pag=' + encodeURIComponent(sessionStorage['pag']);
if (sessionStorage['model'] === 'clustering')
url = 'http://localhost:8000/clustering?' +
'dataset=' + encodeURIComponent(sessionStorage['dataset']) +
'&query=' + encodeURIComponent(sessionStorage['query']) +
'&cluster=' + encodeURIComponent(sessionStorage['pag']);

await axios.get(url)
.then((resp) => {
props.setDocumentDtos(resp.data.results);
props.setShowResults(true);})
Expand Down
22 changes: 20 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from models.corpus import Corpus
from models.fuzzy_model import FuzzyModel
from models.vector_model import VectorModel
# from models.kmeans_based_model import VectorModelKMEANS
from models.kmeans_based_model import VectorModelKMEANS
from models.relevance_feedback import RelevanceFeedback
import dictdatabase as ddb

Expand Down Expand Up @@ -99,4 +99,22 @@ async def feedbackController(
title=doc['title'], author=doc['author'],
text=doc['text'], score=tuple[0]))

return { "results": result }
return { "results": result }

@app.get('/clustering')
async def clusteringController(dataset: str, query: str, cluster: int):

if type(models['clustering'][dataset]) == str:
models['clustering'][dataset] = eval(models['clustering'][dataset])
ranking: VectorModelKMEANS = models['clustering'][dataset]

result = []

for x, score, doc_id in ranking.searchSplitedByClusters(query)[cluster-1]:
print(x, score, doc_id)
doc = corpus[dataset].get_doc(doc_id)
result.append(DocumentDto(doc_id=doc['doc_id'],
title=doc['title'], author=doc['author'],
text=doc['text'], score=score))

return {"results": result}
4 changes: 3 additions & 1 deletion src/models/kmeans_based_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def searchSplitedByClusters(self, query : str):
results_by_cluster = [[] for _ in range(self.noClusters)]
for score, doc_id in results:
results_by_cluster[self.kmeans.labels_[self.doc_postion[doc_id]]].append((self.kmeans.labels_[self.doc_postion[doc_id]], score, doc_id))


results_by_cluster = sorted(results_by_cluster, key=lambda x: x[0][1], reverse=True)

return results_by_cluster

def GetQueryVector(idfs, terms, query):
Expand Down

0 comments on commit 6bb5838

Please sign in to comment.