句子单词统计 Key→Value 动态可视化

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>单词统计 Key→Value 动态可视化</title>
<style>
  body {
    font-family: Arial, sans-serif;
    padding: 20px;
    text-align: center;
  }

  #inputArea {
    margin-bottom: 20px;
  }

  #kvContainer {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 20px;
    max-width: 400px;
    margin-left: auto;
    margin-right: auto;
  }

  .kvPair {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 5px 10px;
    border: 1px solid #888;
    border-radius: 5px;
    transition: background-color 0.3s, color 0.3s;
  }

  .kvPair.active {
    background-color: orange;
    color: white;
  }

  .key, .value {
    min-width: 60px;
    text-align: center;
    font-weight: bold;
  }

  .arrow {
    font-size: 16px;
  }

  #controls {
    margin-top: 20px;
  }

  input[type="text"] {
    width: 300px;
    padding: 5px;
    font-size: 16px;
  }

  button {
    padding: 5px 15px;
    font-size: 16px;
    margin-left: 10px;
    cursor: pointer;
  }
</style>
</head>
<body>

<h2>句子单词统计 Key→Value 动态可视化</h2>
<div id="inputArea">
  <input type="text" id="sentenceInput" value="I love coding and I love teaching">
  <button id="startBtn">开始</button>
</div>

<div id="kvContainer">
  <!-- KeyValuePair 会动态生成 -->
</div>

<div id="controls">
  <button id="prevBtn">上一步</button>
  <button id="nextBtn">下一步</button>
</div>

<script>
const sentenceInput = document.getElementById('sentenceInput');
const startBtn = document.getElementById('startBtn');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const kvContainer = document.getElementById('kvContainer');

let steps = [];        // 保存每一步 Map 状态
let currentStep = -1;
let words = [];
let wordMap = new Map();

// 重置可视化
function resetVisualization() {
  kvContainer.innerHTML = '';
  steps = [];
  currentStep = -1;
  wordMap.clear();
}

// 生成每步状态
function recordSteps(sentence) {
  words = sentence.trim().split(/\s+/);
  wordMap.clear();
  steps = [];
  for (let i = 0; i < words.length; i++) {
    const word = words[i].toLowerCase();
    wordMap.set(word, (wordMap.get(word) || 0) + 1);
    const snapshot = new Map(wordMap); // 保存 Map 副本
    steps.push({
      current: word,
      map: snapshot
    });
  }
}

// 渲染指定步骤
function renderStep(stepIndex) {
  if (stepIndex < 0 || stepIndex >= steps.length) return;

  const step = steps[stepIndex];
  const currentWord = step.current;
  const map = step.map;

  kvContainer.innerHTML = ''; // 清空容器

  map.forEach((value, key) => {
    const kvDiv = document.createElement('div');
    kvDiv.classList.add('kvPair');
    if (key === currentWord) kvDiv.classList.add('active');

    const keyDiv = document.createElement('div');
    keyDiv.classList.add('key');
    keyDiv.innerText = key;

    const arrowDiv = document.createElement('div');
    arrowDiv.classList.add('arrow');
    arrowDiv.innerText = '→';

    const valueDiv = document.createElement('div');
    valueDiv.classList.add('value');
    valueDiv.innerText = value;

    kvDiv.appendChild(keyDiv);
    kvDiv.appendChild(arrowDiv);
    kvDiv.appendChild(valueDiv);

    kvContainer.appendChild(kvDiv);
  });

  currentStep = stepIndex;
}

// 按钮事件
startBtn.addEventListener('click', () => {
  const sentence = sentenceInput.value.trim();
  if (!sentence) {
    alert('请输入句子!');
    return;
  }
  resetVisualization();
  recordSteps(sentence);
  renderStep(0);
});

nextBtn.addEventListener('click', () => {
  if (currentStep < steps.length - 1) {
    renderStep(currentStep + 1);
  }
});

prevBtn.addEventListener('click', () => {
  if (currentStep > 0) {
    renderStep(currentStep - 1);
  }
});
</script>

</body>
</html>
相关推荐
1104.北光c°1 小时前
滑动窗口HotKey探测机制:让你的缓存TTL更智能
java·开发语言·笔记·程序人生·算法·滑动窗口·hotkey
wuhen_n2 小时前
网络请求在Vite层的代理与Mock:告别跨域和后端依赖
前端·javascript·vue.js
for_ever_love__2 小时前
Objective-C学习 NSSet 和 NSMutableSet 功能详解
开发语言·学习·ios·objective-c
用户69371750013847 小时前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦7 小时前
Web 前端搜索文字高亮实现方法汇总
前端
用户69371750013847 小时前
Room 3.0:这次不是升级,是重来
android·前端·google
似水明俊德8 小时前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
漫随流水8 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
Thera7779 小时前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
踩着两条虫9 小时前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程