From c2119af512a48685833292d25324e52f9b35d497 Mon Sep 17 00:00:00 2001 From: Dave Gramlich Date: Mon, 13 Mar 2017 14:36:35 -0400 Subject: [PATCH] language: remove verbose option (#2056) --- packages/google-cloud-language/package.json | 2 - .../google-cloud-language/src/document.js | 540 +++--------------- packages/google-cloud-language/src/index.js | 44 -- .../system-test/language.js | 260 +-------- .../google-cloud-language/test/document.js | 474 +-------------- 5 files changed, 119 insertions(+), 1201 deletions(-) diff --git a/packages/google-cloud-language/package.json b/packages/google-cloud-language/package.json index fa6a3e2e68f..b24e928225e 100644 --- a/packages/google-cloud-language/package.json +++ b/packages/google-cloud-language/package.json @@ -53,12 +53,10 @@ ], "dependencies": { "@google-cloud/common": "^0.12.0", - "arrify": "^1.0.1", "extend": "^3.0.0", "google-gax": "^0.12.0", "google-proto-files": "^0.10.0", "is": "^3.0.1", - "propprop": "^0.3.1", "string-format-obj": "^1.1.0" }, "devDependencies": { diff --git a/packages/google-cloud-language/src/document.js b/packages/google-cloud-language/src/document.js index 93caf8a4ef7..b727a81b087 100644 --- a/packages/google-cloud-language/src/document.js +++ b/packages/google-cloud-language/src/document.js @@ -20,12 +20,10 @@ 'use strict'; -var arrify = require('arrify'); var common = require('@google-cloud/common'); var extend = require('extend'); var format = require('string-format-obj'); var is = require('is'); -var prop = require('propprop'); /*! Developer Documentation * @@ -247,73 +245,19 @@ Document.PART_OF_SPEECH = { * default, all features (`entities`, `sentiment`, and `syntax`) are * enabled. By overriding any of these values, all defaults are switched to * `false`. (Alias for `options.extractDocumentSyntax`) - * @param {boolean} options.verbose - Enable verbose mode for more detailed - * results. Default: `false` * @param {function} callback - The callback function. * @param {?error} callback.err - An error occurred while making this request. * @param {object} callback.annotation - The formatted API response. * @param {string} callback.annotation.language - The language detected from the * text. - * @param {number} callback.annotation.sentiment - A value in the range of - * `-1` (negative) to `1` (positive). + * @param {number} callback.annotation.sentiment - An object representing the + * overall sentiment of the text. * @param {object} callback.annotation.entities - The recognized entities from * the text, grouped by the type of entity. - * @param {string[]} callback.annotation.entities.art - Art entities detected - * from the text. This is only present if detections of this type were - * found. - * @param {string[]} callback.annotation.entities.events - Event entities - * detected from the text. This is only present if detections of this type - * were found. - * @param {string[]} callback.annotation.entities.goods - Consumer good entities - * detected from the text. This is only present if detections of this type - * were found. - * @param {string[]} callback.annotation.entities.organizations - Organization - * entities detected from the text. This is only present if detections of - * this type were found. - * @param {string[]} callback.annotation.entities.other - Other entities - * detected from the text. This is only present if detections of this type - * were found. - * @param {string[]} callback.annotation.entities.people - People entities - * detected from the text. This is only present if detections of this type - * were found. - * @param {string[]} callback.annotation.entities.places - Place entities - * detected from the text. This is only present if detections of this type - * were found. - * @param {string[]} callback.annotation.entities.unknown - Unknown entities - * detected from the text. This is only present if detections of this type - * were found. * @param {string[]} callback.annotation.sentences - Sentences detected from the * text. * @param {object[]} callback.annotation.tokens - Parts of speech that were * detected from the text. - * @param {string} callback.annotation.tokens[].text - The piece of text - * analyzed. - * @param {string} callback.annotation.tokens[].partOfSpeech - A description of - * the part of speech (`Noun (common and propert)`, - * `Verb (all tenses and modes)`, etc.). - * @param {string} callback.annotation.tokens[].tag - A short code - * for the type of speech (`NOUN`, `VERB`, etc.). - * @param {string} callback.annotations.tokens[].aspect - The characteristic of - * a verb that expresses time flow during an event. - * @param {string} callback.annotations.tokens[].case - The grammatical function - * performed by a noun or pronoun in a phrase, clause, or sentence. - * @param {string} callback.annotations.tokens[].form - Form categorizes - * different forms of verbs, adjectives, adverbs, etc. - * @param {string} callback.annotations.tokens[].gender - Gender classes of - * nouns reflected in the behaviour of associated words - * @param {string} callback.annotations.tokens[].mood - The grammatical feature - * of verbs, used for showing modality and attitude. - * @param {string} callback.annotations.tokens[].number - Count distinctions. - * @param {string} callback.annotations.tokens[].person - The distinction - * between the speaker, second person, third person, etc. - * @param {string} callback.annotations.tokens[].proper - This category shows if - * the token is part of a proper name. - * @param {string} callback.annotations.tokens[].reciprocity - Reciprocal - * features of a pronoun - * @param {string} callback.annotations.tokens[].tense - Time reference. - * @param {string} callback.annotations.tokens[].voice - The relationship - * between the action that a verb expresses and the participants identified - * by its arguments. * @param {object} callback.apiResponse - The full API response. * * @example @@ -324,98 +268,6 @@ Document.PART_OF_SPEECH = { * * // annotation = { * // language: 'en', - * // sentiment: 1, - * // entities: { - * // organizations: [ - * // 'Google' - * // ], - * // places: [ - * // 'American' - * // ] - * // }, - * // sentences: [ - * // 'Google is an American multinational technology company ' + - * // 'specializing in Internet-related services and products.' - * // ], - * // tokens: [ - * // { - * // text: 'Google', - * // partOfSpeech: 'Noun (common and proper)', - * // tag: 'NOUN', - * // aspect: 'PERFECTIVE', - * // case: 'ADVERBIAL', - * // form: 'ADNOMIAL', - * // gender: 'FEMININE', - * // mood: 'IMPERATIVE', - * // number: 'SINGULAR', - * // person: 'FIRST', - * // proper: 'PROPER', - * // reciprocity: 'RECIPROCAL', - * // tense: 'PAST', - * // voice: 'PASSIVE' - * // }, - * // { - * // text: 'is', - * // partOfSpeech: 'Verb (all tenses and modes)', - * // tag: 'VERB', - * // aspect: 'PERFECTIVE', - * // case: 'ADVERBIAL', - * // form: 'ADNOMIAL', - * // gender: 'FEMININE', - * // mood: 'IMPERATIVE', - * // number: 'SINGULAR', - * // person: 'FIRST', - * // proper: 'PROPER', - * // reciprocity: 'RECIPROCAL', - * // tense: 'PAST', - * // voice: 'PASSIVE' - * // }, - * // ... - * // ] - * // } - * }); - * - * //- - * // To request only certain annotation types, provide an options object. - * //- - * var options = { - * entities: true, - * sentiment: true - * }; - * - * document.annotate(options, function(err, annotation) { - * if (err) { - * // Error handling omitted. - * } - * - * // annotation = { - * // language: 'en', - * // sentiment: 1, - * // entities: { - * // organizations: [ - * // 'Google' - * // ], - * // places: [ - * // 'American' - * // ] - * // } - * // } - * }); - * - * //- - * // Verbose mode may also be enabled for more detailed results. - * //- - * var options = { - * verbose: true - * }; - * - * document.annotate(options, function(err, annotation) { - * if (err) { - * // Error handling omitted. - * } - * - * // annotation = { - * // language: 'en', * // sentiment: { * // score: 1, * // magnitude: 4 @@ -505,6 +357,70 @@ Document.PART_OF_SPEECH = { * }); * * //- + * // To request only certain annotation types, provide an options object. + * //- + * var options = { + * entities: true, + * sentiment: true + * }; + * + * document.annotate(function(err, annotation) { + * if (err) { + * // Error handling omitted. + * } + * + * // annotation = { + * // language: 'en', + * // sentiment: { + * // score: 1, + * // magnitude: 4 + * // }, + * // entities: { + * // organizations: [ + * // { + * // name: 'Google', + * // type: 'ORGANIZATION', + * // metadata: { + * // wikipedia_url: 'http://en.wikipedia.org/wiki/Google' + * // }, + * // salience: 0.65137446, + * // mentions: [ + * // { + * // text: { + * // content: 'Google', + * // beginOffset: -1 + * // }, + * // type: 'PROPER' + * // } + * // ] + * // } + * // ], + * // places: [ + * // { + * // name: 'American', + * // type: 'LOCATION', + * // metadata: { + * // wikipedia_url: 'http://en.wikipedia.org/wiki/United_States' + * // }, + * // salience: 0.13947370648384094, + * // mentions: [ + * // { + * // text: [ + * // { + * // content: 'American', + * // beginOffset: -1 + * // }, + * // type: 'PROPER' + * // ] + * // } + * // ] + * // } + * // ] + * // }, + * // } + * }); + * + * //- * // If the callback is omitted, we'll return a Promise. * //- * document.annotate().then(function(data) { @@ -545,8 +461,6 @@ Document.prototype.annotate = function(options, callback) { features = featuresRequested; } - var verbose = options.verbose === true; - this.api.Language.annotateText({ document: this.document, features: features, @@ -564,19 +478,18 @@ Document.prototype.annotate = function(options, callback) { }; if (resp.documentSentiment && features.extractDocumentSentiment) { - var sentiment = resp.documentSentiment; - annotation.sentiment = Document.formatSentiment_(sentiment, verbose); + annotation.sentiment = resp.documentSentiment; } if (resp.entities) { - annotation.entities = Document.formatEntities_(resp.entities, verbose); + annotation.entities = resp.entities; } // This prevents empty `sentences` and `tokens` arrays being returned to // users who never wanted sentences or tokens to begin with. if (numFeaturesRequested === 0 || featuresRequested.extractSyntax) { - annotation.sentences = Document.formatSentences_(resp.sentences, verbose); - annotation.tokens = Document.formatTokens_(resp.tokens, verbose); + annotation.sentences = resp.sentences; + annotation.tokens = resp.tokens; } callback(null, annotation, originalResp); @@ -600,24 +513,6 @@ Document.prototype.annotate = function(options, callback) { * @param {?error} callback.err - An error occurred while making this request. * @param {object} callback.entities - The recognized entities from the text, * grouped by the type of entity. - * @param {string[]} callback.entities.art - Art entities detected from the - * text. This is only present if detections of this type were found. - * @param {string[]} callback.entities.events - Event entities detected from the - * text. This is only present if detections of this type were found. - * @param {string[]} callback.entities.goods - Consumer good entities detected - * from the text. This is only present if detections of this type were - * found. - * @param {string[]} callback.entities.organizations - Organization entities - * detected from the text. This is only present if detections of this type - * were found. - * @param {string[]} callback.entities.other - Other entities detected from the - * text. This is only present if detections of this type were found. - * @param {string[]} callback.entities.people - People entities detected from - * the text. This is only present if detections of this type were found. - * @param {string[]} callback.entities.places - Place entities detected from the - * text. This is only present if detections of this type were found. - * @param {string[]} callback.entities.unknown - Unknown entities detected from - * the text. This is only present if detections of this type were found. * @param {object} callback.apiResponse - The full API response. * * @example @@ -628,28 +523,6 @@ Document.prototype.annotate = function(options, callback) { * * // entities = { * // organizations: [ - * // 'Google' - * // ], - * // places: [ - * // 'American' - * // ] - * // } - * }); - * - * //- - * // Verbose mode may also be enabled for more detailed results. - * //- - * var options = { - * verbose: true - * }; - * - * document.detectEntities(options, function(err, entities) { - * if (err) { - * // Error handling omitted. - * } - * - * // entities = { - * // organizations: [ * // { * // name: 'Google', * // type: 'ORGANIZATION', @@ -702,8 +575,6 @@ Document.prototype.detectEntities = function(options, callback) { options = {}; } - var verbose = options.verbose === true; - this.api.Language.analyzeEntities({ document: this.document, encodingType: this.detectEncodingType_(options) @@ -713,10 +584,7 @@ Document.prototype.detectEntities = function(options, callback) { return; } - var originalResp = extend(true, {}, resp); - var groupedEntities = Document.formatEntities_(resp.entities, verbose); - - callback(null, groupedEntities, originalResp); + callback(null, resp.entities, resp); }); }; @@ -731,12 +599,10 @@ Document.prototype.detectEntities = function(options, callback) { * `string`), or `UTF32`. (Alias for `options.encodingType`). Default: * 'UTF8' if a Buffer, otherwise 'UTF16'. See * [`EncodingType`](https://cloud.google.com/natural-language/reference/rest/v1/EncodingType) - * @param {boolean} options.verbose - Enable verbose mode for more detailed - * results. Default: `false` * @param {function} callback - The callback function. * @param {?error} callback.err - An error occurred while making this request. - * @param {number} callback.sentiment - A value in the range of `-1` (negative) - * to `1` (positive). + * @param {number} callback.sentiment - An object representing the overall + * sentiment of the text. * @param {object} callback.apiResponse - The full API response. * * @example @@ -745,21 +611,6 @@ Document.prototype.detectEntities = function(options, callback) { * // Error handling omitted. * } * - * // sentiment = 1 - * }); - * - * //- - * // Verbose mode may also be enabled for more detailed results. - * //- - * var options = { - * verbose: true - * }; - * - * document.detectSentiment(options, function(err, sentiment) { - * if (err) { - * // Error handling omitted. - * } - * * // sentiment = { * // score: 1, * // magnitude: 4, @@ -791,8 +642,6 @@ Document.prototype.detectSentiment = function(options, callback) { options = {}; } - var verbose = options.verbose === true; - this.api.Language.analyzeSentiment({ document: this.document, encodingType: this.detectEncodingType_(options) @@ -802,17 +651,7 @@ Document.prototype.detectSentiment = function(options, callback) { return; } - var originalResp = extend(true, {}, resp); - var sentiment = Document.formatSentiment_(resp.documentSentiment, verbose); - - if (verbose) { - sentiment = extend(sentiment, { - sentences: Document.formatSentences_(resp.sentences, verbose), - language: resp.language - }); - } - - callback(null, sentiment, originalResp); + callback(null, resp.documentSentiment, resp); }); }; @@ -827,17 +666,9 @@ Document.prototype.detectSentiment = function(options, callback) { * `string`), or `UTF32`. (Alias for `options.encodingType`). Default: * 'UTF8' if a Buffer, otherwise 'UTF16'. See * [`EncodingType`](https://cloud.google.com/natural-language/reference/rest/v1/EncodingType) - * @param {boolean} options.verbose - Enable verbose mode for more detailed - * results. Default: `false` * @param {function} callback - The callback function. * @param {?error} callback.err - An error occurred while making this request. * @param {object} callback.syntax - The syntax recognized from the text. - * @param {string} callback.syntax.language - The language detected from the - * text. - * @param {string[]} callback.syntax.sentences - Sentences detected from the - * text. - * @param {object[]} callback.syntax.tokens - Parts of speech that were - * detected from the text. * @param {object} callback.apiResponse - The full API response. * * @example @@ -848,62 +679,6 @@ Document.prototype.detectSentiment = function(options, callback) { * * // syntax = { * // sentences: [ - * // 'Google is an American multinational technology company ' + - * // 'specializing in Internet-related services and products.' - * // ], - * // tokens: [ - * // { - * // text: 'Google', - * // partOfSpeech: 'Noun (common and proper)', - * // tag: 'NOUN', - * // aspect: 'PERFECTIVE', - * // case: 'ADVERBIAL', - * // form: 'ADNOMIAL', - * // gender: 'FEMININE', - * // mood: 'IMPERATIVE', - * // number: 'SINGULAR', - * // person: 'FIRST', - * // proper: 'PROPER', - * // reciprocity: 'RECIPROCAL', - * // tense: 'PAST', - * // voice: 'PASSIVE' - * // }, - * // { - * // text: 'is', - * // partOfSpeech: 'Verb (all tenses and modes)', - * // tag: 'VERB', - * // aspect: 'PERFECTIVE', - * // case: 'ADVERBIAL', - * // form: 'ADNOMIAL', - * // gender: 'FEMININE', - * // mood: 'IMPERATIVE', - * // number: 'SINGULAR', - * // person: 'FIRST', - * // proper: 'PROPER', - * // reciprocity: 'RECIPROCAL', - * // tense: 'PAST', - * // voice: 'PASSIVE' - * // }, - * // ... - * // ], - * // language: 'en' - * // } - * }); - * - * //- - * // Verbose mode may also be enabled for more detailed results. - * //- - * var options = { - * verbose: true - * }; - * - * document.detectSyntax(options, function(err, syntax) { - * if (err) { - * // Error handling omitted. - * } - * - * // syntax = { - * // sentences: [ * // { * // text: { * // content: @@ -963,8 +738,6 @@ Document.prototype.detectSyntax = function(options, callback) { options = {}; } - var verbose = options.verbose === true; - this.api.Language.analyzeSyntax({ document: this.document, encodingType: this.detectEncodingType_(options) @@ -974,173 +747,10 @@ Document.prototype.detectSyntax = function(options, callback) { return; } - var originalResp = extend(true, {}, resp); - var syntax = Document.formatTokens_(resp.tokens, verbose); - - if (verbose) { - syntax = { - tokens: syntax, - sentences: Document.formatSentences_(resp.sentences, verbose), - language: resp.language - }; - } - - callback(null, syntax, originalResp); + callback(null, resp.tokens, resp); }); }; -/** - * Take a raw response from the API and make it more user-friendly. - * - * @private - * - * @param {object} entities - A group of entities returned from the API. - * @param {boolean} verbose - Enable verbose mode for more detailed results. - * @return {object} - The formatted entity object. - */ -Document.formatEntities_ = function(entities, verbose) { - var GROUP_NAME_TO_TYPE = { - UNKNOWN: 'unknown', - PERSON: 'people', - LOCATION: 'places', - ORGANIZATION: 'organizations', - EVENT: 'events', - WORK_OF_ART: 'art', - CONSUMER_GOOD: 'goods', - OTHER: 'other' - }; - - var groupedEntities = entities.reduce(function(acc, entity) { - entity = extend(true, {}, entity); - - var groupName = GROUP_NAME_TO_TYPE[entity.type]; - - acc[groupName] = arrify(acc[groupName]); - acc[groupName].push(entity); - acc[groupName].sort(Document.sortByProperty_('salience')); - - return acc; - }, {}); - - if (!verbose) { - // Simplify the response to only include an array of `name`s. - for (var groupName in groupedEntities) { - if (groupedEntities.hasOwnProperty(groupName)) { - groupedEntities[groupName] = - groupedEntities[groupName].map(prop('name')); - } - } - } - - return groupedEntities; -}; - -/** - * Take a raw response from the API and make it more user-friendly. - * - * @private - * - * @param {object[]} sentences - A group of sentence detections returned from - * the API. - * @param {boolean} verbose - Enable verbose mode for more detailed results. - * @return {object[]|string[]} - The formatted sentences or sentence descriptor - * objects in verbose mode. - */ -Document.formatSentences_ = function(sentences, verbose) { - if (!verbose) { - sentences = sentences.map(prop('text')).map(prop('content')); - } - - return sentences; -}; - -/** - * Take a raw response from the API and make it more user-friendly. - * - * @private - * - * @param {object} sentiment - An analysis of the document's sentiment from the - * API. - * @param {boolean} verbose - Enable verbose mode for more detailed results. - * @return {number|object} - The sentiment represented as a number in the range - * of `-1` to `1` or an object containing `score` and `magnitude` - * measurements in verbose mode. - */ -Document.formatSentiment_ = function(sentiment, verbose) { - sentiment = { - score: sentiment.score, - magnitude: sentiment.magnitude - }; - - if (!verbose) { - sentiment = sentiment.score; - } - - return sentiment; -}; - -/** - * Take a raw response from the API and make it more user-friendly. - * - * @private - * - * @param {object[]} tokens - A group of syntax detections returned from the - * API. - * @param {boolean} verbose - Enable verbose mode for more detailed results. - * @return {number|object} - A slimmed down, simplified object or the original - * object in verbose mode. - */ -Document.formatTokens_ = function(tokens, verbose) { - if (!verbose) { - tokens = tokens.map(function(rawToken) { - var token = extend({}, rawToken.partOfSpeech, { - text: rawToken.text.content, - partOfSpeech: Document.PART_OF_SPEECH[rawToken.partOfSpeech.tag] - }); - - if (rawToken.dependencyEdge) { - var label = rawToken.dependencyEdge.label; - - token.dependencyEdge = extend({}, rawToken.dependencyEdge, { - description: Document.LABEL_DESCRIPTIONS[label] - }); - } - - for (var part in token) { - if (token.hasOwnProperty(part) && /UNKNOWN/.test(token[part])) { - token[part] = undefined; - } - } - - return token; - }); - } - - return tokens; -}; - -/** - * Comparator function to sort an array of objects by a property. - * - * @private - * - * @param {string} propertyName - The name of the property to sort by. - * @return {function} - The comparator function. - */ -Document.sortByProperty_ = function(propertyName) { - return function(entityA, entityB) { - if (entityA[propertyName] < entityB[propertyName]) { - return 1; - } - - if (entityA[propertyName] > entityB[propertyName]) { - return -1; - } - - return 0; - }; -}; - /** * Check if the user provided an encodingType, and map it to its API value. * diff --git a/packages/google-cloud-language/src/index.js b/packages/google-cloud-language/src/index.js index 092cbc1d613..c3e39fcb76a 100644 --- a/packages/google-cloud-language/src/index.js +++ b/packages/google-cloud-language/src/index.js @@ -87,8 +87,6 @@ function Language(options) { * [`EncodingType`](https://cloud.google.com/natural-language/reference/rest/v1/EncodingType) * @param {string} options.language - The language of the text. * @param {string} options.type - The type of document, either `html` or `text`. - * @param {boolean} options.verbose - Enable verbose mode for more detailed - * results. Default: `false` * @param {function} callback - See {module:language/document#annotate}. * * @example @@ -131,15 +129,6 @@ function Language(options) { * language.annotate('¿Dónde está la sede de Google?', options, callback); * * //- - * // Verbose mode may also be enabled for more detailed results. - * //- - * var options = { - * verbose: true - * }; - * - * language.annotate('Hello!', options, callback); - * - * //- * // If the callback is omitted, we'll return a Promise. * //- * language.annotate('Hello!').then(function(data) { @@ -181,8 +170,6 @@ Language.prototype.annotate = function(content, options, callback) { * [`EncodingType`](https://cloud.google.com/natural-language/reference/rest/v1/EncodingType) * @param {string} options.language - The language of the text. * @param {string} options.type - The type of document, either `html` or `text`. - * @param {boolean} options.verbose - Enable verbose mode for more detailed - * results. Default: `false` * @param {function} callback - See {module:language/document#detectEntities}. * * @example @@ -224,15 +211,6 @@ Language.prototype.annotate = function(content, options, callback) { * language.detectEntities('Axel Foley es de Detroit', options, callback); * * //- - * // Verbose mode may also be enabled for more detailed results. - * //- - * var options = { - * verbose: true - * }; - * - * language.detectEntities('Axel Foley is from Detroit', options, callback); - * - * //- * // If the callback is omitted, we'll return a Promise. * //- * language.detectEntities('Axel Foley is from Detroit').then(function(data) { @@ -274,8 +252,6 @@ Language.prototype.detectEntities = function(content, options, callback) { * [`EncodingType`](https://cloud.google.com/natural-language/reference/rest/v1/EncodingType) * @param {string} options.language - The language of the text. * @param {string} options.type - The type of document, either `html` or `text`. - * @param {boolean} options.verbose - Enable verbose mode for more detailed - * results. Default: `false` * @param {function} callback - See {module:language/document#detectSentiment}. * * @example @@ -308,15 +284,6 @@ Language.prototype.detectEntities = function(content, options, callback) { * language.detectSentiment('<h1>Document Title</h1>', options, callback); * * //- - * // Verbose mode may also be enabled for more detailed results. - * //- - * var options = { - * verbose: true - * }; - * - * language.detectSentiment('Hello!', options, callback); - * - * //- * // If the callback is omitted, we'll return a Promise. * //- * language.detectSentiment('Hello!').then(function(data) { @@ -358,8 +325,6 @@ Language.prototype.detectSentiment = function(content, options, callback) { * [`EncodingType`](https://cloud.google.com/natural-language/reference/rest/v1/EncodingType) * @param {string} options.language - The language of the text. * @param {string} options.type - The type of document, either `html` or `text`. - * @param {boolean} options.verbose - Enable verbose mode for more detailed - * results. Default: `false` * @param {function} callback - See {module:language/document#detectSyntax}. * * @example @@ -401,15 +366,6 @@ Language.prototype.detectSentiment = function(content, options, callback) { * language.detectSyntax('Axel Foley es de Detroit', options, callback); * * //- - * // Verbose mode may also be enabled for more detailed results. - * //- - * var options = { - * verbose: true - * }; - * - * language.detectSyntax('Axel Foley is from Detroit', options, callback); - * - * //- * // If the callback is omitted, we'll return a Promise. * //- * language.detectSyntax('Axel Foley is from Detroit').then(function(data) { diff --git a/packages/google-cloud-language/system-test/language.js b/packages/google-cloud-language/system-test/language.js index 3d30af222d9..27b9362f2ae 100644 --- a/packages/google-cloud-language/system-test/language.js +++ b/packages/google-cloud-language/system-test/language.js @@ -203,61 +203,44 @@ describe('Language', function() { describe('annotation', function() { it('should work without creating a document', function(done) { if (!CONTENT_TYPE) { - language.annotate(CONTENT, validateAnnotationSimple(done)); + language.annotate(CONTENT, validateAnnotation(done)); return; } language.annotate( CONTENT, { type: CONTENT_TYPE }, - validateAnnotationSimple(done) + validateAnnotation(done) ); }); it('should return the correct simplified response', function(done) { - DOC.annotate(validateAnnotationSimple(done)); - }); - - it('should support verbose mode', function(done) { - DOC.annotate({ verbose: true }, validateAnnotationVerbose(done)); + DOC.annotate(validateAnnotation(done)); }); it('should return only a single feature', function(done) { DOC.annotate({ entities: true - }, validateAnnotationSingleFeatureSimple(done)); - }); - - it('should return a single feat in verbose mode', function(done) { - DOC.annotate({ - entities: true, - verbose: true - }, validateAnnotationSingleFeatureVerbose(done)); + }, validateAnnotationSingleFeature(done)); }); }); describe('entities', function() { it('should work without creating a document', function(done) { if (!CONTENT_TYPE) { - language.detectEntities(CONTENT, validateEntitiesSimple(done)); + language.detectEntities(CONTENT, validateEntities(done)); return; } language.detectEntities( CONTENT, { type: CONTENT_TYPE }, - validateEntitiesSimple(done) + validateEntities(done) ); }); it('should return the correct simplified response', function(done) { - DOC.detectEntities(validateEntitiesSimple(done)); - }); - - it('should support verbose mode', function(done) { - DOC.detectEntities({ - verbose: true - }, validateEntitiesVerbose(done)); + DOC.detectEntities(validateEntities(done)); }); }); @@ -266,7 +249,7 @@ describe('Language', function() { if (!CONTENT_TYPE) { language.detectSentiment( CONTENT, - validateSentimentSimple(done) + validateSentiment(done) ); return; } @@ -274,18 +257,12 @@ describe('Language', function() { language.detectSentiment( CONTENT, { type: CONTENT_TYPE }, - validateSentimentSimple(done) + validateSentiment(done) ); }); it('should return the correct simplified response', function(done) { - DOC.detectSentiment(validateSentimentSimple(done)); - }); - - it('should support verbose mode', function(done) { - DOC.detectSentiment({ - verbose: true - }, validateSentimentVerbose(done)); + DOC.detectSentiment(validateSentiment(done)); }); }); @@ -294,7 +271,7 @@ describe('Language', function() { if (!CONTENT_TYPE) { language.detectSyntax( CONTENT, - validateSyntaxSimple(done) + validateSyntax(done) ); return; } @@ -302,18 +279,12 @@ describe('Language', function() { language.detectSyntax( CONTENT, { type: CONTENT_TYPE }, - validateSyntaxSimple(done) + validateSyntax(done) ); }); it('should return the correct simplified response', function(done) { - DOC.detectSyntax(validateSyntaxSimple(done)); - }); - - it('should support verbose mode', function(done) { - DOC.detectSyntax({ - verbose: true - }, validateSyntaxVerbose(done)); + DOC.detectSyntax(validateSyntax(done)); }); }); }); @@ -339,100 +310,16 @@ describe('Language', function() { }); } - function validateAnnotationSimple(callback) { - return function(err, annotation, apiResponse) { - try { - assert.ifError(err); - - assert.strictEqual(annotation.language, 'en'); - - assert(is.number(annotation.sentiment)); - - assert.deepEqual(annotation.entities, { - people: ['stephen', 'david'], - places: ['michigan'] - }); - - assert.deepEqual(annotation.sentences, TEXT_CONTENT_SENTENCES); - - assert(is.array(annotation.tokens)); - assert.deepEqual(annotation.tokens[0], { - text: 'Hello', - partOfSpeech: 'Other: foreign words, typos, abbreviations', - tag: 'X', - aspect: undefined, - case: undefined, - form: undefined, - gender: undefined, - mood: undefined, - number: undefined, - person: undefined, - proper: undefined, - reciprocity: undefined, - tense: undefined, - voice: undefined, - dependencyEdge: { - description: 'Root', - label: 'ROOT', - headTokenIndex: 0 - } - }); - - assert(is.object(apiResponse)); - - callback(); - } catch(e) { - callback(e); - } - }; - } - - function validateAnnotationVerbose(callback) { + function validateAnnotation(callback) { return function(err, annotation, apiResponse) { try { assert.ifError(err); - assert.strictEqual(annotation.language, 'en'); - - assert(is.object(annotation.sentiment)); - - assert(is.array(annotation.entities.people)); - assert.strictEqual(annotation.entities.people.length, 2); - assert(is.object(annotation.entities.people[0])); - - assert(is.array(annotation.sentences)); - assert(is.object(annotation.sentences[0])); - - assert(is.array(annotation.tokens)); - assert(is.object(annotation.tokens[0])); - assert.strictEqual(annotation.tokens[0].text.content, 'Hello'); - - assert(is.object(apiResponse)); - - callback(); - } catch(e) { - callback(e); - } - }; - } - - function validateAnnotationSingleFeatureSimple(callback) { - return function(err, annotation, apiResponse) { - try { - assert.ifError(err); - - assert.strictEqual(annotation.language, 'en'); - - assert.deepEqual(annotation.entities, { - people: ['stephen', 'david'], - places: ['michigan'] - }); - - assert.strictEqual(annotation.sentences, undefined); - assert.strictEqual(annotation.sentiment, undefined); - assert.strictEqual(annotation.tokens, undefined); - - assert(is.object(apiResponse)); + assert.strictEqual(annotation.language, apiResponse.language); + assert.deepEqual(annotation.sentiment, apiResponse.documentSentiment); + assert.deepEqual(annotation.entities, apiResponse.entities); + assert.deepEqual(annotation.sentences, apiResponse.sentences); + assert.deepEqual(annotation.tokens, apiResponse.tokens); callback(); } catch(e) { @@ -441,16 +328,13 @@ describe('Language', function() { }; } - function validateAnnotationSingleFeatureVerbose(callback) { + function validateAnnotationSingleFeature(callback) { return function(err, annotation, apiResponse) { try { assert.ifError(err); - assert.strictEqual(annotation.language, 'en'); - - assert(is.array(annotation.entities.people)); - assert.strictEqual(annotation.entities.people.length, 2); - assert(is.object(annotation.entities.people[0])); + assert.strictEqual(annotation.language, apiResponse.language); + assert.deepEqual(annotation.entities, apiResponse.entities); assert.strictEqual(annotation.sentences, undefined); assert.strictEqual(annotation.sentiment, undefined); @@ -465,50 +349,11 @@ describe('Language', function() { }; } - function validateEntitiesSimple(callback) { + function validateEntities(callback) { return function(err, entities, apiResponse) { try { assert.ifError(err); - - assert.deepEqual(entities, { - people: ['stephen', 'david'], - places: ['michigan'] - }); - - assert(is.object(apiResponse)); - - callback(); - } catch(e) { - callback(e); - } - }; - } - - function validateEntitiesVerbose(callback) { - return function(err, entities, apiResponse) { - try { - assert.ifError(err); - - assert(is.array(entities.people)); - assert.strictEqual(entities.people.length, 2); - assert(is.object(entities.people[0])); - - assert(is.object(apiResponse)); - - callback(); - } catch(e) { - callback(e); - } - }; - } - - function validateSentimentSimple(callback) { - return function(err, sentiment, apiResponse) { - try { - assert.ifError(err); - - assert(is.number(sentiment)); - assert(is.object(apiResponse)); + assert.strictEqual(entities, apiResponse.entities); callback(); } catch(e) { @@ -517,18 +362,11 @@ describe('Language', function() { }; } - function validateSentimentVerbose(callback) { + function validateSentiment(callback) { return function(err, sentiment, apiResponse) { try { assert.ifError(err); - - assert(is.object(sentiment)); - assert(is.number(sentiment.score)); - assert(is.number(sentiment.magnitude)); - assert.strictEqual(sentiment.language, 'en'); - assert.strictEqual(sentiment.sentences.length, 2); - - assert(is.object(apiResponse)); + assert.strictEqual(sentiment, apiResponse.documentSentiment); callback(); } catch(e) { @@ -537,53 +375,11 @@ describe('Language', function() { }; } - function validateSyntaxSimple(callback) { - return function(err, tokens, apiResponse) { - try { - assert.ifError(err); - assert.strictEqual(tokens.length, 17); - assert.deepEqual(tokens[0], { - aspect: undefined, - case: undefined, - form: undefined, - gender: undefined, - mood: undefined, - number: undefined, - partOfSpeech: 'Other: foreign words, typos, abbreviations', - person: undefined, - proper: undefined, - reciprocity: undefined, - tag: 'X', - tense: undefined, - text: 'Hello', - voice: undefined, - dependencyEdge: { - description: 'Root', - headTokenIndex: 0, - label: 'ROOT' - } - }); - - assert(is.object(apiResponse)); - - callback(); - } catch (e) { - callback(e); - } - }; - } - - function validateSyntaxVerbose(callback) { + function validateSyntax(callback) { return function(err, syntax, apiResponse) { try { assert.ifError(err); - assert.strictEqual(syntax.sentences.length, 2); - assert(is.object(syntax.sentences[0])); - assert.strictEqual(syntax.tokens.length, 17); - assert(is.object(syntax.tokens[0])); - assert.strictEqual(syntax.language, 'en'); - - assert(is.object(apiResponse)); + assert.strictEqual(syntax, apiResponse.tokens); callback(); } catch (e) { diff --git a/packages/google-cloud-language/test/document.js b/packages/google-cloud-language/test/document.js index 8c2dee343dc..ab0a5b55c36 100644 --- a/packages/google-cloud-language/test/document.js +++ b/packages/google-cloud-language/test/document.js @@ -18,7 +18,6 @@ var assert = require('assert'); var extend = require('extend'); -var prop = require('propprop'); var proxyquire = require('proxyquire'); var util = require('@google-cloud/common').util; @@ -402,13 +401,6 @@ describe('Document', function() { }; } - beforeEach(function() { - Document.formatSentiment_ = util.noop; - Document.formatEntities_ = util.noop; - Document.formatSentences_ = util.noop; - Document.formatTokens_ = util.noop; - }); - it('should always return the language', function(done) { var apiResponse = apiResponses.default; @@ -431,48 +423,28 @@ describe('Document', function() { annotateText: createAnnotateTextWithResponse(apiResponse) }; - var formattedSentences = []; - Document.formatSentences_ = function(sentences, verbose) { - assert.strictEqual(sentences, apiResponse.sentences); - assert.strictEqual(verbose, false); - return formattedSentences; - }; - - var formattedTokens = []; - Document.formatTokens_ = function(tokens, verbose) { - assert.strictEqual(tokens, apiResponse.tokens); - assert.strictEqual(verbose, false); - return formattedTokens; - }; - document.annotate(function(err, annotation, apiResponse_) { assert.ifError(err); - assert.strictEqual(annotation.sentences, formattedSentences); - assert.strictEqual(annotation.tokens, formattedTokens); + assert.strictEqual(annotation.sentences, apiResponse.sentences); + assert.strictEqual(annotation.tokens, apiResponse.tokens); assert.deepEqual(apiResponse_, apiResponse); done(); }); }); - it('should return the formatted sentiment if available', function(done) { + it('should return the sentiment if available', function(done) { var apiResponse = apiResponses.withSentiment; document.api.Language = { annotateText: createAnnotateTextWithResponse(apiResponse) }; - var formattedSentiment = {}; - Document.formatSentiment_ = function(sentiment, verbose) { - assert.strictEqual(sentiment, apiResponse.documentSentiment); - assert.strictEqual(verbose, false); - return formattedSentiment; - }; - document.annotate(function(err, annotation, apiResponse_) { assert.ifError(err); assert.strictEqual(annotation.language, apiResponse.language); - assert.strictEqual(annotation.sentiment, formattedSentiment); + assert + .strictEqual(annotation.sentiment, apiResponse.documentSentiment); assert.deepEqual(apiResponse_, apiResponse); @@ -480,25 +452,18 @@ describe('Document', function() { }); }); - it('should return the formatted entities if available', function(done) { + it('should return the entities if available', function(done) { var apiResponse = apiResponses.withEntities; document.api.Language = { annotateText: createAnnotateTextWithResponse(apiResponse) }; - var formattedEntities = []; - Document.formatEntities_ = function(entities, verbose) { - assert.strictEqual(entities, apiResponse.entities); - assert.strictEqual(verbose, false); - return formattedEntities; - }; - document.annotate(function(err, annotation, apiResponse_) { assert.ifError(err); assert.strictEqual(annotation.language, apiResponse.language); - assert.strictEqual(annotation.entities, formattedEntities); + assert.strictEqual(annotation.entities, apiResponse.entities); assert.deepEqual(apiResponse_, apiResponse); @@ -525,37 +490,6 @@ describe('Document', function() { done(); }); }); - - it('should allow verbose mode', function(done) { - var apiResponse = apiResponses.withAll; - - document.api.Language = { - annotateText: createAnnotateTextWithResponse(apiResponse) - }; - - var numCallsWithCorrectVerbosityArgument = 0; - - function incrementVerbosityVar(_, verbose) { - if (verbose === true) { - numCallsWithCorrectVerbosityArgument++; - } - } - - Document.formatSentiment_ = incrementVerbosityVar; - Document.formatEntities_ = incrementVerbosityVar; - Document.formatSentences_ = incrementVerbosityVar; - Document.formatTokens_ = incrementVerbosityVar; - - document.annotate({ - verbose: true - }, function(err) { - assert.ifError(err); - - assert.strictEqual(numCallsWithCorrectVerbosityArgument, 4); - - done(); - }); - }); }); }); @@ -606,8 +540,6 @@ describe('Document', function() { entities: [] }; - var originalApiResponse = extend({}, apiResponse); - beforeEach(function() { document.api.Language = { analyzeEntities: function(reqOpts, callback) { @@ -616,40 +548,14 @@ describe('Document', function() { }; }); - it('should format the entities', function(done) { - var formattedEntities = {}; - - Document.formatEntities_ = function(entities, verbose) { - assert.strictEqual(entities, apiResponse.entities); - assert.strictEqual(verbose, false); - return formattedEntities; - }; - - document.detectEntities(function(err, entities) { - assert.ifError(err); - assert.strictEqual(entities, formattedEntities); - done(); - }); - }); - - it('should clone the response object', function(done) { + it('should return the entities', function(done) { document.detectEntities(function(err, entities, apiResponse_) { assert.ifError(err); - assert.notStrictEqual(apiResponse_, apiResponse); - assert.deepEqual(apiResponse_, originalApiResponse); - done(); - }); - }); - it('should allow verbose mode', function(done) { - Document.formatEntities_ = function(entities, verbose) { - assert.strictEqual(verbose, true); + assert.strictEqual(entities, apiResponse.entities); + assert.strictEqual(apiResponse_, apiResponse); done(); - }; - - document.detectEntities({ - verbose: true - }, assert.ifError); + }); }); }); }); @@ -704,11 +610,7 @@ describe('Document', function() { language: 'en' }; - var originalApiResponse = extend({}, apiResponse); - beforeEach(function() { - Document.formatSentiment_ = util.noop; - document.api.Language = { analyzeSentiment: function(reqOpts, callback) { callback(null, apiResponse); @@ -716,61 +618,12 @@ describe('Document', function() { }; }); - it('should format the sentiment', function(done) { - var formattedSentiment = {}; - - Document.formatSentiment_ = function(sentiment, verbose) { - assert.strictEqual(sentiment, apiResponse.documentSentiment); - assert.strictEqual(verbose, false); - return formattedSentiment; - }; - - document.detectSentiment(function(err, sentiment) { - assert.ifError(err); - assert.strictEqual(sentiment, formattedSentiment); - done(); - }); - }); - - it('should clone the response object', function(done) { + it('should return the sentiment object', function(done) { document.detectSentiment(function(err, sentiment, apiResponse_) { assert.ifError(err); - assert.notStrictEqual(apiResponse_, apiResponse); - assert.deepEqual(apiResponse_, originalApiResponse); - done(); - }); - }); - - it('should allow verbose mode', function(done) { - var fakeSentiment = {}; - Document.formatSentiment_ = function(sentiment, verbose) { assert.strictEqual(sentiment, apiResponse.documentSentiment); - assert.strictEqual(verbose, true); - return fakeSentiment; - }; - - var fakeSentences = []; - - Document.formatSentences_ = function(sentences, verbose) { - assert.strictEqual(sentences, apiResponse.sentences); - assert.strictEqual(verbose, true); - return fakeSentences; - }; - - var options = { - verbose: true - }; - - document.detectSentiment(options, function(err, sentiment, resp) { - assert.ifError(err); - - assert.strictEqual(sentiment, fakeSentiment); - assert.strictEqual(sentiment.sentences, fakeSentences); - assert.strictEqual(sentiment.language, 'en'); - - assert.deepEqual(resp, apiResponse); - + assert.strictEqual(apiResponse_, apiResponse); done(); }); }); @@ -827,12 +680,7 @@ describe('Document', function() { language: 'en' }; - var originalApiResponse = extend({}, apiResponse); - beforeEach(function() { - Document.formatTokens_ = util.noop; - Document.formatSentences_ = util.noop; - document.api.Language = { analyzeSyntax: function(reqOpts, callback) { callback(null, apiResponse); @@ -840,308 +688,18 @@ describe('Document', function() { }; }); - it('should format the tokens', function(done) { - var formattedTokens = [{}]; - - Document.formatTokens_ = function(tokens, verbose) { - assert.strictEqual(tokens, apiResponse.tokens); - assert.strictEqual(verbose, false); - return formattedTokens; - }; - - document.detectSyntax(function(err, syntax) { - assert.ifError(err); - assert.strictEqual(syntax, formattedTokens); - done(); - }); - }); - - it('should clone the response object', function(done) { + it('should return the token list', function(done) { document.detectSyntax(function(err, syntax, apiResponse_) { assert.ifError(err); - assert.notStrictEqual(apiResponse_, apiResponse); - assert.deepEqual(apiResponse_, originalApiResponse); - done(); - }); - }); - - it('should allow verbose mode', function(done) { - var fakeTokens = []; - - Document.formatTokens_ = function(tokens, verbose) { - assert.strictEqual(tokens, apiResponse.tokens); - assert.strictEqual(verbose, true); - return fakeTokens; - }; - - var fakeSentences = []; - - Document.formatSentences_ = function(sentences, verbose) { - assert.strictEqual(sentences, apiResponse.sentences); - assert.strictEqual(verbose, true); - return fakeSentences; - }; - - var options = { - verbose: true - }; - - document.detectSyntax(options, function(err, syntax, resp) { - assert.ifError(err); - - assert.strictEqual(syntax.tokens, fakeTokens); - assert.strictEqual(syntax.sentences, fakeSentences); - assert.strictEqual(syntax.language, 'en'); - - assert.deepEqual(resp, apiResponse); + assert.strictEqual(syntax, apiResponse.tokens); + assert.strictEqual(apiResponse_, apiResponse); done(); }); }); }); }); - describe('formatEntities_', function() { - var ENTITIES = [ - { type: 'UNKNOWN', salience: -1, name: 'second' }, - { type: 'UNKNOWN', salience: 1, name: 'first' }, - - { type: 'PERSON', salience: -1, name: 'second' }, - { type: 'PERSON', salience: 1, name: 'first' }, - - { type: 'LOCATION', salience: -1, name: 'second' }, - { type: 'LOCATION', salience: 1, name: 'first' }, - - { type: 'ORGANIZATION', salience: -1, name: 'second' }, - { type: 'ORGANIZATION', salience: 1, name: 'first' }, - - { type: 'EVENT', salience: -1, name: 'second' }, - { type: 'EVENT', salience: 1, name: 'first' }, - - { type: 'WORK_OF_ART', salience: -1, name: 'second' }, - { type: 'WORK_OF_ART', salience: 1, name: 'first' }, - - { type: 'CONSUMER_GOOD', salience: -1, name: 'second' }, - { type: 'CONSUMER_GOOD', salience: 1, name: 'first' }, - - { type: 'OTHER', salience: -1, name: 'second' }, - { type: 'OTHER', salience: 1, name: 'first' } - ]; - - var VERBOSE = false; - - var entitiesCopy = extend(true, {}, ENTITIES); - var FORMATTED_ENTITIES = { - unknown: [ entitiesCopy[1], entitiesCopy[0] ], - people: [ entitiesCopy[3], entitiesCopy[2] ], - places: [ entitiesCopy[5], entitiesCopy[4] ], - organizations: [ entitiesCopy[7], entitiesCopy[6] ], - events: [ entitiesCopy[9], entitiesCopy[8] ], - art: [ entitiesCopy[11], entitiesCopy[10] ], - goods: [ entitiesCopy[13], entitiesCopy[12] ], - other: [ entitiesCopy[15], entitiesCopy[14] ], - }; - - var EXPECTED_FORMATTED_ENTITIES = { - default: extend(true, {}, FORMATTED_ENTITIES), - verbose: extend(true, {}, FORMATTED_ENTITIES) - }; - - for (var entityGroupType in EXPECTED_FORMATTED_ENTITIES.default) { - // Only the `name` property is returned by default: - EXPECTED_FORMATTED_ENTITIES.default[entityGroupType] = - EXPECTED_FORMATTED_ENTITIES.default[entityGroupType].map(prop('name')); - } - - it('should group and sort entities correctly', function() { - var formattedEntities = Document.formatEntities_(ENTITIES, VERBOSE); - - Document.sortByProperty_ = function(propertyName) { - assert.strictEqual(propertyName, 'salience'); - return function() { return -1; }; - }; - - assert.deepEqual(formattedEntities, EXPECTED_FORMATTED_ENTITIES.default); - }); - - it('should group and sort entities correctly in verbose mode', function() { - var formattedEntities = Document.formatEntities_(ENTITIES, true); - - Document.sortByProperty_ = function(propertyName) { - assert.strictEqual(propertyName, 'salience'); - return function() { return -1; }; - }; - - assert.deepEqual(formattedEntities, EXPECTED_FORMATTED_ENTITIES.verbose); - }); - }); - - describe('formatSentences_', function() { - var SENTENCES = [ - { - text: { - content: 'Sentence text', - property: 'value' - } - }, - { - text: { - content: 'Another sentence', - property: 'value' - } - } - ]; - - var VERBOSE = false; - - var EXPECTED_FORMATTED_SENTENCES = { - default: SENTENCES.map(prop('text')).map(prop('content')), - verbose: SENTENCES - }; - - it('should correctly format sentences', function() { - var formattedSentences = Document.formatSentences_(SENTENCES, VERBOSE); - - assert.deepEqual( - formattedSentences, - EXPECTED_FORMATTED_SENTENCES.default - ); - }); - - it('should correctly format sentences in verbose mode', function() { - var formattedSentences = Document.formatSentences_(SENTENCES, true); - - assert.deepEqual( - formattedSentences, - EXPECTED_FORMATTED_SENTENCES.verbose - ); - }); - }); - - describe('formatSentiment_', function() { - var SENTIMENT = { - score: -0.5, - magnitude: 0.5 - }; - - var VERBOSE = false; - - var EXPECTED_FORMATTED_SENTIMENT = { - default: SENTIMENT.score, - verbose: { - score: SENTIMENT.score, - magnitude: SENTIMENT.magnitude - } - }; - - it('should format the sentiment correctly', function() { - var sentiment = extend({}, SENTIMENT); - var formattedSentiment = Document.formatSentiment_(sentiment, VERBOSE); - - assert.deepEqual( - formattedSentiment, - EXPECTED_FORMATTED_SENTIMENT.default - ); - }); - - it('should format the sentiment correctly in verbose mode', function() { - var sentiment = extend({}, SENTIMENT); - var formattedSentiment = Document.formatSentiment_(sentiment, true); - - assert.deepEqual( - formattedSentiment, - EXPECTED_FORMATTED_SENTIMENT.verbose - ); - }); - }); - - describe('formatTokens_', function() { - var TOKENS = [ - { - text: { - content: 'Text content' - }, - partOfSpeech: { - tag: 'PART_OF_SPEECH_TAG', - fakePart: 'UNKNOWN' - }, - property: 'value', - dependencyEdge: { - label: 'FAKE_LABEL' - } - } - ]; - - var VERBOSE = false; - - var LABEL_DESCRIPTIONS = { - FAKE_LABEL: 'fake label description' - }; - - var PART_OF_SPEECH = { - PART_OF_SPEECH_TAG: 'part of speech value' - }; - - var EXPECTED_FORMATTED_TOKENS = { - default: TOKENS.map(function(token) { - return { - text: token.text.content, - partOfSpeech: PART_OF_SPEECH.PART_OF_SPEECH_TAG, - tag: 'PART_OF_SPEECH_TAG', - fakePart: undefined, - dependencyEdge: { - label: 'FAKE_LABEL', - description: LABEL_DESCRIPTIONS.FAKE_LABEL - } - }; - }), - - verbose: TOKENS - }; - - beforeEach(function() { - Document.PART_OF_SPEECH = PART_OF_SPEECH; - Document.LABEL_DESCRIPTIONS = LABEL_DESCRIPTIONS; - }); - - it('should correctly format tokens', function() { - var formattedTokens = Document.formatTokens_(TOKENS, VERBOSE); - - assert.deepEqual(formattedTokens, EXPECTED_FORMATTED_TOKENS.default); - }); - - it('should correctly format tokens in verbose mode', function() { - var formattedTokens = Document.formatTokens_(TOKENS, true); - - assert.deepEqual(formattedTokens, EXPECTED_FORMATTED_TOKENS.verbose); - }); - }); - - describe('sortByProperty_', function() { - var sortFn; - - beforeEach(function() { - sortFn = Document.sortByProperty_('sortedProperty'); - }); - - it('should sort by a property name', function() { - assert.strictEqual( - sortFn({ sortedProperty: 0 }, { sortedProperty: 1 }), - 1 - ); - - assert.strictEqual( - sortFn({ sortedProperty: 1 }, { sortedProperty: -1 }), - -1 - ); - - assert.strictEqual( - sortFn({ sortedProperty: 0 }, { sortedProperty: 0 }), - 0 - ); - }); - }); - describe('detectEncodingType_', function() { it('should return if no encoding type is set', function() { assert.strictEqual(document.detectEncodingType_({