diff --git a/libsvm-dp-core/src/main/java/org/shirdrn/document/preprocessing/component/AbstractOutputtingQuantizedData.java b/libsvm-dp-core/src/main/java/org/shirdrn/document/preprocessing/component/AbstractOutputtingQuantizedData.java index 9086bd2..2438cc6 100644 --- a/libsvm-dp-core/src/main/java/org/shirdrn/document/preprocessing/component/AbstractOutputtingQuantizedData.java +++ b/libsvm-dp-core/src/main/java/org/shirdrn/document/preprocessing/component/AbstractOutputtingQuantizedData.java @@ -57,7 +57,7 @@ public void fire() { StringBuffer line = new StringBuffer(); line.append(labelId).append(" "); Entry> docsEntry = docsIter.next(); - Map terms = docsEntry.getValue(); + Map terms = sortMapByKey(docsEntry.getValue()); for(Entry termEntry : terms.entrySet()) { String word = termEntry.getKey(); Integer wordId = getWordId(word); @@ -83,6 +83,25 @@ public void fire() { LOG.info("Finished: outputVectorFile=" + context.getFDMetadata().getOutputVectorFile()); } + /** + * sort map by key + * @param oriMap original map + * @return sorted map + */ + private Map sortMapByKey(Map oriMap) { + if (oriMap == null || oriMap.isEmpty()) { + return null; + } + Map sortedMap = new TreeMap( + new Comparator() { + public int compare(String key1, String key2) { + return getWordId(key1) - getWordId(key2); + } + }); + sortedMap.putAll(oriMap); + return sortedMap; + } + private Integer getWordId(String word) { TermFeatureable term = featuredTermsMap.get(word); return term == null ? null : term.getId();