MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / Index

Class Index

vision/textDetection.js:34–129  ·  view source on GitHub ↗

* State manager for text processing. Stores and reads results from Redis.

Source from the content-addressed store, hash-verified

32 * State manager for text processing. Stores and reads results from Redis.
33 */
34class Index {
35 /**
36 * Create a new Index object.
37 */
38 constructor() {
39 // Connect to a redis server.
40 const TOKEN_DB = 0;
41 const DOCS_DB = 1;
42 const PORT = process.env.REDIS_PORT || '6379';
43 const HOST = process.env.REDIS_HOST || '127.0.0.1';
44
45 this.tokenClient = redis
46 .createClient({url: `redis://${HOST}:${PORT}`, db: TOKEN_DB})
47 .on('error', err => {
48 console.error('ERR:REDIS: ' + err);
49 throw err;
50 });
51 this.docsClient = redis
52 .createClient({url: `redis://${HOST}:${PORT}`, db: DOCS_DB})
53 .on('error', err => {
54 console.error('ERR:REDIS: ' + err);
55 throw err;
56 });
57
58 (async () => {
59 await this.tokenClient.connect();
60 await this.docsClient.connect();
61 })();
62 }
63
64 /**
65 * Close all active redis server connections.
66 */
67 quit() {
68 this.tokenClient.quit();
69 this.docsClient.quit();
70 }
71
72 /**
73 * Tokenize the given document.
74 * @param {string} filename - key for the storage in redis
75 * @param {string} document - Collection of words to be tokenized
76 * @returns {Promise<void>}
77 */
78 async add(filename, document) {
79 const PUNCTUATION = ['.', ',', ':', ''];
80 const tokenizer = new natural.WordTokenizer();
81 const tokens = tokenizer.tokenize(document);
82 // filter out punctuation, then add all tokens to a redis set.
83 await Promise.all(
84 tokens
85 .filter(token => PUNCTUATION.indexOf(token) === -1)
86 .map(token => this.tokenClient.sAdd(token, filename))
87 );
88 await this.docsClient.set(filename, document);
89 }
90
91 /**

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected