15
15
16
16
'use strict' ;
17
17
18
- let async = require ( 'async' ) ;
19
- let fs = require ( 'fs' ) ;
20
- let path = require ( 'path' ) ;
18
+ const async = require ( 'async' ) ;
19
+ const fs = require ( 'fs' ) ;
20
+ const path = require ( 'path' ) ;
21
21
22
22
// By default, the client will authenticate using the service account file
23
23
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
24
24
// the project specified by the GCLOUD_PROJECT environment variable. See
25
25
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
26
- let vision = require ( '@google-cloud/vision' ) ;
27
- let natural = require ( 'natural' ) ;
28
- let redis = require ( 'redis' ) ;
26
+ const vision = require ( '@google-cloud/vision' ) ;
27
+ const natural = require ( 'natural' ) ;
28
+ const redis = require ( 'redis' ) ;
29
29
30
30
// Instantiate a vision client
31
- let client = new vision . ImageAnnotatorClient ( ) ;
31
+ const client = new vision . ImageAnnotatorClient ( ) ;
32
32
33
33
function Index ( ) {
34
34
// Connect to a redis server.
35
- let TOKEN_DB = 0 ;
36
- let DOCS_DB = 1 ;
37
- let PORT = process . env . REDIS_PORT || '6379' ;
38
- let HOST = process . env . REDIS_HOST || '127.0.0.1' ;
35
+ const TOKEN_DB = 0 ;
36
+ const DOCS_DB = 1 ;
37
+ const PORT = process . env . REDIS_PORT || '6379' ;
38
+ const HOST = process . env . REDIS_HOST || '127.0.0.1' ;
39
39
40
40
this . tokenClient = redis
41
41
. createClient ( PORT , HOST , {
@@ -59,12 +59,12 @@ Index.prototype.quit = function() {
59
59
} ;
60
60
61
61
Index . prototype . add = function ( filename , document , callback ) {
62
- let self = this ;
63
- let PUNCTUATION = [ '.' , ',' , ':' , '' ] ;
64
- let tokenizer = new natural . WordTokenizer ( ) ;
65
- let tokens = tokenizer . tokenize ( document ) ;
62
+ const self = this ;
63
+ const PUNCTUATION = [ '.' , ',' , ':' , '' ] ;
64
+ const tokenizer = new natural . WordTokenizer ( ) ;
65
+ const tokens = tokenizer . tokenize ( document ) ;
66
66
67
- let tasks = tokens
67
+ const tasks = tokens
68
68
. filter ( function ( token ) {
69
69
return PUNCTUATION . indexOf ( token ) === - 1 ;
70
70
} )
@@ -82,8 +82,8 @@ Index.prototype.add = function(filename, document, callback) {
82
82
} ;
83
83
84
84
Index . prototype . lookup = function ( words , callback ) {
85
- let self = this ;
86
- let tasks = words . map ( function ( word ) {
85
+ const self = this ;
86
+ const tasks = words . map ( function ( word ) {
87
87
word = word . toLowerCase ( ) ;
88
88
return function ( cb ) {
89
89
self . tokenClient . smembers ( word , cb ) ;
@@ -114,7 +114,7 @@ Index.prototype.setContainsNoText = function(filename, callback) {
114
114
} ;
115
115
116
116
function lookup ( words , callback ) {
117
- let index = new Index ( ) ;
117
+ const index = new Index ( ) ;
118
118
index . lookup ( words , function ( err , hits ) {
119
119
index . quit ( ) ;
120
120
if ( err ) {
@@ -146,9 +146,9 @@ function extractDescriptions(filename, index, response, callback) {
146
146
147
147
function getTextFromFiles ( index , inputFiles , callback ) {
148
148
// Make a call to the Vision API to detect text
149
- let requests = [ ] ;
149
+ const requests = [ ] ;
150
150
inputFiles . forEach ( filename => {
151
- let request = {
151
+ const request = {
152
152
image : { content : fs . readFileSync ( filename ) . toString ( 'base64' ) } ,
153
153
features : [ { type : 'TEXT_DETECTION' } ] ,
154
154
} ;
@@ -157,11 +157,11 @@ function getTextFromFiles(index, inputFiles, callback) {
157
157
client
158
158
. batchAnnotateImages ( { requests : requests } )
159
159
. then ( results => {
160
- let detections = results [ 0 ] . responses ;
161
- let textResponse = { } ;
162
- let tasks = [ ] ;
160
+ const detections = results [ 0 ] . responses ;
161
+ const textResponse = { } ;
162
+ const tasks = [ ] ;
163
163
inputFiles . forEach ( function ( filename , i ) {
164
- let response = detections [ i ] ;
164
+ const response = detections [ i ] ;
165
165
if ( response . error ) {
166
166
console . log ( 'API Error for ' + filename , response . error ) ;
167
167
return ;
@@ -186,7 +186,7 @@ function getTextFromFiles(index, inputFiles, callback) {
186
186
187
187
// Run the example
188
188
function main ( inputDir , callback ) {
189
- let index = new Index ( ) ;
189
+ const index = new Index ( ) ;
190
190
191
191
async . waterfall (
192
192
[
@@ -198,7 +198,7 @@ function main(inputDir, callback) {
198
198
function ( files , cb ) {
199
199
async . parallel (
200
200
files . map ( function ( file ) {
201
- let filename = path . join ( inputDir , file ) ;
201
+ const filename = path . join ( inputDir , file ) ;
202
202
return function ( cb ) {
203
203
fs . stat ( filename , function ( err , stats ) {
204
204
if ( err ) {
@@ -216,7 +216,7 @@ function main(inputDir, callback) {
216
216
} ,
217
217
// Figure out which files have already been processed
218
218
function ( allImageFiles , cb ) {
219
- let tasks = allImageFiles
219
+ const tasks = allImageFiles
220
220
. filter ( function ( filename ) {
221
221
return filename ;
222
222
} )
@@ -256,16 +256,16 @@ function main(inputDir, callback) {
256
256
}
257
257
258
258
if ( module === require . main ) {
259
- let generalError =
259
+ const generalError =
260
260
'Usage: node textDetection <command> <arg> ...\n\n' +
261
261
'\tCommands: analyze, lookup' ;
262
262
if ( process . argv . length < 3 ) {
263
263
console . log ( generalError ) ;
264
264
// eslint-disable-next-line no-process-exit
265
265
process . exit ( 1 ) ;
266
266
}
267
- let args = process . argv . slice ( 2 ) ;
268
- let command = args . shift ( ) ;
267
+ const args = process . argv . slice ( 2 ) ;
268
+ const command = args . shift ( ) ;
269
269
if ( command === 'analyze' ) {
270
270
if ( ! args . length ) {
271
271
console . log ( 'Usage: node textDetection analyze <dir>' ) ;
0 commit comments