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

org.github.evenjn.guess.benchmark.NoiseMapleTrainer Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
/**
 *
 * Copyright 2016 Marco Trevisan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.github.evenjn.guess.benchmark;

import java.util.Random;
import java.util.Vector;
import java.util.function.Function;

import org.github.evenjn.guess.Trainer;
import org.github.evenjn.knit.KnittingCursable;
import org.github.evenjn.knit.KnittingTuple;
import org.github.evenjn.yarn.Cursable;
import org.github.evenjn.yarn.Di;
import org.github.evenjn.yarn.ProgressSpawner;
import org.github.evenjn.yarn.Tuple;

public class NoiseMapleTrainer implements
		Trainer, Tuple> {

	private final int below_size;

	private final Vector below = new Vector<>( );

	public NoiseMapleTrainer(
			Cursable above_alphabet,
			Cursable below_alphabet) {
		KnittingCursable.wrap( below_alphabet ).collect( below );
		below_size = below.size( );
	}

	@Override
	public Function, Tuple> train(
			ProgressSpawner progress,
			Cursable, Tuple>> data ) {
		final Random r = new Random( 1l );
		return new Function, Tuple>( ) {

			@Override
			public Tuple apply(
					Tuple t ) {
				Vector v = new Vector<>( );
				for ( int i = 0; i < t.size( ); i++ ) {
					int index = r.nextInt( below_size );
					v.add( below.get( index ) );
				}
				return KnittingTuple.wrap( v );
			}
		};
	}

}