Spaces:
Running
Running
Update index.html
Browse files- index.html +94 -18
index.html
CHANGED
@@ -1,19 +1,95 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
</html>
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<title>3D Word Embeddings Visualization</title>
|
6 |
+
<style>
|
7 |
+
body { margin: 0; }
|
8 |
+
canvas { display: block; }
|
9 |
+
#info {
|
10 |
+
position: absolute;
|
11 |
+
top: 10px;
|
12 |
+
left: 10px;
|
13 |
+
background: rgba(255,255,255,0.8);
|
14 |
+
padding: 10px;
|
15 |
+
border-radius: 4px;
|
16 |
+
}
|
17 |
+
</style>
|
18 |
+
</head>
|
19 |
+
<body>
|
20 |
+
<div id="info">Click on a word to see details</div>
|
21 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
22 |
+
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
|
23 |
+
<script>
|
24 |
+
let scene, camera, renderer, controls;
|
25 |
+
let raycaster = new THREE.Raycaster();
|
26 |
+
let mouse = new THREE.Vector2();
|
27 |
+
let spheres = []; // To keep track of word nodes
|
28 |
+
|
29 |
+
init();
|
30 |
+
animate();
|
31 |
+
|
32 |
+
function init() {
|
33 |
+
// Create scene and camera
|
34 |
+
scene = new THREE.Scene();
|
35 |
+
camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
|
36 |
+
camera.position.z = 100;
|
37 |
+
|
38 |
+
// Renderer
|
39 |
+
renderer = new THREE.WebGLRenderer({antialias: true});
|
40 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
41 |
+
document.body.appendChild(renderer.domElement);
|
42 |
+
|
43 |
+
// Controls for interactive orbiting
|
44 |
+
controls = new THREE.OrbitControls(camera, renderer.domElement);
|
45 |
+
|
46 |
+
// Load JSON data with word embeddings
|
47 |
+
fetch('fasttext_3d.json')
|
48 |
+
.then(response => response.json())
|
49 |
+
.then(data => {
|
50 |
+
// Normalize coordinates (optional, depending on your TSNE results)
|
51 |
+
data.forEach(item => {
|
52 |
+
// Create a sphere for each word
|
53 |
+
let geometry = new THREE.SphereGeometry(1.5, 16, 16);
|
54 |
+
let material = new THREE.MeshBasicMaterial({ color: 0x0077ff });
|
55 |
+
let sphere = new THREE.Mesh(geometry, material);
|
56 |
+
sphere.position.set(item.x, item.y, item.z);
|
57 |
+
sphere.userData = { word: item.word };
|
58 |
+
scene.add(sphere);
|
59 |
+
spheres.push(sphere);
|
60 |
+
});
|
61 |
+
})
|
62 |
+
.catch(err => console.error(err));
|
63 |
+
|
64 |
+
// Add event listener for mouse click
|
65 |
+
window.addEventListener('click', onMouseClick, false);
|
66 |
+
}
|
67 |
+
|
68 |
+
function onMouseClick(event) {
|
69 |
+
// Calculate mouse position in normalized device coordinates (-1 to +1)
|
70 |
+
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
|
71 |
+
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
|
72 |
+
|
73 |
+
raycaster.setFromCamera(mouse, camera);
|
74 |
+
|
75 |
+
// Calculate objects intersecting the picking ray
|
76 |
+
let intersects = raycaster.intersectObjects(spheres);
|
77 |
+
if (intersects.length > 0) {
|
78 |
+
let selected = intersects[0].object;
|
79 |
+
let infoDiv = document.getElementById("info");
|
80 |
+
infoDiv.innerHTML = "Word: " + selected.userData.word +
|
81 |
+
"<br>Position: (" +
|
82 |
+
selected.position.x.toFixed(2) + ", " +
|
83 |
+
selected.position.y.toFixed(2) + ", " +
|
84 |
+
selected.position.z.toFixed(2) + ")";
|
85 |
+
}
|
86 |
+
}
|
87 |
+
|
88 |
+
function animate() {
|
89 |
+
requestAnimationFrame(animate);
|
90 |
+
controls.update();
|
91 |
+
renderer.render(scene, camera);
|
92 |
+
}
|
93 |
+
</script>
|
94 |
+
</body>
|
95 |
</html>
|