第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用普通 JavaScript 獲取文本的單詞密度?

如何使用普通 JavaScript 獲取文本的單詞密度?

倚天杖 2022-09-16 21:41:52
這是我輸入文本的地方:單擊“計數(shù)”按鈕后,它將轉(zhuǎn)到此頁面:我的文字和字?jǐn)?shù)被顯示出來了。但是,我如何使用普通的JavaScript獲取此文本的單詞密度,并在此頁面上實(shí)際顯示它?這是我的網(wǎng)頁:<!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();};
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個贊

要獲得@SimoneRossaini所說的單詞密度,只需使用列表并保存找到每個單詞的次數(shù)即可。例如,這最終是這樣的:

http://img1.sycdn.imooc.com//63247d640001d67102100230.jpg

我修改了您的代碼并添加了“密度”一詞。


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>";

  };

  

  const getWordDensity = (str) => {

    let wordList = {};

    str.split(/[\s\.,]+/).forEach(word => {

      if(typeof wordList[word] == "undefined"){

        wordList[word] = 1;

      }

      else{

        wordList[word]++;

      }

    });

    return wordList;

  };

  const wordDensity = (getWordDensity(textValue));

  

  const renderWordDensity = () => {

    const wordDensityDiv = document.getElementById("word-density");

    

    let table = "<table>";

    for(let word in wordDensity){

      table += "<tr><td>" + word + "</td><td>" + wordDensity[word] + "</td></tr>";

    }

    table += "</table>";

    

    wordDensityDiv.innerHTML = "<h1> Word Density: </h1>" + table;

  };


  renderWordCount();

  renderWordDensity();

};

<!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"></div>

        </div>

    </body>

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

</html>


查看完整回答
反對 回復(fù) 2022-09-16
  • 1 回答
  • 0 關(guān)注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號