Spaces:
Running
Running
File size: 7,533 Bytes
b9aa460 |
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
"use client";
import { useState } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { gameData } from "@/lib/game-data";
import { Grid3X3, Target } from "lucide-react";
interface HoveredCell {
word1: string;
word2: string;
distance: number;
}
export function ScoreMatrixVisualization() {
const [hoveredCell, setHoveredCell] = useState<HoveredCell | null>(null);
const { words, distances, finalScore } = gameData.finalMatrix;
const getColorIntensity = (distance: number) => {
const maxDistance = Math.max(...distances.flat());
const minDistance = Math.min(...distances.flat().filter(d => d > 0));
const normalizedDistance = (distance - minDistance) / (maxDistance - minDistance);
if (distance === 0) return "bg-gray-200";
const intensity = Math.floor(normalizedDistance * 4) + 1;
return `bg-green-${intensity * 100} hover:bg-green-${Math.min(intensity * 100 + 100, 900)}`;
};
const getCellClasses = (distance: number) => {
if (distance === 0) {
return "bg-gray-200 text-gray-500";
}
const maxDistance = Math.max(...distances.flat());
const intensity = distance / maxDistance;
if (intensity > 0.8) return "bg-green-500 text-white hover:bg-green-600";
if (intensity > 0.6) return "bg-green-400 text-white hover:bg-green-500";
if (intensity > 0.4) return "bg-green-300 text-gray-900 hover:bg-green-400";
if (intensity > 0.2) return "bg-green-200 text-gray-900 hover:bg-green-300";
return "bg-green-100 text-gray-900 hover:bg-green-200";
};
return (
<Card className="w-full">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Grid3X3 className="h-5 w-5" />
Semantic Distance Matrix
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-4">
<div className="flex items-center justify-between">
<p className="text-sm text-gray-600">
Interactive matrix showing semantic distances between final word pairs
</p>
<div className="flex items-center gap-2">
<Target className="h-4 w-4 text-green-600" />
<Badge className="bg-green-100 text-green-800">
Score: {finalScore}
</Badge>
</div>
</div>
<div className="overflow-x-auto">
<div className="inline-block min-w-full">
<table className="w-full">
<thead>
<tr>
<th className="p-2"></th>
{words.map((word, index) => (
<th key={index} className="p-2 text-xs font-medium text-gray-600 min-w-[60px]">
<div className="transform -rotate-45 origin-bottom-left whitespace-nowrap">
{word}
</div>
</th>
))}
</tr>
</thead>
<tbody>
{words.map((rowWord, rowIndex) => (
<tr key={rowIndex}>
<td className="p-2 text-xs font-medium text-gray-600 sticky left-0 bg-white">
<div className="max-w-[80px] truncate" title={rowWord}>
{rowWord}
</div>
</td>
{words.map((colWord, colIndex) => {
const distance = distances[rowIndex][colIndex];
return (
<td
key={colIndex}
className="p-1"
onMouseEnter={() => setHoveredCell({
word1: rowWord,
word2: colWord,
distance
})}
onMouseLeave={() => setHoveredCell(null)}
>
<div
className={`
w-12 h-12 flex items-center justify-center text-xs font-medium
rounded cursor-pointer transition-colors duration-200
${getCellClasses(distance)}
`}
>
{distance}
</div>
</td>
);
})}
</tr>
))}
</tbody>
</table>
</div>
</div>
{hoveredCell && (
<div className="p-3 bg-blue-50 border border-blue-200 rounded-lg">
<div className="text-sm">
<span className="font-medium text-blue-900">
{hoveredCell.word1} ↔ {hoveredCell.word2}
</span>
<span className="text-blue-700 ml-2">
Distance: {hoveredCell.distance}
</span>
</div>
<p className="text-xs text-blue-600 mt-1">
{hoveredCell.distance === 0
? "Same word (diagonal)"
: `Higher values indicate more semantically distant concepts`
}
</p>
</div>
)}
<div className="flex items-center justify-between text-xs text-gray-500">
<div className="flex items-center gap-4">
<div className="flex items-center gap-1">
<div className="w-3 h-3 bg-green-100 rounded"></div>
<span>Low distance</span>
</div>
<div className="flex items-center gap-1">
<div className="w-3 h-3 bg-green-300 rounded"></div>
<span>Medium distance</span>
</div>
<div className="flex items-center gap-1">
<div className="w-3 h-3 bg-green-500 rounded"></div>
<span>High distance</span>
</div>
</div>
<span>Matrix is symmetric</span>
</div>
<div className="p-4 bg-gray-50 rounded-lg">
<h4 className="font-medium text-gray-900 mb-2">Matrix Analysis</h4>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 text-sm">
<div>
<span className="font-medium text-gray-700">Average Distance:</span>
<div className="text-lg font-semibold text-green-600">
{(distances.flat().filter(d => d > 0).reduce((a, b) => a + b, 0) / distances.flat().filter(d => d > 0).length).toFixed(1)}
</div>
</div>
<div>
<span className="font-medium text-gray-700">Highest Distance:</span>
<div className="text-lg font-semibold text-blue-600">
{Math.max(...distances.flat())}
</div>
</div>
<div>
<span className="font-medium text-gray-700">Lowest Distance:</span>
<div className="text-lg font-semibold text-orange-600">
{Math.min(...distances.flat().filter(d => d > 0))}
</div>
</div>
</div>
</div>
</div>
</CardContent>
</Card>
);
} |