Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. 1.
  2. Zawartość pliku whatnext.js
  3.  
  4. const redis = require('redis');
  5.  
  6. const client = redis.createClient();
  7.  
  8. client.on("error", (err) => { console.log("Error " + err); });
  9.  
  10. var BreakException = {};
  11.  
  12.  
  13.  
  14. function nextWord(lastWord, currentText) {
  15.  
  16. client.zrevrange(`word:${lastWord}:nextwords`, 0, -1, function(err, replies) {
  17.  
  18. console.log(`Word ${lastWord}:`)
  19.  
  20. var i = 0;
  21.  
  22. replies.forEach((reply) => {
  23.  
  24. if (i < 5 && reply.startsWith(currentText)) {
  25.  
  26. console.log(reply);
  27.  
  28. ++i;
  29.  
  30. }
  31.  
  32. })
  33.  
  34. });
  35.  
  36. }
  37.  
  38.  
  39.  
  40. nextWord("greater", "t");
  41.  
  42. nextWord("kill", "h");
  43.  
  44. nextWord("watson", "s");
  45.  
  46.  
  47.  
  48. client.quit();
  49.  
  50.  
  51.  
  52. Wyniki dla parametrów:
  53.  
  54. greater, t
  55.  
  56. than
  57.  
  58. trust
  59.  
  60.  
  61.  
  62. kill, h
  63.  
  64. him
  65.  
  66. her
  67.  
  68. his
  69.  
  70. watson, s
  71.  
  72. said
  73.  
  74. should
  75.  
  76. surely
  77.  
  78. so
  79.  
  80. shrugged
  81. 2
  82. const fs = require('fs'), redis = require('redis');
  83.  
  84.  
  85.  
  86. function readFile(filename, key) {
  87.  
  88. const client = redis.createClient();
  89.  
  90. client.on("error", (err) => { console.log("Error " + err); });
  91.  
  92. fs.readFile(filename, 'utf8', function(err, data) {
  93.  
  94. if (err) {
  95.  
  96. return console.log(err);
  97.  
  98. }
  99.  
  100. var splits = data.split(/\W/).filter(
  101.  
  102. (value) => value.length > 1
  103.  
  104. ).map(
  105.  
  106. (value) => value.toLocaleLowerCase()
  107.  
  108. );
  109.  
  110. splits.forEach((value, index) => {
  111.  
  112. client.sadd(key, value);
  113.  
  114. });
  115.  
  116. client.quit();
  117.  
  118. });
  119.  
  120. }
  121.  
  122.  
  123.  
  124. readFile('cano.txt', 'arthur');
  125.  
  126. readFile('t8.shakespeare.txt', 'william');
  127. 3
  128. a. William Shakespeare Poprawnie
  129. 4
  130. 9792
  131. 5
  132. const fs = require('fs'), redis = require('redis');
  133.  
  134.  
  135.  
  136. function readFile(filename, key) {
  137.  
  138. const client = redis.createClient();
  139.  
  140. client.on("error", (err) => { console.log("Error " + err); });
  141.  
  142. fs.readFile(filename, 'utf8', function(err, data) {
  143.  
  144. if (err) {
  145.  
  146. return console.log(err);
  147.  
  148. }
  149.  
  150. var splits = data.split(/\W/).filter(
  151.  
  152. (value) => value.length > 1
  153.  
  154. ).map(
  155.  
  156. (value) => value.toLocaleLowerCase()
  157.  
  158. );
  159.  
  160. splits.forEach((value, index) => {
  161.  
  162. client.zadd(key, 0, value);
  163.  
  164. });
  165.  
  166. client.quit();
  167.  
  168. });
  169.  
  170. }
  171.  
  172.  
  173.  
  174. readFile('cano.txt', 'sorted');
  175.  
  176. readFile('t8.shakespeare.txt', 'sorted');
  177. 6
  178. 16408
  179. 7
  180. alack
  181. ale porpawna odpowiedź to ponoć
  182. `antiumz
  183. antium
  184. 8
  185. 23870
  186. 9
  187. const fs = require('fs'), redis = require('redis');
  188.  
  189.  
  190.  
  191. function readSortedList(listKey, sortedListKey, mapKey) {
  192.  
  193. const client = redis.createClient();
  194.  
  195. client.on("error", (err) => { console.log("Error " + err); });
  196.  
  197. client.zrange(sortedListKey, 0, -1, function(err, sortedReplies) {
  198.  
  199. if (err)
  200.  
  201. throw(err);
  202.  
  203. sortedReplies.forEach((value, index) => {
  204.  
  205. client.sismember(listKey, value, function(err1, isMember) {
  206.  
  207. if (err1)
  208.  
  209. throw(err1);
  210.  
  211. if (isMember)
  212.  
  213. client.setbit(mapKey, index, 1);
  214.  
  215. })
  216.  
  217. });
  218.  
  219. })
  220.  
  221. client.bitpos(mapKey, 0, function(err, ret) {
  222.  
  223. console.log(ret);
  224.  
  225. });
  226.  
  227. // client.quit();
  228.  
  229. }
  230.  
  231.  
  232.  
  233. readSortedList('arthur', 'sorted', 'arthur:bitwords');
  234. 10
  235. fałsz
  236. 11
  237. 0238
  238. 12
  239. const redis = require('redis');
  240.  
  241.  
  242.  
  243. function createHyperLogLog(arthur, william, logKey) {
  244.  
  245. const client = redis.createClient();
  246.  
  247. client.on("error", (err) => { console.log("Error " + err); });
  248.  
  249. client.sinter(arthur, william, function(err, values) {
  250.  
  251. if (err)
  252.  
  253. throw(err);
  254.  
  255. values.forEach((value, index) => {
  256.  
  257. client.pfadd(logKey, value);
  258.  
  259. })
  260.  
  261. });
  262.  
  263. }
  264.  
  265.  
  266.  
  267. createHyperLogLog('arthur', 'william', 'arthur:hyperwords');
  268. 13
  269. 53
  270. 14
  271. 48,67
  272. Ale dobra odpowiedź to ponoć
  273. 5
  274. 15
  275. const dbKeys = await client.keysAsync("db:*");
  276.  
  277. (. . .)
  278.  
  279. type = await client.hgetAsync("db:"+name, "type");
  280.  
  281. release = await client.hgetAsync("db:"+name, "current_release");
  282.  
  283. score = await client.hgetAsync("db:"+name, "score");
  284. A szesnastego nie ma
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement