Format the document. Does not affect/alter doc.
(InputDocument doc, boolean collectAnalysis)
| 121 | |
| 122 | /** Format the document. Does not affect/alter doc. */ |
| 123 | public String format(InputDocument doc, boolean collectAnalysis) throws Exception { |
| 124 | if ( testDoc!=null ) throw new IllegalArgumentException("can't call format > once"); |
| 125 | // for debugging we need a map from original token with actual line:col to tree node. used by token analysis |
| 126 | originalDoc = doc; |
| 127 | originalTokenToNodeMap = indexTree(doc.tree); |
| 128 | originalTokens = doc.tokens; |
| 129 | |
| 130 | this.testDoc = InputDocument.dup(doc); // make copy of doc, getting new tokens, tree |
| 131 | output = new StringBuilder(); |
| 132 | this.realTokens = getRealTokens(testDoc.tokens); |
| 133 | // squeeze out ws and kill any line/col info so we can't use ground truth by mistake |
| 134 | wipeCharPositionInfoAndWhitespaceTokens(testDoc.tokens); // all except for first token |
| 135 | wsClassifier = new kNNClassifier(corpus, wsFeatures, corpus.injectWhitespace); |
| 136 | hposClassifier = new kNNClassifier(corpus, hposFeatures, corpus.hpos); |
| 137 | |
| 138 | analysis = new Vector<>(testDoc.tokens.size()); |
| 139 | analysis.setSize(testDoc.tokens.size()); |
| 140 | |
| 141 | // make an index on the duplicated doc tree with tokens missing line:col info |
| 142 | if ( tokenToNodeMap == null ) { |
| 143 | tokenToNodeMap = indexTree(testDoc.tree); |
| 144 | } |
| 145 | |
| 146 | WritableToken firstToken = (WritableToken)testDoc.tokens.getNextRealToken(-1); |
| 147 | |
| 148 | String prefix = originalTokens.getText(Interval.of(0, firstToken.getTokenIndex())); // gets any comments in front + first real token |
| 149 | charPosInLine = firstToken.getCharPositionInLine()+firstToken.getText().length()+1; // start where first token left off |
| 150 | line = Tool.count(prefix, '\n') + 1; |
| 151 | output.append(prefix); |
| 152 | |
| 153 | // first identify oversize lists with separators |
| 154 | IdentifyOversizeLists splitter = new IdentifyOversizeLists(corpus, testDoc.tokens, tokenToNodeMap); |
| 155 | ParseTreeWalker.DEFAULT.walk(splitter, testDoc.tree); |
| 156 | tokenToListInfo = splitter.tokenToListInfo; |
| 157 | |
| 158 | realTokens = getRealTokens(testDoc.tokens); |
| 159 | for (int i = Trainer.ANALYSIS_START_TOKEN_INDEX; i<realTokens.size(); i++) { // can't process first token |
| 160 | int tokenIndexInStream = realTokens.get(i).getTokenIndex(); |
| 161 | processToken(i, tokenIndexInStream, collectAnalysis); |
| 162 | } |
| 163 | |
| 164 | releaseMemory(); |
| 165 | |
| 166 | return output.toString(); |
| 167 | } |
| 168 | |
| 169 | public float getWSEditDistance() throws Exception { |
| 170 | List<Token> wsTokens = filter(originalTokens.getTokens(), |
no test coverage detected