All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.expleague.commons.seq.regexp.converters.PatternVecConverter Maven / Gradle / Ivy

Go to download

Utilities including math, charsequence based text processing, sequences etc.

There is a newer version: 1.4.9
Show newest version
package com.expleague.commons.seq.regexp.converters;


import com.expleague.commons.func.Converter;
import com.expleague.commons.math.vectors.Vec;
import com.expleague.commons.math.vectors.impl.vectors.ArrayVec;
import com.expleague.commons.seq.regexp.Alphabet;
import com.expleague.commons.seq.regexp.Pattern;

/**
 * User: Manokk
 * Date: 10.09.11
 * Time: 15:18
 */
public class PatternVecConverter implements Converter, Vec> {
  private final Alphabet alphabet;

  public PatternVecConverter(final Alphabet a) {
    this.alphabet = a;
  }

  @Override
  public Pattern convertFrom(final Vec vec) {
    final int size = vec.dim();
    final Pattern p = new Pattern(alphabet);
    for (int i = 0; i < size; i+=2) {
      final int code = (int) vec.get(i);
      final int mod = (int) vec.get(i + 1);
      p.add(alphabet.condition(code), Pattern.Modifier.values()[mod]);
    }
    return p;
  }

  @Override
  public Vec convertTo(final Pattern pattern) {
    final int size = pattern.size();
    final double[] patternVec = new double[2 * size];
    for (int i = 0, j = 0; i < size; i++, j+=2) {
      patternVec[j] = alphabet.indexCondition(pattern.condition(i));
      patternVec[j + 1] = pattern.modifier(i).ordinal();
    }
    return new ArrayVec(patternVec);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy