1 
  2 ////////////////////////////////////////////////////////////////////////////////
  3 /**
  4  * @fileOverview Copyright (C) 2009 www.webis.de
  5  * @author Christof Braeutigam christof.braeutigam@uni-weimar.de
  6  * @version 0.1
  7  */
  8 
  9 /**
 10  * Document is a simple representation for a real-world document. It may have
 11  * one plaintext, one unique identifier and an arbitrary number of features
 12  * representing properties of the document, e.g. the URI or topic. The
 13  * constructor parameters are optional, all may be set later by using the proper
 14  * methods.
 15  * @deprecated Needs refactoring to match concept of current Java aitools
 16  * Document version
 17  * @param {String} id Identifier for the document.
 18  * @param {Array} features An arbitrary number of features given as array.
 19  * @param {String} plaintext This documents plaintext.
 20  * @author Christof Braeutigam christof.braeutigam@uni-weimar.de
 21  * @constructor
 22  * @class Simple representation for a real-world document
 23  */
 24 de.aitools.js.Document = function(id, features, plaintext) {
 25     
 26   var id_ = "";
 27   var features_ = []; // original: Map<String, List<Feature>>;
 28                       // TODO: besser ein Object verwenden
 29   var plaintext_ = "";
 30   
 31   if(typeof(id) !== "undefined" && id !== null) {
 32     // TODO: am besten gleich auf den typ checken, den der parameter haben soll?
 33     if(typeof(id) !== "string") { id = String(id); }
 34     CHECK_TYPE(id, "string",
 35                "type of id is not string. this shouldn't happen!");
 36     id_ = id;
 37   }
 38   
 39   if(typeof(features) !== "undefined" && features !== null) {
 40     if(typeof(features) === "object") { features_ = features; }
 41     else { throw new Error("features is not an object/array"); }
 42   }
 43   
 44   if(typeof(plaintext) !== "undefined" && plaintext !== null) {
 45     if(typeof(plaintext) === "string") { plaintext_ = plaintext; }
 46     else { throw new Error("plaintext is not a string"); }
 47   }
 48   
 49   /**
 50    * Add a feature to this document.
 51    * @param {de.aitools.js.Feature} feature The feature to add.
 52    * @returns {Number} Number of features with the same name.
 53    */
 54   this.addFeature = function(feature) {
 55     featureName = feature.getName();
 56     list = features_[featureName];
 57     if(list === undefined) {
 58       list = [];
 59       features_[featureName] = list;
 60     }
 61     list.push(feature);
 62     return list.length;
 63   };
 64   
 65   /**
 66    * Returns all features.
 67    * @returns {Array} Map containing name -> [Feature] mappings.
 68    */
 69   this.getFeatures = function() { return features_; };
 70   
 71   /**
 72    * Returns true if this document contains at least one feature with name
 73    * "featureName".
 74    * @param {String} featureName Name of the feature to check.
 75    * @returns {Boolean} True if this document contains at least one feature
 76    *                    named "featureName", otherwise false.
 77    */
 78   this.hasFeatures = function(featureName) {
 79     CHECK_NOT_NULL(featureName);
 80     if(features_[featureName] === undefined) { return false; }
 81     else { return true; }
 82   };
 83   
 84   /**
 85    * Returns all features with the given name.
 86    * @param {String} featureName Name of the feture(s) to return.
 87    * @returns {Array} Map containing name -> [{@link de.aitools.js.Feature}] mappings.
 88    */
 89   this.getFeatures = function(featureName) { return features_[featureName]; };
 90   
 91   /**
 92    * Returns the value of the attribute from the feature with the given
 93    * name and key.
 94    * @param {String} featureName Name of the feature.
 95    * @param {String} attributeKey Key of the requested attribute.
 96    * @returns {Object} Value of the requested attribute.
 97    */
 98   this.getFeatureAttributes = function(featureName, attributeKey) {
 99     CHECK_NOT_NULL(featureName);
100     CHECK_NOT_NULL(attributeKey);
101     features = this.getFeatures(featureName);
102     CHECK(features.length > 0);
103     return features[0].getAttribute(attributeKey);
104   };
105   
106   /**
107    * Returns the document id.
108    * @returns {String} Id of the document.
109    */
110   this.getId = function() { return id_; };
111   
112   /**
113    * Returns the plaintext of the document.
114    * @returns {String} Plaintext of the document.
115    */
116   this.getPlaintext = function() { return plaintext_; };
117   
118   /**
119    * Set the features of this document with the given features.
120    * @param {Array} features Features to set.
121    */
122   this.setFeatures = function(features) { features_ = features; };
123   
124   /**
125    * Set the id of this document.
126    * @param {String} id Id of the document.
127    */
128   this.setId = function(id) { id_ = id; };
129   
130   /**
131    * Set the plaintext of this document.
132    * @param {String} plaintext Plaintext of the document.
133    */
134   this.setPlaintext = function(plaintext) { plaintext_ = plaintext; };
135   
136   /**
137    * Returns the document formattet as string.
138    * @returns {String} The document formattet as string.
139    */
140   this.toString = function() {
141     s = "";
142     s += ("id: " + id_ + "\n");
143     // TODO: wo bleibt hier hasOwnProperty ???
144     // noch ist es ein array, das braucht kein hasOwnProperty...
145     for(var featureName in features_) {
146       s += ("\n" + featureName + "\n");
147       for(var feature in features_[featureName]) {
148         // TODO: wird feature.toString() vielleicht implizit aufgerufen wenn man
149         //       String(feature) aufruft?
150         s += ("  " + feature.toString() + "\n");
151       }
152     }
153     return s;
154   };
155 };
156 
157 // Document.js
158 ////////////////////////////////////////////////////////////////////////////////
159