1 /* (This is the new BSD license.)
  2 * Copyright (c) 2014, Chris Culy
  3 * All rights reserved.
  4 *
  5 * Redistribution and use in source and binary forms, with or without
  6 * modification, are permitted provided that the following conditions are met:
  7 *     * Redistributions of source code must retain the above copyright
  8 *       notice, this list of conditions and the following disclaimer.
  9 *     * Redistributions in binary form must reproduce the above copyright
 10 *       notice, this list of conditions and the following disclaimer in the
 11 *       documentation and/or other materials provided with the distribution.
 12 *     * Neither the name of the Chris Culy nor the 
 13 *		names of its contributors may be used to endorse or promote 
 14 *		products from this software without specific prior written permission.
 15 *
 16 * THIS SOFTWARE IS PROVIDED BY Chris Culy
 17 * ``AS IS'' AND ANY OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 19 * ARE DISCLAIMED. IN NO EVENT SHALL Chris Culy
 20 * BE LIABLE FOR ANY, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 21 * CONSEQUENTIAL DAMAGES INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 22 * GOODS OR SERVICES; OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 23 * CAUSED AND ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 24 * TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 26 */
 27 
 28 "use strict";
 29 var textmodel = textmodel || {};
 30 
 31 (function(){
 32 
 33 /**
 34   * @class textmodel.TextModelUtilities
 35   *  This is the class for the TextModelUtilities
 36   * <p>
 37   * The constructor
 38   * @param _fldNames is an array of strings of field names
 39   * @param _fldDelim is a string containing the field delimiter
 40   * @param tiToString is a function that will become the toString method of {@link textmodel.TextModelUtilities.TextInfo}
 41   */   
 42 textmodel.TextModelUtilities = function(_fldNames, _fldDelim, tiToString) {
 43     
 44     var what = this;
 45     var fldNames = _fldNames;
 46     var fldDelim = _fldDelim;
 47     
 48     /**
 49      * Convenience function that recursively creates TextInfo objects from (arrys of) array elments
 50      * @param arrayOfText is an array of delimited text, or an array of arrays of delimited text
 51      * @returns corresponding array(s) of TextInfo objects
 52      */
 53     this.textArrayToTextInfo = function(arrayOfText) {
 54         var elt = arrayOfText[0]; //assuming this exists
 55         if (typeof elt === "object" && elt instanceof Array) { //cf. http://javascript.crockford.com/remedial.html
 56             return arrayOfText.map(function(e) {
 57                 return what.textArrayToTextInfo(e);    
 58             });
 59         }
 60         
 61         return arrayOfText.map(function(t) {
 62             return new what.TextInfo(t);    
 63         });
 64     }
 65     
 66     /**
 67      * Constructor for an object derived from delimited text e.g. dogs/dog/NN -> {token:"dogs", lemma:"dog", pos:"NN"}
 68      * @param text is the delmited text to convert into an object. The field names and delimiter come from {@link trextModel.textModelUtilities}
 69      */
 70     this.TextInfo = function(text) {
 71         var that = this;
 72         
 73         var pieces = text.split(fldDelim);
 74     
 75         pieces.forEach(function(p,i) {
 76             that[ fldNames[i] ] = p;
 77         });
 78        
 79         
 80     }
 81     
 82     if (typeof(tiToString) === 'function') {
 83         this.TextInfo.prototype.toString = tiToString;
 84     } else {
 85         this.TextInfo.prototype.toString = function() {
 86             return this[ fldNames[0] ];
 87         }
 88     }
 89     
 90     return this;
 91 }
 92     
 93 })();