<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package representation;

import be.ac.ulg.montefiore.run.jahmm.Hmm;
import be.ac.ulg.montefiore.run.jahmm.ObservationDiscrete;
import be.ac.ulg.montefiore.run.jahmm.OpdfDiscrete;
import be.ac.ulg.montefiore.run.jahmm.OpdfDiscreteFactory;
import be.ac.ulg.montefiore.run.jahmm.learn.BaumWelchLearner;
import entities.HMMFeatureVector;
import entities.HMMSequence.Packet;
import java.util.List;
import entities.HMMSequence;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


/**
 *
 * @author nikos
 */
public class HmmHandler implements GenomicSequenceRepresentationHandler<List<ObservationDiscrete<HMMSequence.Packet>>>{
    
    protected final Map<String, Hmm> classModel;

    public HmmHandler() {
        this.classModel = new HashMap<>();
    }

    @Override
    public void train(List<List<ObservationDiscrete<Packet>>> representation, String label) {
        
        Hmm hmmTemp = initializeHMM();
        
        // Train the model based on the observations
        BaumWelchLearner bwl = new BaumWelchLearner();
        
        hmmTemp = bwl.learn(hmmTemp, representation);
        
        classModel.put(label, hmmTemp);
    }

    @Override
    public Map<String, Hmm> getClassModel() {
        return classModel;
    }

    protected Hmm initializeHMM() {
        
        Hmm<ObservationDiscrete<HMMSequence.Packet>> hmm =
                new Hmm<>(2, 
                new OpdfDiscreteFactory<>(HMMSequence.Packet.class));
        
        // Random initialization (with fixed seed to allow for reproducibility)
        Random r = new Random(1L);<commit_msg>Update HmmHandler.java<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package representation;

import be.ac.ulg.montefiore.run.jahmm.Hmm;
import be.ac.ulg.montefiore.run.jahmm.ObservationDiscrete;
import be.ac.ulg.montefiore.run.jahmm.OpdfDiscrete;
import be.ac.ulg.montefiore.run.jahmm.OpdfDiscreteFactory;
import be.ac.ulg.montefiore.run.jahmm.draw.GenericHmmDrawerDot;
import be.ac.ulg.montefiore.run.jahmm.learn.BaumWelchLearner;
import entities.HMMFeatureVector;
import entities.HMMSequence.Packet;
import java.util.List;
import entities.HMMSequence;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author nikos
 */
public class HmmHandler implements GenomicSequenceRepresentationHandler<List<ObservationDiscrete<HMMSequence.Packet>>>{
    
    protected final Map<String, Hmm> classModel;

    public HmmHandler() {
        this.classModel = new HashMap<>();
    }

    @Override
    public void train(List<List<ObservationDiscrete<Packet>>> representation, String label) {
        
        Hmm hmmTemp = initializeHMM();
        
        // Train the model based on the observations
        BaumWelchLearner bwl = new BaumWelchLearner();
        
        hmmTemp = bwl.learn(hmmTemp, representation);
        
        try {
            (new GenericHmmDrawerDot()).write(hmmTemp, "learntHmm.dot");
        } catch (IOException ex) {
            Logger.getLogger(HmmHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        classModel.put(label, hmmTemp);
    }

    @Override
    public Map<String, Hmm> getClassModel() {
        return classModel;
    }

    protected Hmm initializeHMM() {
        
        Hmm<ObservationDiscrete<HMMSequence.Packet>> hmm =
                new Hmm<>(2, 
                new OpdfDiscreteFactory<>(HMMSequence.Packet.class));
        
        // Random initialization (with fixed seed to allow for reproducibility)
        Random r = new Random(1L);
{'fraction_non_alphanumeric': 0.09962453066332916, 'fraction_numerical': 0.0010012515644555694, 'mean_word_length': 3.111111111111111, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '801657', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1191, 'n_words': 268}