Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

An API and basic interface for entity searching from DBPedia, enhanced with images

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { createRoot } from "https://esm.sh/react-dom@18.2.0/client";
import React, { useEffect, useState } from "https://esm.sh/react@18.2.0";
function sanitizeHtml(html) {
return html ? html.replace(/<\/?[^>]+(>|$)/g, "") : "";
}
function calculateScore(result, query) {
let score = 0;
let explanation = [];
// Exact match in label
if (result.label && result.label[0].toLowerCase() === query.toLowerCase()) {
score += 10;
explanation.push("Exact label match: +10");
}
// Partial match in label
if (result.label && result.label[0].toLowerCase().includes(query.toLowerCase())) {
score += 5;
explanation.push("Partial label match: +5");
}
// Score based on refCount
if (result.refCount && result.refCount[0]) {
const refScore = Math.log(parseInt(result.refCount[0]) + 1) * 2;
score += refScore;
explanation.push(`Reference count (${result.refCount[0]}): +${refScore.toFixed(2)}`);
}
// Score based on important types
const importantTypes = ["Person", "Place", "Organization", "Country", "City"];
if (result.type) {
const matchedTypes = result.type.filter(type =>
importantTypes.some(impType => type.includes(impType))
);
if (matchedTypes.length > 0) {
score += matchedTypes.length * 3;
explanation.push(`Important types (${matchedTypes.length}): +${matchedTypes.length * 3}`);
}
}
return { score, explanation };
}
function App() {
const [query, setQuery] = useState("");
const [results, setResults] = useState([]);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
const urlQuery = urlParams.get("query");
if (urlQuery) {
setQuery(urlQuery);
fetchResults(urlQuery);
}
}, []);
const fetchResults = async (searchQuery) => {
setLoading(true);
setError(null);
try {
const response = await fetch(`/api/search?query=${encodeURIComponent(searchQuery)}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
setResults(data.results);
} catch (e) {
console.error("Error fetching results:", e);
setError("An error occurred while fetching results. Please try again.");
} finally {
setLoading(false);
}
};
const handleSubmit = (e) => {
e.preventDefault();
if (query) {
window.history.pushState({}, "", `?query=${encodeURIComponent(query)}`);
fetchResults(query);
}
};
return React.createElement(
"div",
{ className: "container mx-auto px-4 py-8" },
React.createElement("h1", { className: "text-3xl font-bold text-center mb-8" }, "DBpedia Entity Search"),
React.createElement(
"form",
{ onSubmit: handleSubmit, className: "flex mb-8" },
React.createElement("input", {
type: "text",
value: query,
onChange: (e) => setQuery(e.target.value),
placeholder: "Enter your search query",
className: "flex-grow p-2 border border-gray-300 rounded-l-md focus:outline-none focus:ring-2 focus:ring-blue-500",
}),
ejfox-dbpediaenhanced.web.val.run
September 9, 2024