UNPKG

node-nlp

Version:

Library for NLU (Natural Language Understanding) done in Node.js

215 lines (179 loc) 8.82 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: nlp/stemmers/natural/token.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: nlp/stemmers/natural/token.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/* Copyright (c) 2015, Luís Rodrigues Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ module.exports = (function () { 'use strict'; /** * Stemmer token constructor. * * @param {String} string Token string. */ var Token = function (string) { this.vowels = ''; this.regions = {}; this.string = string; this.original = string; } /** * Set vowels. * * @param {String|Array} vowels List of vowels. * @return {Token} Token instance. */ Token.prototype.usingVowels = function (vowels) { this.vowels = vowels; return this; }; /** * Marks a region by defining its starting index or providing a callback * function that does. * * @param {String} region Region name. * @param {Array|Number} args Callback arguments or region start index. * @param {Function} callback Function that determines the start index (optional). * @param {Object} context Callback context (optional, defaults to this). * @return {Token} Token instance. */ Token.prototype.markRegion = function (region, args, callback, context) { if (typeof callback === 'function') { this.regions[region] = callback.apply(context || this, [].concat(args)); } else if (!isNaN(args)) { this.regions[region] = args; } return this; }; /** * Replaces all instances of a string with another. * * @param {String} find String to be replaced. * @param {String} replace Replacement string. * @return {Token} Token instance. */ Token.prototype.replaceAll = function (find, replace) { this.string = this.string.split(find).join(replace); return this; }; /** * Replaces the token suffix if in a region. * * @param {String} suffix Suffix to replace. * @param {String} replace Replacement string. * @param {String} region Region name. * @return {Token} Token instance. */ Token.prototype.replaceSuffixInRegion = function (suffix, replace, region) { var suffixes = [].concat(suffix); for (var i = 0; i &lt; suffixes.length; i++) { if (this.hasSuffixInRegion(suffixes[i], region)) { this.string = this.string.slice(0, -suffixes[i].length) + replace; return this; } } return this; }; /** * Determines whether the token has a vowel at the provided index. * * @param {Integer} index Character index. * @return {Boolean} Whether the token has a vowel at the provided index. */ Token.prototype.hasVowelAtIndex = function (index) { return this.vowels.indexOf(this.string[index]) !== -1; }; /** * Finds the next vowel in the token. * * @param {Integer} start Starting index offset. * @return {Integer} Vowel index, or the end of the string. */ Token.prototype.nextVowelIndex = function (start) { var index = (start >= 0 &amp;&amp; start &lt; this.string.length) ? start : this.string.length; while (index &lt; this.string.length &amp;&amp; !this.hasVowelAtIndex(index)) { index++; } return index; }; /** * Finds the next consonant in the token. * * @param {Integer} start Starting index offset. * @return {Integer} Consonant index, or the end of the string. */ Token.prototype.nextConsonantIndex = function (start) { var index = (start >= 0 &amp;&amp; start &lt; this.string.length) ? start : this.string.length; while (index &lt; this.string.length &amp;&amp; this.hasVowelAtIndex(index)) { index++; } return index; }; /** * Determines whether the token has the provided suffix. * @param {String} suffix Suffix to match. * @return {Boolean} Whether the token string ends in suffix. */ Token.prototype.hasSuffix = function (suffix) { return this.string.slice(-suffix.length) === suffix; }; /** * Determines whether the token has the provided suffix within the specified * region. * * @param {String} suffix Suffix to match. * @param  {String} region Region name. * @return {Boolean} Whether the token string ends in suffix. */ Token.prototype.hasSuffixInRegion = function (suffix, region) { var regionStart = this.regions[region] || 0, suffixStart = this.string.length - suffix.length; return this.hasSuffix(suffix) &amp;&amp; suffixStart >= regionStart; }; return Token; })(); </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BinaryNeuralNetworkClassifier.html">BinaryNeuralNetworkClassifier</a></li><li><a href="Classifier.html">Classifier</a></li><li><a href="ConversationContext.html">ConversationContext</a></li><li><a href="DutchStemmer.html">DutchStemmer</a></li><li><a href="EnglishStemmer.html">EnglishStemmer</a></li><li><a href="EnumNamedEntity.html">EnumNamedEntity</a></li><li><a href="Evaluator.html">Evaluator</a></li><li><a href="HungarianStemmer.html">HungarianStemmer</a></li><li><a href="ItalianStemmer.html">ItalianStemmer</a></li><li><a href="Language.html">Language</a></li><li><a href="LogisticRegressionClassifier.html">LogisticRegressionClassifier</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="MemoryConversationContext.html">MemoryConversationContext</a></li><li><a href="NamedEntity.html">NamedEntity</a></li><li><a href="NerManager.html">NerManager</a></li><li><a href="NlgManager.html">NlgManager</a></li><li><a href="NlpClassifier.html">NlpClassifier</a></li><li><a href="NlpManager.html">NlpManager</a></li><li><a href="NorwegianStemmer.html">NorwegianStemmer</a></li><li><a href="PortugueseStemmer.html">PortugueseStemmer</a></li><li><a href="Recognizer.html">Recognizer</a></li><li><a href="RegexNamedEntity.html">RegexNamedEntity</a></li><li><a href="RomanianStemmer.html">RomanianStemmer</a></li><li><a href="RussianStemmer.html">RussianStemmer</a></li><li><a href="SentimentAnalyzer.html">SentimentAnalyzer</a></li><li><a href="SentimentManager.html">SentimentManager</a></li><li><a href="SimilarSearch.html">SimilarSearch</a></li><li><a href="SlotManager.html">SlotManager</a></li><li><a href="StemmerJa.html">StemmerJa</a></li><li><a href="SwedishStemmer.html">SwedishStemmer</a></li><li><a href="Tokenizer.html">Tokenizer</a></li><li><a href="TrimNamedEntity.html">TrimNamedEntity</a></li><li><a href="TurkishStemmer.html">TurkishStemmer</a></li><li><a href="Vector.html">Vector</a></li><li><a href="XTable.html">XTable</a></li></ul><h3>Global</h3><ul><li><a href="global.html#endsinArr">endsinArr</a></li><li><a href="global.html#prelude">prelude</a></li><li><a href="global.html#regions">regions</a></li><li><a href="global.html#stem">stem</a></li><li><a href="global.html#stopwords">stopwords</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sat Oct 13 2018 19:14:51 GMT+0200 (CEST) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>