diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c41cc9e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6f91320 --- /dev/null +++ b/pom.xml @@ -0,0 +1,55 @@ + + 4.0.0 + + ztbd + IWI + 1.0-SNAPSHOT + jar + + IWI + http://maven.apache.org + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + + + + + src/main/resources + true + + + + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + org.swinglabs + swing-layout + 1.0.3 + + + commons-io + commons-io + 2.4 + + + + diff --git a/src/main/java/ztbd/iwi/App.java b/src/main/java/ztbd/iwi/App.java new file mode 100644 index 0000000..f90a447 --- /dev/null +++ b/src/main/java/ztbd/iwi/App.java @@ -0,0 +1,51 @@ +package ztbd.iwi; + +import java.util.HashMap; +import java.util.Scanner; + +/** + * Hello world! + * + */ +public class App { + + public static void main(String[] args) { + try { + NaiveClassifier nc; + try (Scanner scan = new Scanner(Thread.currentThread().getContextClassLoader().getResourceAsStream("input2_test.tab"))) { + int noOfOutputClasses = scan.nextInt(); + HashMap hm = new HashMap<>(); + while (hm.size() < noOfOutputClasses) { + hm.put(scan.next(), scan.next()); + } + int noOfLines = scan.nextInt(); + int noOfIntInLine = scan.nextInt(); + int noOfAttrInLine = noOfIntInLine - 1; + String expectedValue = null; + Integer[] buffor = new Integer[noOfAttrInLine]; + nc = new NaiveClassifier(noOfAttrInLine, hm); + while (noOfLines-- > 0) { + for (int i = 0; i < noOfAttrInLine; i++) { + buffor[i] = scan.nextInt(); + } + expectedValue = scan.next(); + + for (int i = 0; i < noOfAttrInLine; i++) { + nc.getAttributeForIndex(i).incrementValue(new NBAttributeValue(buffor[i]), nc.getOutputValueForString(expectedValue)); + } + } + } + + //nc.getVectorProbabilityForAnswer(NBVector nbVec, NBOutputValue ov); + //nc.klasifyVector(); + System.out.println("Probability of getting answer 1 for R-1 classification with attribute number 1 of value 0"); + System.out.println(nc.getAttributeForIndex(1).getProbablity(new NBAttributeValue(0), nc.getOutputValueForString("1"))); + + System.out.println("Probability of getting answer 1 for R-1 classification with attribute number 2 of value 3"); + System.out.println(nc.getAttributeForIndex(2).getProbablity(new NBAttributeValue(3), nc.getOutputValueForString("1"))); + + } catch (Exception e) { + e.printStackTrace(System.err); + } + } +} diff --git a/src/main/java/ztbd/iwi/MainPanel.form b/src/main/java/ztbd/iwi/MainPanel.form new file mode 100644 index 0000000..dd9bf1f --- /dev/null +++ b/src/main/java/ztbd/iwi/MainPanel.form @@ -0,0 +1,17 @@ + + +
+ + + + + + + + + + + + + +
diff --git a/src/main/java/ztbd/iwi/MainPanel.java b/src/main/java/ztbd/iwi/MainPanel.java new file mode 100644 index 0000000..fc0465e --- /dev/null +++ b/src/main/java/ztbd/iwi/MainPanel.java @@ -0,0 +1,43 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package ztbd.iwi; + +/** + * + * @author jjroman + */ +public class MainPanel extends javax.swing.JPanel { + + /** + * Creates new form MainPanel + */ + public MainPanel() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 400, Short.MAX_VALUE) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 300, Short.MAX_VALUE) + ); + } + // //GEN-END:initComponents + // Variables declaration - do not modify//GEN-BEGIN:variables + // End of variables declaration//GEN-END:variables +} diff --git a/src/main/java/ztbd/iwi/NBAttribute.java b/src/main/java/ztbd/iwi/NBAttribute.java new file mode 100644 index 0000000..abddf16 --- /dev/null +++ b/src/main/java/ztbd/iwi/NBAttribute.java @@ -0,0 +1,54 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package ztbd.iwi; + +import java.util.HashMap; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * + * @author jjroman + */ +class NBAttribute { + private int summarisedNoOfEntities = 0; + HashMap > probabilityMap = new HashMap<>(); + + /** + * Metoda używana do inkrementalnego uczenia + * @param attrVal + * @param outVal + */ + public void incrementValue(NBAttributeValue attrVal, NBOutputValue outVal) { + // sprawdzenie istnienia + if(probabilityMap.containsKey(attrVal)){ + if(probabilityMap.get(attrVal).containsKey(outVal)){ + probabilityMap.get(attrVal).get(outVal).incrementAndGet(); + }else{ + probabilityMap.get(attrVal).put(outVal, new AtomicInteger(1)); + } + }else{ + HashMap hm = new HashMap<>(); + hm.put(outVal, new AtomicInteger(1)); + probabilityMap.put(attrVal,hm); + } + summarisedNoOfEntities++; + } + + public double getProbablity(NBAttributeValue attrVal, NBOutputValue outVal){ + if(summarisedNoOfEntities > 0 + && probabilityMap.containsKey(attrVal) + && probabilityMap.get(attrVal).containsKey(outVal) + ){ + + return ((double)(probabilityMap.get(attrVal).get(outVal).get() ))/((double) probabilityMap.get(attrVal).size()); + } + + return almostZero(); + } + + private double almostZero(){ + return 1.0 / (summarisedNoOfEntities+1); + } +} diff --git a/src/main/java/ztbd/iwi/NBAttributeValue.java b/src/main/java/ztbd/iwi/NBAttributeValue.java new file mode 100644 index 0000000..a7763dc --- /dev/null +++ b/src/main/java/ztbd/iwi/NBAttributeValue.java @@ -0,0 +1,40 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package ztbd.iwi; + +/** + * + * @author jjroman + */ +class NBAttributeValue implements Comparable { + + private int value; + public NBAttributeValue(int value ) { + this.value = value; + } + + public int getValue(){ + return value; + } + + @Override + public boolean equals(Object o){ + return (o instanceof NBAttributeValue) && ((NBAttributeValue)o).getValue() == this.getValue(); + } + + @Override + public int hashCode() { + int hash = 3; + hash = 53 * hash + this.value; + return hash; + } + @Override + public int compareTo(NBAttributeValue o) { + return (this.getValue() - o.getValue()); + } + + + +} diff --git a/src/main/java/ztbd/iwi/NBOutputValue.java b/src/main/java/ztbd/iwi/NBOutputValue.java new file mode 100644 index 0000000..4ed7bee --- /dev/null +++ b/src/main/java/ztbd/iwi/NBOutputValue.java @@ -0,0 +1,38 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package ztbd.iwi; + +import java.util.Objects; + +/** + * + * @author jjroman + */ +class NBOutputValue { + int key = 0; + String name = null; + + public NBOutputValue(String key, String name) { + this(Integer.parseInt(key), name); + } + + public NBOutputValue(int key, String name) { + this.key = key; + this.name = name; + } + + @Override + public boolean equals(Object o){ + return (o instanceof NBOutputValue) && ((NBAttributeValue)o).hashCode() == this.hashCode(); + } + + @Override + public int hashCode() { + int hash = 3; + hash = 19 * hash + this.key; + hash = 19 * hash + Objects.hashCode(this.name); + return hash; + } +} diff --git a/src/main/java/ztbd/iwi/NaiveClassifier.java b/src/main/java/ztbd/iwi/NaiveClassifier.java new file mode 100644 index 0000000..8bdfe79 --- /dev/null +++ b/src/main/java/ztbd/iwi/NaiveClassifier.java @@ -0,0 +1,58 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package ztbd.iwi; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author jjroman + */ +public class NaiveClassifier { + + private NBAttribute[] attributes = null; + private HashMap ovMap = null; + + /** + * + * @param noOfAttributes + * @param ovMap + */ + public NaiveClassifier(int noOfAttributes, Map ovMap) { + initAttributes(noOfAttributes); + initOutputValues(ovMap); + } + + + + private void initAttributes(int noOfAttributes) { + this.attributes = new NBAttribute[noOfAttributes]; + for (int i = 0; i < noOfAttributes; i++) { + this.attributes[i] = new NBAttribute(); + } + } + + private void initOutputValues(Map ovMap) { + this.ovMap = new HashMap<>(); + + for(Map.Entry me : ovMap.entrySet()){ + this.ovMap.put(me.getKey(), new NBOutputValue(me.getKey(), me.getValue())); + } + } + + public NBAttribute getAttributeForIndex(int index) { + return this.attributes[index]; + } + + public NBOutputValue getOutputValueForString(String ov) { + return this.ovMap.get(ov); + } + + public void train(NBAttribute attr, NBAttributeValue attrVal, NBOutputValue outVal) { + attr.incrementValue(attrVal, outVal); + } + +} diff --git a/src/main/resources/input2_test.tab b/src/main/resources/input2_test.tab new file mode 100644 index 0000000..6a533bb --- /dev/null +++ b/src/main/resources/input2_test.tab @@ -0,0 +1,9 @@ +2 +1 green +2 red +5 5 +1 1 2 0 0 +0 2 1 0 1 +0 1 3 0 1 +1 1 3 1 0 +1 2 1 1 0 \ No newline at end of file diff --git a/src/test/java/ztbd/iwi/AppTest.java b/src/test/java/ztbd/iwi/AppTest.java new file mode 100644 index 0000000..0b60e8d --- /dev/null +++ b/src/test/java/ztbd/iwi/AppTest.java @@ -0,0 +1,38 @@ +package ztbd.iwi; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}