【问题标题】:How do I find/target the most frequent word in .txt file and change it如何在 .txt 文件中查找/定位最常用的单词并更改它
【发布时间】:2016-09-14 09:50:35
【问题描述】:

我正在尝试找出如何在文本文件中找到一个最常用的词并更改该单个词以使其包含在其他东西中,例如:freewordchoice(免费+常用词+选择)和无处不在该单词是该单词可以更改的文本的文本。我一直在为这样的事情发疯,但我找不到它。我对 Javascript 非常陌生,这正是我想要使用的。要上传和显示文本工作正常,我不明白的是我如何定位最常用的单词并在它实际显示在浏览器上之前在整个文本中更改它。在我看来,我需要某种变量来找到单词并在某个地方存储世界,以及一个变量,我可以在其中放入要添加或更改目标单词的内容。

示例文本:阿拉丁和神灯的古腾堡计划 Etext

信息/问题更新:下面的代码在上面的示例文本的全文中查找最常用的词。我现在这个词是阿拉丁。问题是我可以让它正确地替换 Aladdin 这个词。我确实打印出 fooAladdinbar,就像我想要的那样,但不是仅仅更改 Aladding = fooAladdinbar,而是在示例文本中的每个字母之间添加 fooAladdinbar。

这已解决,是一个可变问题。

【问题讨论】:

  • 你有一些测试数据吗?文本文件会是什么样子?什么编码?哪些语言有特殊字符等等?
  • 我更新了我的答案以涵盖您问题的替换部分。
  • 这是我正在处理的演示:internetstall.nu/demo/demo.html,我只是上传了一个包含文本的简单 .txt 文件。不确定您所说的语言特殊字符是什么意思,我正在尝试使用 javascript 完成此操作,但有些事情告诉我这不是您的意思。
  • 问题已经解决了吗?
  • 不,但我刚刚更新了代码,因为我最终改变了一点。

标签: javascript word-frequency


【解决方案1】:

这并不完美,但可以,这是一个演示:

(这个Demo只是找常用词)

  • 它使用正则表达式分割文本
  • 然后数单词
  • 然后返回最常用的词

var data = document.getElementById("data").value;

var allWords = data.split(/\b/);
var wordCountList = {};

allWords.forEach(function(word){
  if(word !== " "){
    if(!wordCountList.hasOwnProperty(word)){
      wordCountList[word] = {word: word, count:0};
    }
    wordCountList[word].count++;
  }
})


var maxCountWord = {count:0};
for(var propName in wordCountList){
  var currentWord = wordCountList[propName];
  if(maxCountWord.count<currentWord.count){
    maxCountWord = currentWord;
  }
}
console.info(maxCountWord);
textarea{
  width:100%;
  height:100px;
}
<textarea id="data" >
<!-- start slipsum code -->

The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.

<!-- end slipsum code -->  
</textarea>

<div id="result"></div>

替换这个词你也可以用正则表达式:
(这个Demo只是替换了一个常用词)

function freewordchoice (free, word, choice){
  var data = document.getElementById("data").innerHTML;   
  var replaceExpression = new RegExp("\\b"+word+"\\b","gi");
  console.info(replaceExpression);
  data =data.replace(replaceExpression, free + word + choice);
  document.getElementById("result").innerHTML = data;   
   
 }


freewordchoice("<b>", "the", "</b>");
<b>Before:</b>
<div id="data" >
<!-- start slipsum code -->

The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.

<!-- end slipsum code -->  
</div>
<br/><br/>
<b>After:</b>
 <div id="result" >
   
   </div>

更新:

问题出在这一行

common = 'the,a,do,in,with,this,so,that,of,and,not,did,when,what,were,went,was,as,  
if,who,had,at,can,you,which,while,will,to,till,then,them,their,she,  
he,once,out,no,must,many,me,is,it,his,him,her,about,have,i,has,your,  
would,where,whom,s,on,from,for,by,but,all,said,my,';

问题出在字符串末尾,said,my,'; 删除最后一个逗号,它应该可以工作,如下所示:

common = 'the,a,do,in,with,this,so,that,of,and,not,did,when,what,were,went,was,as,  
if,who,had,at,can,you,which,while,will,to,till,then,them,their,she,  
he,once,out,no,must,many,me,is,it,his,him,her,about,have,i,has,your,  
would,where,whom,s,on,from,for,by,but,all,said,my';

由于通过最后一个逗号,最后一个单词是一个空字符串。

【讨论】:

  • 当我尝试运行它时,我只得到错误,我看到数据部分中有一个字符串,但是我如何使它与上传到浏览器的 .txt 文件一起工作,就像我在这里做的那样我正在做这个演示:internetstall.nu/demo/demo.html
  • 此脚本文件未加载,请检查浏览器控制台(按F12)。
  • @user3481279 你看到我最后的评论了吗?脚本文件没有被加载。我的解决方案有帮助吗?还是需要更多帮助?
  • 按 F12 并没有告诉我太多,也不知道我在寻找什么。我剩下的唯一一件事就是弄清楚regExp,它完成了我想要的一半。但是,它不仅仅是将最常用的单词更改为 fooaladdinbar(如果最常用的单词是 aladdin 那么),而是在字符串的每个字母之间的 fooaladdinbar 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多