To be called by the #initialLoad() method. It will take in the text and add a new document vector to the data set. Once all text documents have been loaded, this method should never be called again. This method is thread safe. @param text the text of the document to add @return t
(String text)
| 141 | * zero and counts up. |
| 142 | */ |
| 143 | protected int addOriginalDocument(String text) |
| 144 | { |
| 145 | if(noMoreAdding) |
| 146 | throw new RuntimeException("Initial data set has been finalized"); |
| 147 | StringBuilder localWorkSpace = workSpace.get(); |
| 148 | List<String> localStorageSpace = storageSpace.get(); |
| 149 | Map<String, Integer> localWordCounts = wordCounts.get(); |
| 150 | if(localWorkSpace == null) |
| 151 | { |
| 152 | localWorkSpace = new StringBuilder(); |
| 153 | localStorageSpace = new ArrayList<String>(); |
| 154 | localWordCounts = new LinkedHashMap<String, Integer>(); |
| 155 | workSpace.set(localWorkSpace); |
| 156 | storageSpace.set(localStorageSpace); |
| 157 | wordCounts.set(localWordCounts); |
| 158 | } |
| 159 | |
| 160 | localWorkSpace.setLength(0); |
| 161 | localStorageSpace.clear(); |
| 162 | localWordCounts.clear(); |
| 163 | |
| 164 | tokenizer.tokenize(text, localWorkSpace, localStorageSpace); |
| 165 | |
| 166 | for(String word : localStorageSpace) |
| 167 | { |
| 168 | Integer count = localWordCounts.get(word); |
| 169 | if(count == null) |
| 170 | localWordCounts.put(word, 1); |
| 171 | else |
| 172 | localWordCounts.put(word, count+1); |
| 173 | } |
| 174 | |
| 175 | SparseVector vec = new SparseVector(currentLength.get()+1, localWordCounts.size());//+1 to avoid issues when its length is zero, will be corrected in finalization step anyway |
| 176 | for(Iterator<Map.Entry<String, Integer>> iter = localWordCounts.entrySet().iterator(); iter.hasNext();) |
| 177 | { |
| 178 | Map.Entry<String, Integer> entry = iter.next(); |
| 179 | String word = entry.getKey(); |
| 180 | |
| 181 | int ms_to_sleep = 1; |
| 182 | while(!addWord(word, vec, entry.getValue()))//try in a loop, expoential back off |
| 183 | { |
| 184 | try |
| 185 | { |
| 186 | Thread.sleep(ms_to_sleep); |
| 187 | ms_to_sleep = Math.min(100, ms_to_sleep*2); |
| 188 | } |
| 189 | catch (InterruptedException ex) |
| 190 | { |
| 191 | Logger.getLogger(TextDataLoader.class.getName()).log(Level.SEVERE, null, ex); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | localWordCounts.clear(); |
| 196 | |
| 197 | synchronized(vectors) |
| 198 | { |
| 199 | vectors.add(vec); |
| 200 | return documents++; |
nothing calls this directly
no test coverage detected