dataquery.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. define( function ( require, exports, module ) {
  2. var data = ( function ( origin ) {
  3. var ret = [],
  4. length = origin.length,
  5. i;
  6. var id = 1000;
  7. for ( i = 0; i < length; i++ ) {
  8. if ( +( origin[ i ][ 1 ] ) >= 0 ) {
  9. ret.push( {
  10. id: id++,
  11. seq: origin[ i ][ 0 ].split( '->' ),
  12. length: +origin[ i ][ 1 ],
  13. uv: +origin[ i ][ 2 ]
  14. } );
  15. }
  16. }
  17. return ret;
  18. } )( require( '../pathdiagram/data' ) );
  19. var length = data.length;
  20. var abs = Math.abs;
  21. function subseqIndex( seq, sub ) {
  22. var i = 0,
  23. j = 0,
  24. iLength = seq.length,
  25. jLength = sub.length,
  26. iPreserve = 0;
  27. while ( true ) {
  28. if ( seq[ i ] != sub[ j ] ) {
  29. i = ++iPreserve;
  30. j = 0;
  31. if ( i >= iLength ) {
  32. return -1;
  33. }
  34. continue;
  35. }
  36. if ( seq[ i ] == sub[ j ] ) {
  37. i++;
  38. j++;
  39. }
  40. if ( j == jLength ) {
  41. return iPreserve;
  42. }
  43. }
  44. }
  45. return {
  46. random: function ( minUv ) {
  47. var keywords = [];
  48. for ( var i = 0; i < length; i++ ) {
  49. if ( data[ i ].length === 0 ) {
  50. keywords.push( data[ i ] );
  51. }
  52. }
  53. minUv = minUv || 5000;
  54. var ret;
  55. while ( !ret || ret.uv < minUv ) {
  56. ret = keywords[ Math.floor( Math.random() * keywords.length ) ];
  57. }
  58. ret.word = ret.seq[0];
  59. return ret;
  60. },
  61. search: function( keyword ) {
  62. var result = [], ret;
  63. for ( var i = 0; i < length; i++ ) {
  64. if ( data[ i ].length === 0 && data[i].seq[0].indexOf(keyword) != -1 ) {
  65. result.push( data[i] );
  66. }
  67. }
  68. if(!result.length) {
  69. return this.random();
  70. }
  71. result.sort(function(a, b) {
  72. return b.uv - a.uv;
  73. });
  74. ret = result[0];
  75. ret.word = ret.seq[0];
  76. return ret;
  77. },
  78. upstream: function ( path ) {
  79. var upstream = [];
  80. for ( var i = 0; i < length; i++ ) {
  81. if ( subseqIndex( data[ i ].seq, path.seq ) > 0 &&
  82. data[ i ].length == path.length + 1 ) {
  83. upstream.push( {
  84. id: data[ i ].id,
  85. seq: data[ i ].seq,
  86. length: data[ i ].length,
  87. uv: data[ i ].uv,
  88. word: data[ i ].seq[ 0 ]
  89. } );
  90. }
  91. }
  92. return upstream;
  93. },
  94. downstream: function ( path ) {
  95. var downstream = [];
  96. for ( var i = 0; i < length; i++ ) {
  97. if ( subseqIndex( data[ i ].seq, path.seq ) === 0 &&
  98. data[ i ].length == path.length + 1 ) {
  99. downstream.push( {
  100. id: data[ i ].id,
  101. seq: data[ i ].seq,
  102. length: data[ i ].length,
  103. uv: data[ i ].uv,
  104. word: data[ i ].seq[ data[ i ].length - 1 ]
  105. } );
  106. }
  107. }
  108. return downstream;
  109. }
  110. };
  111. } );