1 
  2 ////////////////////////////////////////////////////////////////////////////////
  3 /**
  4  * @fileOverview Copyright (C) 2009 www.webis.de
  5  * @author Alexander Kuemmel alexander.kuemmel@uni-weimar.de
  6  * @author Christian Fricke christian.fricke@uni-weimar.de
  7  * @version 0.1
  8  */
  9 
 10 /**
 11  * Represents a data-structure that stores unique occurrences of terms
 12  * in a central storage mapped to specific numeric identifiers
 13  * @class Container for terms and unique ids
 14  * @constructor
 15  * @author Alexander Kuemmel alexander.kuemmel@uni-weimar.de
 16  * @author Christian Fricke christian.fricke@uni-weimar.de
 17  */
 18 de.aitools.js.Vocabulary = function () {
 19   /**
 20    * @private
 21    * @type {Object}
 22    */
 23   var termHashMap_ = {};
 24   /**
 25    * @private
 26    * @type {Object}
 27    */
 28   var idHashMap_ = {};
 29   /**
 30    * @private
 31    * @type {Number}
 32    */
 33   var size_ = 0;
 34   
 35   /**
 36    * Reset the content of the {@link de.aitools.js.Vocabulary} instance
 37    * @public
 38    */
 39   this.reset = function () {
 40     this.clear();
 41     // delete termHashMap_; termHashMap_ = {};
 42     // delete idHashMap_; idHashMap_ = {};
 43     // size_ = 0;
 44   };
 45   
 46   /**
 47    * Adds a string into the {@link de.aitools.js.Vocabulary} instance
 48    * and returns a unique id of it for later use
 49    * @param {String} term The string value to add
 50    * @public
 51    * @returns {Number} Unique term identifier
 52    */
 53   this.add = function (term) {
 54     if (!this.contains(term)) {
 55       termHashMap_[term] = size_;
 56       idHashMap_[size_] = term;
 57       size_ += 1;
 58     }
 59     return this.getId(term);
 60   };
 61   
 62   /**
 63    * Returns the term for the given unique id
 64    * @param {Number} id A unique numeric identifier
 65    * @public
 66    * @returns {String} Stored term
 67    */
 68   this.getTerm = function (id) {
 69     if (typeof(idHashMap_[id]) === "undefined") { return null; }
 70     return idHashMap_[id];
 71   };
 72   
 73   /**
 74    * Returns the unique identifier of the given string
 75    * @param {String} term A string value within the {@link de.aitools.js.Vocabulary}
 76    * @public
 77    * @returns {Number} Unique term identifier
 78    */
 79   this.getId = function (term) {
 80     if (typeof(termHashMap_[term]) === "undefined") {
 81       throw new Error("VocabularyException: Key not found");
 82     }
 83     return termHashMap_[term];
 84   };
 85   
 86   /**
 87    * Checks if the {@link de.aitools.js.Vocabulary} contains the term and adds the term
 88    * if not.
 89    * In each case the unique id of the term is returned.
 90    * @param {String} term String value
 91    * @private
 92    * @returns {Number} Unique term identifier
 93    */
 94   this.intern = function (term) {
 95     if (!this.contains(term)) { this.add(term); }
 96     return this.getId(term);
 97   };
 98   
 99   /**
100    * Returns a boolean whether the term exists in the {@link de.aitools.js.Vocabulary} or not
101    * @param {String} term String value
102    * @public
103    * @returns {Boolean}
104    */
105   this.contains = function (term) {
106     return (typeof(termHashMap_[term]) !== "undefined");
107   };
108   
109   /**
110    * Returns the number of terms within the {@link de.aitools.js.Vocabulary}
111    * @public
112    * @returns {Number}
113    */
114   this.size = function () {
115     return size_;
116   };
117   
118   /**
119    * Clears all data-structures with this {@link de.aitools.js.Vocabulary} instance
120    * @public
121    */
122   this.clear = function () {
123     for (var i in termHashMap_) {
124       if (termHashMap_.hasOwnProperty(i)) {
125         delete idHashMap_[termHashMap_[i]];
126         delete termHashMap_[i];
127       }
128     }
129     termHashMap_ = {};
130     idHashMap_ = {};
131     size_ = 0;
132   };
133 };
134 
135 // Vocabulary.js
136 ////////////////////////////////////////////////////////////////////////////////
137