2016-06-30 2 views
4

안녕하세요, iv javascript에서 단어 유형 모델의 간단한 가방을 개발하는 방법을 살펴 보았지만 몇 가지 예를 둘러 보면서 시간을 보냈습니다. jnode 또는 browserify가 내가 본 것으로부터 설치되도록 요구합니다. 텍스트를 읽고, 분할하고, 텍스트에서 가장 자주 사용되는 단어를 가져 오려고합니다. 그러나 JavaScript 값을 반환하기 위해 javascript의 배열 객체를 사용하는 데 문제가 있습니다. 지금까지 번호가 지정된 인덱스 만 반환 할 수 있습니다.배열에서 발생 횟수를 계산하고 최고 값을 얻는 간단한 방법

function bagOfWords(text){ 
text=text.toLowerCase(); //make everything lower case 
var bag = text.split(" "); //remove blanks 

//count duplicates 
var map = bag.reduce(function(prev, cur) { 
    prev[cur] = (prev[cur] || 0) + 1; 
    return prev; 
}, {}); 


var arr = Object.keys(map).map(function (key) { return map[key]; }); //index based on values to find top 10 possible tags 
arr=arr.sort(sortNumber); //sort the numbered array 

var top10 = new Array(); //the final array storing the top 10 elements 
for (i = arr.length; top10.length < 10; i--) { 
if(top10.length<10){ 
top10.push(arr[i]);} 

} 

} 

reduce 메소드를 사용하면 인덱스를 반복하고 원본 정렬 된 텍스트를 참조하지 않고도 reduce 메소드를 사용하여 상위 10 개 단어를 찾고, 계산하고 검색하는 간단한 방법이 있습니까?

+1

배열을 사용하지 않고지도를 사용하십시오 (반드시 [ES6 Map] (https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map))'var map = {}'그리고 현재 단어를 맵 키로 사용합니다.'var count = map [word]; if (count === undefined) count = 1; else count + = 1; map [word] = count;'그러나이 방법을 사용하면 맵에서 모든 것을 반복하여 가장 큰 숫자를 찾아야합니다. –

+0

아, 좋은 생각, 모든 도움에 감사드립니다! – D3181

답변

2

문제에 대한 좋은 reduce 해결책이 있는지 모르겠어요,하지만 난 알고리즘을 마련했습니다

  1. 정렬 모든 단어

    ,이 배열을 복제.
  2. 정렬 복제 배열 lastIndexOf()indexOf()를 사용하여 발생 역순 워드의 정렬 된 목록.
  3. filter() 중복을 제거하기위한 새로운 배열.
  4. slice() 필터 된 배열을 첫 10 단어로 제한합니다.

발췌문 :

function bagOfWords(text) { 
 
    var bag = text. 
 
       toLowerCase(). 
 
       split(' '). 
 
       sort(), 
 
     clone = bag.slice(); //needed because sort changes the array in place 
 
      
 
    return bag. 
 
      sort(function(a, b) { //sort in reverse order of occurrence 
 
    \t  return (clone.lastIndexOf(b) - clone.indexOf(b) + 1) - 
 
     \t    (clone.lastIndexOf(a) - clone.indexOf(a) + 1); 
 
    \t }). 
 
      filter(function(word, idx) { //remove duplicates 
 
      return bag.indexOf(word) === idx; 
 
      }). 
 
      slice(0, 10); //first 10 elements 
 
} //bagOfWords 
 

 
console.log(bagOfWords('four eleven two eleven ten nine one six seven eleven nine ten seven four seven six eleven nine five ten seven six eleven nine seven three five ten eleven six nine two five seven ten eleven nine six three eight eight eleven nine ten eight three eight five eleven eight ten nine four four eight eleven ten five eight six seven eight nine ten ten eleven ')); 
 

 
console.log(bagOfWords('Four score and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal Now we are engaged in a great civil war testing whether that nation or any nation so conceived and so dedicated can long endure We are met on a great battle-field of that war We have come to dedicate a portion of that field as a final resting place for those who here gave their lives that that nation might live It is altogether fitting and proper that we should do this But in a larger sense we can not dedicate we can not consecrate we can not hallow this ground The brave men living and dead who struggled here have consecrated it far above our poor power to add or detract The world will little note nor long remember what we say here but it can never forget what they did here It is for us the living rather to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced It is rather for us to be here dedicated to the great task remaining before us that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion that we here highly resolve that these dead shall not have died in vain that this nation under God shall have a new birth of freedom and that government of the people by the people for the people shall not perish from the earth'));

1

Array.prototype.reduce()를 사용해야한다는 요구 사항이 있습니까? 이 방법은 전체 요소 배열을 하나의 값으로 줄이기위한 것으로, 사용 사례를 충족시키는 것처럼 들리지 않습니다. 단순히 단어의 수를 계산하려면 사전을 사용하는 것이 좋습니다.

function bagOfWords(text, topCnt) { 
 
    text= text.toLowerCase(); //make everything lower case 
 
    var bag = text.split(" "); //remove blanks 
 
    //Remove "." and possibly other punctuation? 
 

 
    //build the word dictionary; 
 
    var wordDict = {}; 
 
    for(idx in bag) { 
 
    if(bag[idx] in wordDict) { 
 
     wordDict[bag[idx]]++; 
 
    } 
 
    else { 
 
     wordDict[bag[idx]] = 1; 
 
    } 
 
    } 
 
    //get the duplicate free array 
 
    var dupFree = []; 
 
    for(word in wordDict) { 
 
    dupFree.push(word); 
 
    } 
 
    //find the top topCnt; 
 
    //Custom sort method to sort the array based on the dict we created. 
 
    var sorted = dupFree.sort(function(a, b) { 
 
    if (wordDict[a] > wordDict[b]) { 
 
     return -1; 
 
    } 
 
    if (wordDict[a] < wordDict[b]) { 
 
     return 1; 
 
    } 
 
    return 0; 
 
    }); 
 
    
 
    //Now we can just return back the spliced array. 
 
    //NOTE - if there is a tie, it would not return all the ties. 
 
    // For instance, if there were twenty words with each having the same occurance, this would not return back all 20 of them. To do that, you would need another loop. 
 
    return sorted.slice(0,topCnt); 
 
} 
 

 
    var lorem = "Lorem ipsum dolor sit amet consectetur adipiscing elit Duis gravida, lectus vel semper porttitor nulla nulla semper tortor et maximus quam orci a nibh Duis vel aliquet est Aliquam at elit libero Duis molestie nisi et lectus fringilla vulputate Integer in faucibus dolor Vivamus leo magna, interdum sit amet arcu et vulputate aliquam elit Pellentesque vel imperdiet nisi maximus malesuada eros Aenean sit amet turpis lorem Pellentesque in scelerisque ante Nunc sed dignissim ex Quisque neque risus feugiat a felis vitae blandit tristique mauris Etiam pharetra eleifend felis ac cursus Pellentesque ac magna nec lectus interdum lacinia Fusce imperdiet libero accumsan dolor consectetur, sed finibus justo ornare. Vivamus vehicula ornare metus quis fermentum sapien ullamcorper non Cras non odio interdum facilisis elit sit amet facilisis risus"; 
 
console.log(bagOfWords(lorem,10));

확실히 내가 코멘트에 언급 한 바와 같이, 될 수있는 몇 가지 개선 사항이 있습니다. 최소한이 일을 시작해야합니다. 여기의 마술은 사전을 사용하여 중복을 제거하고 어커런스를 계산 한 다음 사용자 정의 정렬 함수를 사용하여 원하는 순서대로 배열을 정렬합니다.

모든 자바 스크립트 기능 요구 사항에 대해 MDN을 확인하십시오. 이 사이트는 놀라운 자원입니다. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

+0

도움을 주셔서 감사합니다. 귀하의 샘플 및 링크는이 문제, 특히 귀하의 의견을 해결하는 데 도움이되었습니다. 다른 어떤 것도 귀하의 프로세스를 따르고 이해하는 데 정말로 도움이되는 의견을 가진 귀하의 의견과 크게 다르지 않았습니다. – D3181

2

당신은, 매개 변수 0, 10Array.protototype.slice() 배열을 갖는 대부분의 사건에서 처음 10 개 항목을 반환하는 결과에서 중복 오브젝트를 제외 할 String.prototype.match(), Array.prototype.some()을 사용할 수 있습니다 같은 단어.여기

+0

샘플도 매우 잘 작동했기 때문에 페이지의 모든 사용자가 실제로 내 질문에 대한 모든 유효한 대답 이었기 때문에 질문에 대한 최종 답을 고르려고 애썼다. 도움을 주셔서 감사합니다. 그리고 나는 같은 문제에 대한 모든 다른 해결책에 놀라움을 보였습니다.이 경우, 당신의 경기 사용은 놀라움을 주었고,이 문제를 풀기 위해 사용했던 몇 줄의 코드로 나를 겸손하게 만들었습니다. – D3181

1

var text = document.querySelector("div").textContent; 
 

 
var res = text.match(/[a-z]+/ig).reduce((arr, word) => { 
 
    return !arr.some(w => w.word === word) 
 
      ? [...arr, { 
 
       word: word, 
 
       len: text.match(new RegExp("\\b(" + word + ")\\b", "g")).length 
 
      }] 
 
      : arr 
 
}, []) 
 
.sort((a, b) => { 
 
    return b.len - a.len 
 
}); 
 

 
console.log(res.slice(0, 10));
<div> 
 
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam et ipsum eget purus maximus suscipit. Aliquam fringilla eros at lorem venenatis, et hendrerit neque ultrices. Suspendisse blandit, nulla eu hendrerit mattis, elit nibh blandit nibh, non scelerisque leo tellus placerat est. Phasellus dignissim velit metus. Sed quis urna et nunc hendrerit tempus quis eu neque. Vestibulum placerat massa eget sapien viverra fermentum. Aenean ac feugiat nibh, eu dignissim ligula. In hac habitasse platea dictumst. Nunc ipsum dolor, consectetur at justo eget, venenatis vulputate dui. Nulla facilisi. Suspendisse consequat pellentesque tincidunt. Nam aliquam mi a risus suscipit rutrum. 
 

 
Donec porta enim at lectus scelerisque, non tristique ex interdum. Nam vehicula consequat feugiat. In dictum metus a porttitor efficitur. Praesent varius elit porta consectetur ornare. Mauris euismod ullamcorper arcu. Vivamus ante enim, mollis eget auctor quis, tristique blandit velit. Aliquam ut erat eu erat vehicula sodales. Vestibulum et lectus at neque sodales congue ut id nibh. Etiam congue ornare felis eget dictum. Donec quis nisl non arcu tincidunt iaculis. 
 

 
Donec rutrum quam sit amet interdum mattis. Morbi eget fermentum dui. Morbi pulvinar nunc sed viverra sollicitudin. Praesent facilisis, quam ut malesuada lobortis, elit urna luctus nulla, sed condimentum dolor arcu id metus. Donec sit amet tempus massa. Nulla facilisi. Suspendisse egestas sollicitudin tempus. Fusce rutrum vel diam quis accumsan. 
 

 
Etiam purus arcu, suscipit et fermentum vel, commodo a leo. Vestibulum varius purus felis, fringilla blandit lacus luctus varius. In tempus imperdiet risus ut imperdiet. Ut ut faucibus nunc. Vivamus augue orci, lobortis at enim non, faucibus pharetra est. Pellentesque ante arcu, rhoncus eu lectus nec, ornare molestie lorem. Suspendisse at cursus erat. Vivamus quis lacinia neque. Donec euismod neque eget purus faucibus hendrerit. 
 

 
Fusce in ante placerat, aliquam mauris et, condimentum ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris hendrerit egestas risus, at consequat metus interdum et. Proin ut tellus quis lorem auctor tempor. Mauris viverra ligula et finibus iaculis. Mauris quis enim a lorem bibendum cursus nec nec enim. Etiam porttitor ligula et erat sagittis vulputate. Fusce ornare mi quis ante faucibus mattis. Aliquam tristique libero sed magna dapibus, vitae sollicitudin lorem malesuada. Praesent dignissim malesuada tellus vitae facilisis. Nullam diam augue, tincidunt ut maximus non, convallis vel felis. 
 
</div>

다른 알고리즘을 간다 :

function myCounter(bagWords) { 

    // Create an array of bag words from string 
    var bagMap = bagWords.toLowerCase().split(' '); 

    // Count duplicates 
    var bagCount = bagMap.reduce((countWords, word) => { 
     countWords[word] = ++countWords[word] || 1; 
     return countWords; 
    }, {}); 

    // Create a sorted array 
    var bagResult = []; 
    return bagResult = Object.keys(bagCount).map(function(el, i) { 
     bagResult.push(bagCount[el]); 
     return { 
      word: el, 
      count: bagCount[el] 
     }; 
    }).sort((a,b) => {  
     return b.count-a.count; 
    }).slice(0,10); 

} 

var bagWords = "Pat cat hat Tat bat rat pat Cat Hat tat Bat Rat test pat pat bat bat tap tac eat bat Ope ope Asd Eat dsa"; 
console.log(myCounter(bagWords)); 

은 아마 할 수있는 사람을 도와줍니다.

관련 문제