Skip to content

web search done right

only show one definition in thesaurus widget with option to expand

Mikkel Denker4929f123e3a3
diff --git a/configs/api.toml b/configs/api.toml
index a87c5bf0..2b9664d9 100644
--- a/configs/api.toml
+++ b/configs/api.toml
@@ -3,7 +3,7 @@ crossencoder_model_path = "data/cross_encoder"
gossip_addr = "0.0.0.0:3005"
gossip_seed_nodes = ["0.0.0.0:3006", "0.0.0.0:3007"]
host = "0.0.0.0:3000"
-# lambda_model_path = "data/lambdamart.txt"
+lambda_model_path = "data/lambdamart.txt"
prometheus_host = "0.0.0.0:3001"
queries_csv_path = "data/queries_us.csv"
spell_checker_path = "data/web_spell/checker"
diff --git a/configs/search_server.toml b/configs/search_server.toml
index aee278bc..c2a3898b 100644
--- a/configs/search_server.toml
+++ b/configs/search_server.toml
@@ -7,7 +7,7 @@ host_centrality_store_path = "data/centrality"
index_path = "data/index"
shard_id = 0
# linear_model_path = "data/linear_model.json"
-# lambda_model_path = "data/lambdamart.txt"
+lambda_model_path = "data/lambdamart.txt"
[snippet]
max_considered_words = 10_000
diff --git a/core/src/widgets/thesaurus.rs b/core/src/widgets/thesaurus.rs
index 791d27f4..261df258 100644
--- a/core/src/widgets/thesaurus.rs
+++ b/core/src/widgets/thesaurus.rs
@@ -65,6 +65,8 @@ pub struct Info {
definition: Definition,
examples: Vec<Example>,
similar: Vec<Id>,
+ hyponyms: Vec<Id>,
+ hypernyms: Vec<Id>,
pos: PartOfSpeech,
}
@@ -108,6 +110,7 @@ impl Graph {
}
}
+#[derive(Debug)]
struct EdgeQuery<'a> {
to: NodeQuery<'a>,
label: &'a str,
@@ -118,6 +121,14 @@ struct NodeQuery<'a> {
graph: &'a Graph,
}
+impl std::fmt::Debug for NodeQuery<'_> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("NodeQuery")
+ .field("node", &self.node)
+ .finish()
+ }
+}
+
impl<'a> NodeQuery<'a> {
pub fn new(node: &'a Node, graph: &'a Graph) -> Self {
Self { node, graph }
@@ -215,6 +226,16 @@ impl Dictionary {
None
}
+ fn ids2lemmas(&self, ids: &[Id]) -> Vec<Lemma> {
+ ids.iter()
+ .filter_map(|id| self.get_by_id(id.clone()))
+ .filter_map(|i| self.reverse_lemmas.get(&i.id))
+ .flatten()
+ .filter_map(|l| self.spellings.get(l))
+ .cloned()
+ .collect()
+ }
+
pub fn lookup(&self, query: &str) -> Option<ThesaurusWidget> {
let query = self.query_lemma(query)?;
let lemma = Lemma(query);
@@ -234,16 +255,12 @@ impl Dictionary {
.push(WordMeaning {
definition: info.definition.clone(),
examples: info.examples.clone(),
- similar: info
- .similar
- .iter()
- .filter_map(|id| self.get_by_id(id.clone()))
- .filter_map(|i| self.reverse_lemmas.get(&i.id))
- .flatten()
- .filter_map(|l| self.spellings.get(l))
- .filter(|&l| l != &lemma)
- .cloned()
+ similar: self
+ .ids2lemmas(&info.similar)
+ .into_iter()
+ .filter(|l| l != &lemma)
.dedup_by(|a, b| a == b)
+ .take(5)
.collect(),
});
}
@@ -308,6 +325,7 @@ impl Dictionary {
let value_str_regex = regex::Regex::new(r#""(.*)"@"#).unwrap();
for entry in lexical_entries {
+ dbg!(&entry);
let entry = NodeQuery::new(entry, &graph);
let written_rep = entry
@@ -382,12 +400,26 @@ impl Dictionary {
.map(Example)
.collect();
+ dbg!(&concept.edges());
+
let similar: Vec<_> = concept
.filtered_edges(|e| e.label.contains("#similar"))
.into_iter()
.map(|e| Id(e.to.node.0.clone()))
.collect();
+ let hyponyms: Vec<_> = concept
+ .filtered_edges(|e| e.label.contains("#hyponym"))
+ .into_iter()
+ .map(|e| Id(e.to.node.0.clone()))
+ .collect();
+
+ let hypernyms: Vec<_> = concept
+ .filtered_edges(|e| e.label.contains("#hypernym"))
+ .into_iter()
+ .map(|e| Id(e.to.node.0.clone()))
+ .collect();
+
let pos = concept
.single_edge(|e| e.label.contains("#partOfSpeech"))
.unwrap()
@@ -414,6 +446,8 @@ impl Dictionary {
definition,
examples,
similar,
+ hyponyms,
+ hypernyms,
pos,
};
diff --git a/frontend/.svelte-kit/tsconfig.json b/frontend/.svelte-kit/tsconfig.json
index d5b888f0..96fee02c 100644
--- a/frontend/.svelte-kit/tsconfig.json
+++ b/frontend/.svelte-kit/tsconfig.json
@@ -22,12 +22,14 @@
],
"moduleResolution": "node",
"module": "esnext",
+ "noEmit": true,
"target": "esnext",
"ignoreDeprecations": "5.0"
},
"include": [
"ambient.d.ts",
"./types/**/$types.d.ts",
+ "../vite.config.js",
"../vite.config.ts",
"../src/**/*.js",
"../src/**/*.ts",
diff --git a/frontend/src/routes/search/ThesaurusWidget.svelte b/frontend/src/routes/search/ThesaurusWidget.svelte
index a158e9d4..1e35f77c 100644
--- a/frontend/src/routes/search/ThesaurusWidget.svelte
+++ b/frontend/src/routes/search/ThesaurusWidget.svelte
@@ -1,7 +1,46 @@
<script lang="ts">
- import type { PartOfSpeech, ThesaurusWidget } from '$lib/api';
+ import type { PartOfSpeech, ThesaurusWidget, WordMeaning } from '$lib/api';
+ import ChevronDown from '~icons/heroicons/chevron-down';
+ import ChevronUp from '~icons/heroicons/chevron-up';
+ import { slide } from 'svelte/transition';
+ import ThesaurusWidgetMeaning from './ThesaurusWidgetMeaning.svelte';
export let widget: ThesaurusWidget;
+ console.log(widget);
+
+ // convert meanings into a list of either pos or meanings
+ enum MeaningType {
+ pos,
+ meaning,
+ }
+ interface Pos {
+ type: MeaningType.pos;
+ pos: PartOfSpeech;
+ }
+
+ interface Meaning {
+ type: MeaningType.meaning;
+ meaning: WordMeaning
+ }
+
+ type MeaningOrPos = Pos | Meaning;
+
+ $: meanings = widget.meanings.flatMap((meaning) => {
+ return [
+ { type: MeaningType.pos, pos: meaning.pos },
+ ...meaning.meanings.map((meaning) => ({
+ type: MeaningType.meaning,
+ meaning: meaning,
+ })) as MeaningOrPos[],
+ ];
+ }) as MeaningOrPos[];
+
+ $: nonExpandedMeanings = meanings.slice(0, 2);
+ $: expandedMeanings = meanings.slice(2);
+
+
+ $: expanded = false;
+
const posName = (pos: PartOfSpeech): String => {
switch (pos) {
@@ -22,41 +61,42 @@
<div class="rounded-xl border pb-1 pl-5 pr-3 pt-5">
<div class="text-neutral-focus">
<h2 class="text-2xl font-bold">{widget.term}</h2>
- <div class="flex flex-col space-y-2">
- {#each widget.meanings as { pos, meanings }}
- <div>
+ <div class="flex flex-col space-y-3 transition">
+ {#each nonExpandedMeanings as m}
+ {#if m.type == MeaningType.pos}
+ <div class="text-sm italic">
+ {posName(m.pos)}
+ </div>
+ {:else if m.type == MeaningType.meaning}
+ <ThesaurusWidgetMeaning meaning={m.meaning} />
+ {/if}
+ {/each}
+ {#if expanded}
+ <div transition:slide={{duration: 200}} class="space-y-3">
+ {#each expandedMeanings as m}
+ {#if m.type == MeaningType.pos}
<div class="text-sm italic">
- {posName(pos)}
+ {posName(m.pos)}
</div>
- <ol class="list-decimal space-y-2 pl-5">
- {#each meanings as meaning}
- <li>
- <div>
- {meaning.definition}
- </div>
- {#if meaning.similar.length > 0}
- <div class="flex space-x-1 text-sm">
- <div class="font-medium text-primary-focus">Similar:</div>
- <div class="inline-block space-x-1">
- {#each meaning.similar as similar}
- <div class="float-left inline [&:not(:last-child)]:after:content-[',']">
- <a
- class="float-left hover:underline"
- href="/search?q={encodeURIComponent('definition of ' + similar)}"
- >
- {similar}
- </a>
- </div>
- {/each}
- </div>
- </div>
- {/if}
- </li>
- {/each}
- </ol>
- </div>
+ {:else if m.type == MeaningType.meaning}
+ <ThesaurusWidgetMeaning meaning={m.meaning} />
+ {/if}
{/each}
+ </div>
+ {/if}
</div>
+ {#if expandedMeanings.length > 0}
+ <button
+ class="h-6 w-6 cursor-pointer rounded-full text-primary"
+ aria-label={expanded ? "Show less word definitions" : "Show more word definitions"}
+ on:click={() => expanded = !expanded}>
+ {#if expanded}
+ <ChevronUp />
+ {:else}
+ <ChevronDown />
+ {/if}
+ </button>
+ {/if}
<div class="float-right mt-1 text-[0.75rem] italic text-neutral">
Data from <a href="https://en-word.net/" class="hover:underline">Open English WordNet</a> and
<a href="https://wordnet.princeton.edu/" class="hover:underline">Princeton WordNet</a>
diff --git a/frontend/src/routes/search/ThesaurusWidgetMeaning.svelte b/frontend/src/routes/search/ThesaurusWidgetMeaning.svelte
new file mode 100644
index 00000000..56100d12
--- /dev/null
+++ b/frontend/src/routes/search/ThesaurusWidgetMeaning.svelte
@@ -0,0 +1,31 @@
+<script lang="ts">
+ import type { WordMeaning } from "$lib/api";
+
+ export let meaning: WordMeaning;
+</script>
+<div>
+ <div>
+ {meaning.definition}
+ </div>
+ {#if meaning.similar.length > 0}
+ <div class="flex space-x-1 text-sm">
+ <div class="font-medium text-primary-focus">Similar:</div>
+ <div class="inline-block space-x-1">
+ {#each meaning.similar as similar}
+ <div class="float-left inline [&:not(:last-child)]:after:content-[',']">
+ <a
+ class="float-left hover:underline"
+ href="/search?q={encodeURIComponent('definition of ' + similar)}"
+ >
+ {similar}
+ </a>
+ </div>
+ {/each}
+ </div>
+ </div>
+ {:else if meaning.examples.length > 0}
+ <div class="text-sm text-neutral-focus/60">
+ "{meaning.examples[0]}"
+ </div>
+ {/if}
+</div>
\ No newline at end of file