如何使用普通 JavaScript 获取文本的单词密度?

这是我输入文本的地方:

http://img.mukewang.com/63247d450001dbe409300915.jpg

单击“计数”按钮后,它将转到此页面:

http://img2.mukewang.com/63247d4f0001c3c921560989.jpg

我的文字和字数被显示出来了。但是,我如何使用普通的JavaScript获取此文本的单词密度,并在此页面上实际显示它?


这是我的网页:


<!DOCTYPE html>

<html>

    <head>

        <title>Word Counter</title>

    </head>

    <body>

        <div id="input-page">

            <h1>Word Counter</h1>

            <form action="">

                <textarea id="text" type="text" rows="22" cols="60"></textarea>

                <br />

            </form>

            <button onclick="displayText()">COUNT</button>

        </div>


        <div id="count-page" style="display: none;">

            <h1>Your Text:</h1>

            <p id="display-text"></p>

            <div id="word-count"></div>

            <div id="word-density">

                <h1>Word Density:</h1>

            </div>

        </div>

    </body>

    <script src="app.js"></script>

</html>

脚本:


const displayText = () => {

  const inputPage = document.getElementById("input-page");

  const countPage = document.getElementById("count-page");

  const text = document.getElementById("text");

  const textValue = text.value;


  if (text.value !== "") { // normal flow will continue if the text-area is not empty

    inputPage.style.display = "none";

    document.getElementById("display-text").innerText = textValue;

    countPage.style.display = "block";

  } else { // if the text-area is empty, it will issue a warning.

    alert("Please enter some text first.")

  }


  const countWords = (str) => {

    return str.split(" ").length;

  };

  const wordCount = (countWords(textValue));


  const renderWordCount = () => {

    const wordCountDiv = document.getElementById("word-count");

    wordCountDiv.innerHTML = "<h1> Words Counted: " + wordCount + "</h1>";

  };


  renderWordCount();

};


倚天杖
浏览 83回答 1
1回答

一只甜甜圈

要获得@SimoneRossaini所说的单词密度,只需使用列表并保存找到每个单词的次数即可。例如,这最终是这样的:我修改了您的代码并添加了“密度”一词。const displayText = () => {&nbsp; const inputPage = document.getElementById("input-page");&nbsp; const countPage = document.getElementById("count-page");&nbsp; const text = document.getElementById("text");&nbsp; const textValue = text.value;&nbsp; if (text.value !== "") { // normal flow will continue if the text-area is not empty&nbsp; &nbsp; inputPage.style.display = "none";&nbsp; &nbsp; document.getElementById("display-text").innerText = textValue;&nbsp; &nbsp; countPage.style.display = "block";&nbsp; } else { // if the text-area is empty, it will issue a warning.&nbsp; &nbsp; alert("Please enter some text first.")&nbsp; }&nbsp; const countWords = (str) => {&nbsp; &nbsp; return str.split(" ").length;&nbsp; };&nbsp; const wordCount = (countWords(textValue));&nbsp; const renderWordCount = () => {&nbsp; &nbsp; const wordCountDiv = document.getElementById("word-count");&nbsp; &nbsp; wordCountDiv.innerHTML = "<h1> Words Counted: " + wordCount + "</h1>";&nbsp; };&nbsp;&nbsp;&nbsp; const getWordDensity = (str) => {&nbsp; &nbsp; let wordList = {};&nbsp; &nbsp; str.split(/[\s\.,]+/).forEach(word => {&nbsp; &nbsp; &nbsp; if(typeof wordList[word] == "undefined"){&nbsp; &nbsp; &nbsp; &nbsp; wordList[word] = 1;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; wordList[word]++;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; return wordList;&nbsp; };&nbsp; const wordDensity = (getWordDensity(textValue));&nbsp;&nbsp;&nbsp; const renderWordDensity = () => {&nbsp; &nbsp; const wordDensityDiv = document.getElementById("word-density");&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; let table = "<table>";&nbsp; &nbsp; for(let word in wordDensity){&nbsp; &nbsp; &nbsp; table += "<tr><td>" + word + "</td><td>" + wordDensity[word] + "</td></tr>";&nbsp; &nbsp; }&nbsp; &nbsp; table += "</table>";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; wordDensityDiv.innerHTML = "<h1> Word Density: </h1>" + table;&nbsp; };&nbsp; renderWordCount();&nbsp; renderWordDensity();};<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title>Word Counter</title>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <div id="input-page">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h1>Word Counter</h1>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form action="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <textarea id="text" type="text" rows="22" cols="60"></textarea>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button onclick="displayText()">COUNT</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div id="count-page" style="display: none;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h1>Your Text:</h1>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p id="display-text"></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div id="word-count"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div id="word-density"></div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body>&nbsp; &nbsp; <script src="app.js"></script></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript