Package etc.aloe.data

Examples of etc.aloe.data.SegmentSet


        List<Message> messages = sortByTime(messageSet.getMessages());
        if (byParticipant) {
            messages = sortByParticipant(messages);
        }

        SegmentSet segments = new SegmentSet();

        Segment current = new Segment();
        long lastTime = 0;
        String lastParticipant = null;

        int numLabeled = 0;
        for (Message message : messages) {
            long msgSeconds = message.getTimestamp().getTime() / 1000;
            long diffSeconds = (msgSeconds - lastTime);

            boolean newSegment = false;
            if (lastTime > 0 && diffSeconds > thresholdSeconds) {
                newSegment = true;
            }
            if (byParticipant && lastParticipant != null && !lastParticipant.equals(message.getParticipant())) {
                newSegment = true;
            }

            if (newSegment) {
                if (this.resolution != null) {
                    current.setTrueLabel(this.resolution.resolveLabel(current));
                    if (current.hasTrueLabel()) {
                        numLabeled++;
                    }
                }
                segments.add(current);
                current = new Segment();
            }

            lastTime = msgSeconds;
            lastParticipant = message.getParticipant();
            current.add(message);
        }

        if (current.getMessages().size() > 0) {
            if (this.resolution != null) {
                current.setTrueLabel(this.resolution.resolveLabel(current));
                if (current.hasTrueLabel()) {
                    numLabeled++;
                }
            }
            segments.add(current);
        }

        System.out.println("Grouped messages into " + segments.size() + " segments (" + numLabeled + " labeled).");

        return segments;
    }
View Full Code Here


        this.falseNegativeCost = falseNegativeCost;
    }

    @Override
    public SegmentSet balance(SegmentSet segmentSet) {
        SegmentSet balanced = new SegmentSet();

        List<Segment> allSegments = segmentSet.getSegments();
        List<Segment> resultSegments = new ArrayList<Segment>();

        List<Segment> positive = new ArrayList<Segment>();
        List<Segment> negative = new ArrayList<Segment>();
        List<Segment> unlabeled = new ArrayList<Segment>();

        for (Segment segment : allSegments) {
            if (!segment.hasTrueLabel()) {
                unlabeled.add(segment);
            } else if (segment.getTrueLabel() == true) {
                positive.add(segment);
            } else if (segment.getTrueLabel() == false) {
                negative.add(segment);
            }
        }

        if (!unlabeled.isEmpty()) {
            throw new IllegalArgumentException("Data set contains " + unlabeled.size() + " unlabeled examples.");
        }

        double currentPositiveNegativeRatio = (double) positive.size() / negative.size();
        double desiredPositiveNegativeRatio = (double) falseNegativeCost / falsePositiveCost;

        //Will we be downsampling positive or negative examples?
        if (currentPositiveNegativeRatio > desiredPositiveNegativeRatio) {
            //We are removing positive examples
            resultSegments.addAll(negative);

            int desiredPositiveExamples = computeFinalExamples(negative.size(), desiredPositiveNegativeRatio);
            sampleInto(resultSegments, positive, desiredPositiveExamples);
        } else if (currentPositiveNegativeRatio < desiredPositiveNegativeRatio) {
            //We are removing negative examples
            resultSegments.addAll(positive);

            int desiredNegativeExamples = computeFinalExamples(positive.size(), 1.0 / desiredPositiveNegativeRatio);
            sampleInto(resultSegments, negative, desiredNegativeExamples);
        } else {
            //We are not doing any downsampling
            resultSegments.addAll(positive);
            resultSegments.addAll(negative);
        }

        balanced.setSegments(resultSegments);

        System.out.println("Balanced (" + positive.size() + ", " + negative.size() + ") to ("
                + balanced.getCountWithTrueLabel(true) + ", " + balanced.getCountWithTrueLabel(false) + ")");

        return balanced;
    }
View Full Code Here

public class NullSegmentation implements Segmentation {

    @Override
    public SegmentSet segment(MessageSet messages) {
        System.out.println("Applying no segmentation procedure.");
        SegmentSet segments = new SegmentSet();

        int numLabeled = 0;
        for (Message message : messages.getMessages()) {
            Segment current = new Segment();

            current.add(message);

            if (message.hasTrueLabel()) {
                current.setTrueLabel(message.getTrueLabel());
                numLabeled++;
            }

            segments.add(current);
        }

        System.out.println("Grouped messages into " + segments.size() + " segments (" + numLabeled + " labeled).");

        return segments;
    }
View Full Code Here

        this.falseNegativeCost = falseNegativeCost;
    }

    @Override
    public SegmentSet balance(SegmentSet segmentSet) {
        SegmentSet balanced = new SegmentSet();

        List<Segment> allSegments = segmentSet.getSegments();
        List<Segment> resultSegments = new ArrayList<Segment>();

        List<Segment> positive = new ArrayList<Segment>();
        List<Segment> negative = new ArrayList<Segment>();
        List<Segment> unlabeled = new ArrayList<Segment>();

        for (Segment segment : allSegments) {
            if (!segment.hasTrueLabel()) {
                unlabeled.add(segment);
            } else if (segment.getTrueLabel() == true) {
                positive.add(segment);
            } else if (segment.getTrueLabel() == false) {
                negative.add(segment);
            }
        }

        if (!unlabeled.isEmpty()) {
            throw new IllegalArgumentException("Data set contains " + unlabeled.size() + " unlabeled examples.");
        }

        double currentPositiveNegativeRatio = (double) positive.size() / negative.size();
        double desiredPositiveNegativeRatio = (double) falseNegativeCost / falsePositiveCost;

        //Will we be upsampling positive or negative examples?
        if (currentPositiveNegativeRatio < desiredPositiveNegativeRatio) {
            //We are adding positive examples
            resultSegments.addAll(negative);

            int desiredPositiveExamples = computeFinalExamples(negative.size(), desiredPositiveNegativeRatio);
            sampleInto(resultSegments, positive, desiredPositiveExamples);
        } else if (currentPositiveNegativeRatio > desiredPositiveNegativeRatio) {
            //We are adding negative examples
            resultSegments.addAll(positive);

            int desiredNegativeExamples = computeFinalExamples(positive.size(), 1.0 / desiredPositiveNegativeRatio);
            sampleInto(resultSegments, negative, desiredNegativeExamples);
        } else {
            //We are not doing any downsampling
            resultSegments.addAll(positive);
            resultSegments.addAll(negative);
        }
        balanced.setSegments(resultSegments);

        System.out.println("Balanced (" + positive.size() + ", " + negative.size() + ") to (" + balanced.getCountWithTrueLabel(true) + ", " + balanced.getCountWithTrueLabel(false) + ")");

        return balanced;
    }
View Full Code Here

        messages.add(new Message(5, new Date(now + 2 * minute), "Alice", "upon"));
        messages.add(new Message(6, new Date(now + 3 * minute), "Alice", "a"));
        messages.add(new Message(7, new Date(now + 3 * minute + second), "Alice", "time"));

        ThresholdSegmentation instance = new ThresholdSegmentation(30, true);
        SegmentSet segments = instance.segment(messages);

        //Expecting 5 segments
        assertEquals(5, segments.size());

        Segment seg0 = segments.get(0);
        Segment seg1 = segments.get(1);
        Segment seg2 = segments.get(2);
        Segment seg3 = segments.get(3);
        Segment seg4 = segments.get(4);

        assertEquals(1, seg0.getMessages().size()); //Alice
        assertEquals(1, seg1.getMessages().size()); //Alice
        assertEquals(2, seg2.getMessages().size()); //Alice, Alice
        assertEquals(2, seg3.getMessages().size()); //Bob, Bob
View Full Code Here

        messages.add(new Message(5, new Date(now + 2 * minute), "Alice", "upon"));
        messages.add(new Message(6, new Date(now + 3 * minute), "Bob", "a"));
        messages.add(new Message(7, new Date(now + 3 * minute + second), "Alice", "time"));

        ThresholdSegmentation instance = new ThresholdSegmentation(30, false);
        SegmentSet segments = instance.segment(messages);

        //Expecting 4 segments
        assertEquals(4, segments.size());

        Segment seg0 = segments.get(0);
        Segment seg1 = segments.get(1);
        Segment seg2 = segments.get(2);
        Segment seg3 = segments.get(3);

        assertEquals(3, seg0.getMessages().size());
        assertEquals(2, seg1.getMessages().size());
        assertEquals(1, seg2.getMessages().size());
        assertEquals(2, seg3.getMessages().size());
View Full Code Here

        messages.add(new Message(5, new Date(), "Alice", "upon"));
        messages.add(new Message(6, new Date(), "Bob", "a"));
        messages.add(new Message(7, new Date(), "Alice", "time"));

        ThresholdSegmentation instance = new ThresholdSegmentation(30, true);
        SegmentSet segments = instance.segment(messages);

        //Expecting 2 segments, one for each participant
        assertEquals(2, segments.size());

        Segment seg0 = segments.get(0);
        Segment seg1 = segments.get(1);

        assertEquals(4, seg0.getMessages().size());
        assertEquals(4, seg1.getMessages().size());

        String participant0 = seg0.getMessages().get(0).getParticipant();
View Full Code Here

        messages.add(new Message(5, new Date(), "Alice", "upon"));
        messages.add(new Message(6, new Date(), "Bob", "a"));
        messages.add(new Message(7, new Date(), "Alice", "time"));

        ThresholdSegmentation instance = new ThresholdSegmentation(30, false);
        SegmentSet segments = instance.segment(messages);

        //Expecting 1 segment
        assertEquals(1, segments.size());

        Segment seg0 = segments.get(0);

        assertEquals(8, seg0.getMessages().size());

        for (int i = 0; i < seg0.getMessages().size(); i++) {
            Message message = seg0.getMessages().get(i);
View Full Code Here

TOP

Related Classes of etc.aloe.data.SegmentSet

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.