Java tutorial
/** * POJO bean executing the Apriori Frequent Pattern algorithm on weather data * Copyright (C) 2014 Edgar H. de Graaf * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Copyright (C) 2014 Edgar H. de Graaf, edgargithub@outlook.com */ package demo.bean; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import weka.associations.Apriori; import weka.associations.AprioriItemSet; import weka.core.FastVector; import weka.core.Instances; import weka.core.converters.ConverterUtils.DataSource; public class WekaDABeanImpl implements DABean { @Override public Map<Integer, ResultItem> generateResults() { Map<Integer, ResultItem> myResults = new HashMap<Integer, ResultItem>(); try { InputStream input = getClass().getResourceAsStream("/arff/weather.nominal.arff"); DataSource source = new DataSource(input); Instances data = source.getDataSet(); Apriori apriori = new Apriori(); apriori.buildAssociations(data); FastVector res[] = apriori.getAllTheRules(); int x = 0; for (FastVector rule : res) { if (rule == null) continue; for (int i = 0; i < rule.size(); ++i) { Object o = rule.elementAt(i); if (o instanceof AprioriItemSet) { AprioriItemSet itemSet = (AprioriItemSet) o; System.out.println(itemSet.toString(data) + " " + itemSet.items().length); String ruleStr = itemSet.toString(data) + " "; myResults.put((x + 1), new ResultItem((x + 1), ruleStr)); if (x >= 10) break; x++; } } } } catch (Exception ex) { ex.printStackTrace(); } return myResults; } }