Skip to content

Commit

Permalink
Segmentator added
Browse files Browse the repository at this point in the history
  • Loading branch information
DDred committed Oct 10, 2011
1 parent d36a92f commit f5a16f7
Show file tree
Hide file tree
Showing 40 changed files with 556 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import libiada.FastChainAlgorithms.FastChain.Calculators.FastCalculatorFactory;
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.FastChainAlgorithms.FastChain.Calculators.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import libiada.FastChainAlgorithms.FastChain.FastAlphabet;
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.FastChainAlgorithms.FastChain.UtilClasses.ChainAlgebra.FastChainAlgebra;
import libiada.IntervalAnalysis.LinkUp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import libiada.FastChainAlgorithms.FastChain.FastAlphabet;
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.FastChainAlgorithms.FastChain.UtilClasses.ChainAlgebra.FastChainAlgebra;
import libiada.IntervalAnalysis.LinkUp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package libiada.FastChainAlgorithms.FastChain.Calculators;

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.IntervalAnalysis.LinkUp;

/**
* Created by IntelliJ IDEA.
* User: Alex
* Date: 10.10.11
* Time: 2:03
*/
public class FastAverageWordLength extends FastCalculatorBase {
@Override
public double getValue(FastChain chain, LinkUp linkUp) throws Exception {
double result = 0;
for (int i = 0; i < chain.length(); i++) {
result += chain.get(i).length();
}
return result / chain.length();
}

@Override
public double getValue(FastUniformChain chain, LinkUp linkUp) throws Exception {
double result = 0;
for (int i = 0; i < chain.length(); i++) {
if (chain.get(i) == "-")
continue;
result += chain.get(i).length();
}
return result / chain.length();
}

@Override
public String getName() {
return "L_avegare" + super.getName();
}

@Override
public String getType() {
return "double";
}

@Override
public String getGroup() {
return "Common";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package libiada.FastChainAlgorithms.FastChain.Interfaces;
package libiada.FastChainAlgorithms.FastChain.Calculators;

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package libiada.FastChainAlgorithms.FastChain.Calculators;

import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;

import java.util.HashSet;

/**
Expand Down Expand Up @@ -74,4 +72,8 @@ public static FastCalculatorBase getBinaryAverageRemoteness(String sym1, String
public static FastCalculatorBase getPositionedAverageRemoteness(String event, HashSet<Integer> poses, int period) {
return new FastPositionedAverageRemoteness(event, poses, period);
}

public static FastCalculatorBase getAverageWordLength() {
return new FastAverageWordLength();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastIntervalsChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastPeriodicChainConverter;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastPeriodicChainConverter;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastPeriodicChainConverter;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastChainBase;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.FastChainAlgorithms.FastChain.UtilClasses.ChainToTripleConverter;
import libiada.IntervalAnalysis.LinkUp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.FastChainAlgorithms.FastChain.FastIntervalsChain;
import libiada.FastChainAlgorithms.FastChain.FastUniformChain;
import libiada.FastChainAlgorithms.FastChain.Interfaces.FastCalculatorBase;
import libiada.IntervalAnalysis.LinkUp;

import java.util.Map;
Expand Down
22 changes: 20 additions & 2 deletions src/libiada/FastChainAlgorithms/FastChain/FastChainBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ public FastChainBase(int length) throws Exception {

@Override
public String toString() {
if (events.size() == 0)
return "";
String result = "";
for (Integer cur : events) {
result += alphabet.get(cur);
for (int i = 0; i < events.size()-1; i++) {
result += alphabet.get(events.get(i)) + ", ";
}
result += alphabet.get(events.get(events.size()-1));
return result;
}

Expand Down Expand Up @@ -108,4 +111,19 @@ public String getBuilding() {
}
return result;
}

@Override
public boolean equals(Object obj) {
if (obj.getClass() != this.getClass())
return false;
FastChainBase ch = (FastChainBase)obj;
if (alphabet.size() != ch.alphabetPower())
return false;
if (length() != ch.length())
return false;
for (int i = 0; i < this.length(); i++)
if (!get(i).equalsIgnoreCase(ch.get(i)))
return false;
return true;
}
}
32 changes: 32 additions & 0 deletions src/libiada/Segmentation/Criteria/Criteria.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package libiada.Segmentation.Criteria;

import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.Segmentation.SegmentationModel;
import libiada.Segmentation.SimpleSegmentator;

import java.util.ArrayList;

/**
* Created by IntelliJ IDEA.
* User: Alex
* Date: 09.10.11
* Time: 17:09
*/
public abstract class Criteria {
private SegmentationModel bestModel = null;

public SegmentationModel getBest(FastChain chain, ArrayList<SegmentationModel> models) throws Exception {
for (int i = 0; i < models.size(); i++) {
SimpleSegmentator segmentator = new SimpleSegmentator();
FastChain current = segmentator.segment(chain, models.get(i));
if (isBetter(current, chain))
bestModel = models.get(i);
}
clear();
return bestModel;
}

protected abstract void clear();

protected abstract boolean isBetter(FastChain segmentedChain, FastChain rootChain) throws Exception;
}
35 changes: 35 additions & 0 deletions src/libiada/Segmentation/Criteria/MaxG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package libiada.Segmentation.Criteria;

import libiada.FastChainAlgorithms.FastChain.Calculators.FastCalculatorFactory;
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.IntervalAnalysis.LinkUp;

/**
* Created by IntelliJ IDEA.
* User: Alex
* Date: 09.10.11
* Time: 17:07
*/
public class MaxG extends Criteria {
private LinkUp linkUp;
private double maxG = 0;

public MaxG(LinkUp linkUp) {
this.linkUp = linkUp;
}

@Override
protected void clear() {
maxG = 0;
}

@Override
protected boolean isBetter(FastChain segmentedChain, FastChain rootChain) throws Exception {
double g = FastCalculatorFactory.getAverageRemoteness().getValue(segmentedChain, linkUp);
if (g >= maxG) {
maxG = g;
return true;
}
return false;
}
}
40 changes: 40 additions & 0 deletions src/libiada/Segmentation/Criteria/ShenonFenoCriteria.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package libiada.Segmentation.Criteria;

import libiada.FastChainAlgorithms.FastChain.Calculators.FastCalculatorFactory;
import libiada.FastChainAlgorithms.FastChain.FastChain;
import libiada.IntervalAnalysis.LinkUp;

/**
* Created by IntelliJ IDEA.
* User: Alex
* Date: 10.10.11
* Time: 1:55
*/
public class ShenonFenoCriteria extends Criteria {
private LinkUp linkUp;
private double minValue = Double.MAX_VALUE;
private double epsilon = 0;

public ShenonFenoCriteria(LinkUp linkUp, double epsilon) {
this.linkUp = linkUp;
this.epsilon = epsilon;
}

@Override
protected void clear() {
minValue = Double.MAX_VALUE;
}

@Override
protected boolean isBetter(FastChain segmentedChain, FastChain rootChain) throws Exception {
double g_M = FastCalculatorFactory.getAverageRemoteness().getValue(segmentedChain, linkUp);
double g_m = FastCalculatorFactory.getAverageRemoteness().getValue(rootChain, linkUp);
double L_av = FastCalculatorFactory.getAverageWordLength().getValue(segmentedChain, linkUp);
double value = Math.abs((segmentedChain.length() * g_M) / (rootChain.length() * g_m) - L_av) - epsilon;
if (value < minValue) {
minValue = value;
return true;
}
return false;
}
}
30 changes: 30 additions & 0 deletions src/libiada/Segmentation/Cutters/ChainCutter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package libiada.Segmentation.Cutters;

import libiada.FastChainAlgorithms.FastChain.FastChain;

import java.util.ArrayList;

/**
* Created by IntelliJ IDEA.
* User: Alex
* Date: 09.10.11
* Time: 15:43
*/
public class ChainCutter {
public ArrayList<FastChain> cut(FastChain chain, int leftChainLength) throws Exception {
if (leftChainLength > chain.length())
throw new IndexOutOfBoundsException("Граница обрезания за пределами длинны строки");
ArrayList<FastChain> result = new ArrayList<FastChain>();
FastChain left = new FastChain();
for (int i = 0; i < leftChainLength; i++) {
left.add(chain.get(i));
}
FastChain right = new FastChain();
for (int i = leftChainLength; i < chain.length(); i++) {
right.add(chain.get(i));
}
result.add(left);
result.add(right);
return result;
}
}
Loading

0 comments on commit f5a16f7

Please sign in to comment.