Skip to content

Commit

Permalink
Query unigrams and bigrams in query
Browse files Browse the repository at this point in the history
  • Loading branch information
daoudclarke committed Nov 3, 2024
1 parent 6317727 commit 1efec6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ impl Ranker {
}
}

pub fn get_query_terms(&self) -> JsValue {
let tokens = self.query.split_whitespace().collect::<Vec<&str>>();
let bigrams = tokens.windows(2).map(|pair| pair.join(" ")).collect::<Vec<String>>();
let unique_tokens = tokens.iter().map(|s| s.to_string()).collect::<HashSet<String>>();
let unique_bigrams = bigrams.iter().collect::<HashSet<&String>>();
let mut terms = unique_tokens.iter().collect::<Vec<&String>>();
terms.extend(unique_bigrams.iter());
serde_wasm_bindgen::to_value(&terms).unwrap()
}

pub fn add_search_result(&mut self, url: &str, title: &str, extract: &str) {
self.search_results
.push(SearchResult::new(url, title, extract));
Expand Down
19 changes: 12 additions & 7 deletions www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ let searchButton = document.getElementById("search-button");
searchButton.addEventListener("click", async (e) => {
let searchTerm = searchInput.value;
let ranker = wasm.Ranker.new(searchTerm);
let response = await fetch(`https://api.mwmbl.org/api/v1/search/raw?s=${searchTerm}`);
let data = await response.json();
console.log("Data", data);
data.results.forEach((result) => {
ranker.add_search_result(result.url, result.title, result.extract);
});
let terms = ranker.get_query_terms();
console.log("Query terms", terms);
for (const term of terms) {
let response = await fetch(`https://api.mwmbl.org/api/v1/search/raw?s=${term}`);
let data = await response.json();
console.log("Data", data);
for (const result of data.results) {
ranker.add_search_result(result.url, result.title, result.extract);
}
}
let rankedData = ranker.rank();
console.log(rankedData);

Expand All @@ -26,9 +30,10 @@ searchButton.addEventListener("click", async (e) => {
rankedData.forEach((result) => {
let div = document.createElement("div");
div.innerHTML = `
<a href="${result.url}">${result.url}</a>
<h3>${result.title}</h3>
<p>${result.extract}</p>
<a href="${result.url}">result.url</a>
<br><br>
`;
outputDiv.appendChild(div);
});
Expand Down

0 comments on commit 1efec6d

Please sign in to comment.