Java tutorial
/** * This software is licensed under the Apache License, version 2 ("ALv2"), quoted below. * * Copyright 2016 Gianluca Tomasino <http://www.gianlucatomasino.me> * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package io.learningbox.controller; import io.learningbox.model.LearningSet; import io.learningbox.service.LearningSetRepository; import opennlp.tools.doccat.DoccatFactory; import opennlp.tools.doccat.DoccatModel; import opennlp.tools.doccat.DocumentCategorizerME; import opennlp.tools.doccat.DocumentSample; import opennlp.tools.util.ObjectStream; import opennlp.tools.util.TrainingParameters; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.SortedMap; @RestController @RequestMapping("/api") public class APIController { @Autowired private LearningSetRepository repository; @RequestMapping(value = "/categorize/{area}", method = RequestMethod.POST) public SortedMap<Double, Set<String>> categorize(@PathVariable final String area, @RequestBody String input) throws IOException { List<LearningSet> l = repository.findByArea(area); final Iterator<LearningSet> sets = l.iterator(); ObjectStream<DocumentSample> stream = new ObjectStream<DocumentSample>() { @Override public DocumentSample read() throws IOException { if (sets.hasNext()) { LearningSet s = sets.next(); return new DocumentSample(s.getCategory(), s.getText()); } return null; } @Override public void reset() throws IOException, UnsupportedOperationException { throw new UnsupportedOperationException(); } @Override public void close() throws IOException { //Do nothing } }; TrainingParameters trainingParameters = TrainingParameters.defaultParams(); trainingParameters.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(1000)); trainingParameters.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(1)); DoccatModel model = DocumentCategorizerME.train("en", stream, trainingParameters, new DoccatFactory()); DocumentCategorizerME myCategorizer = new DocumentCategorizerME(model); return myCategorizer.sortedScoreMap(input); } }