Monday 15 March 2010

javascript - How should I represent data for effective searching and comparing strings -


I have two arrays with length 300. They look like this (JSON representation):

  [["word1", 0.000199], ["word 2", 0.00012], ... ["word 15", 0.00012] ], ... [["Untitled 1", 0.0032199], ["other word 2", 0.032302], ... ["anotherword15", 0.0320102]]]   

and my It has a brute force algorithm:

 for  (var i = 0; i <300+ i ++) {for (var j = 0; j & lt; 15; j + +) {For var ii = i + 1; ii <300; ii ++) {for (var jj = 0; jj & lt; 15; jj ++) {for (var jjj = 0; jjj & Lt; 15; jjj ++) {if (new_keywords [i] [j] [0] === new_keyword [ii] [jj] [0] and amp; new_keywords [ii] [jj] [ 0] === state_keyword [i] [jejezi] [0]) {console.log (0); }}}}}}   

I need to find the same word in those arrays and if the words are the same, then I use the values ​​and divide the sum by 3 And change that value to the state_keywords array. So much for every word, the value of my values ​​will be once in the array.

Now ... my attitude is very bad because I now have about 300 million iterations and it's crazy. I need some better implementation of my array in Javascript

Edit:

With the example it is.

/ P>

EDIT2:

I'm sorry if I'm not enough. So what am I doing:

  • I have the array state_keywords . The index is 0 to 299 and they can be one themes ...
  • Each subject can be represented by 15 words and every time the new_keywords Array comes, they can be different.
  • When new_keywords array arrives, I need to check every word in that array if it is in the state_keywords array on the same topic index.
  • If it is: Add the probabilities up and divide by 2.
  • If it is not: add a new word in the state_keyword array but if they are more than 15 words for a topic (which are now), then I need to store just sorted from the first 15 possibilities.

    And I need to effectively do this because I need to do it every second, so it should be fast.

    EDIT3:

    Now I use this code:

      var i, j, jj, l ; For {i = 0; i <300; i ++} {for (j = 0; j & lt; 15; j ++) {l = new_keywords [i]. Length; (Jj = 0; jj & lt; l; jj ++) {if (state_keyword [i] [j] [0] === new_keyword [i] [jj] [0]) {state_keywords [i] [j ] [1] = (state_keyword [i] [j] [1] + new_keyword [i] [jj] [1]) / 2; }}}}   

    Which is faster than ever.

    Why do not you consider those arrays in keys with objects in the objects? Then you can see the words directly and get the value?

      var wordlists = [{word1 ": 0.000199," word2 ": 0.000102, ..." word15 ": 0.00012}, ... {" anotherword1 ": 0.0032199," anotherword2 " : 0.032302, ... "anotherword15": 0.0320102}]   

    and then

      word list [0] ["word2"] //0.000102    

No comments:

Post a Comment