
com.metreeca.text.Model Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metreeca-text Show documentation
Show all versions of metreeca-text Show documentation
A lightweight NLP framework.
The newest version!
/*
* Copyright © 2013-2022 Metreeca srl
*
* 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 com.metreeca.text;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import static java.lang.Math.max;
public final class Model {
public static Model dot() {
return new Model<>(values -> values.isEmpty() ? -1 : 1);
}
public static Model test(final Predicate predicate) {
if ( predicate == null ) {
throw new NullPointerException("null predicate");
}
return new Model<>(values -> !values.isEmpty() && predicate.test(values.get(0)) ? 1 : -1);
}
@SafeVarargs public static Model seq(final Model... models) {
if ( models == null || Arrays.stream(models).anyMatch(Objects::isNull) ) {
throw new NullPointerException("null models");
}
return new Model<>(values -> {
int i=0;
for (final Model model : models) {
final int j=model.find(values.subList(i, values.size()));
if ( j >= 0 ) {
i+=j;
} else {
return -1;
}
}
return i;
});
}
@SafeVarargs public static Model alt(final Model... models) {
if ( models == null || Arrays.stream(models).anyMatch(Objects::isNull) ) {
throw new NullPointerException("null models");
}
return new Model<>(values -> Arrays
.stream(models)
.mapToInt(model -> model.find(values))
.max()
.orElse(-1)
);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private final ToIntFunction super List> specs;
private Model(final ToIntFunction super List> specs) { this.specs=specs; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public int find(final List values) {
if ( values == null ) {
throw new NullPointerException("null values");
}
return specs.applyAsInt(values);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Model opt() {
return new Model<>(values -> max(0, find(values)));
}
public Model star() {
return new Model<>(values -> {
final int n=values.size();
int i=0;
while ( i < n ) {
final int l=find(values.subList(i, n));
if ( l >= 0 ) {
i+=l;
} else {
return i;
}
}
return i;
});
}
public Model plus() {
return new Model<>(values -> {
final int n=values.size();
int i=0;
while ( i < n ) {
final int l=find(values.subList(i, n));
if ( l >= 0 ) {
i+=l;
} else {
return i > 0 ? i : -1;
}
}
return i > 0 ? i : -1;
});
}
public Model at() {
return new Model<>(values -> find(values) >= 0 ? 0 : -1);
}
public Model not() {
return new Model<>(values -> find(values) >= 0 ? -1 : 0);
}
public Model and(final Model model) {
if ( model == null ) {
throw new NullPointerException("null model");
}
return seq(this, model);
}
public Model or(final Model model) {
if ( model == null ) {
throw new NullPointerException("null model");
}
return alt(this, model);
}
public Model all(final Predicate predicate) {
if ( predicate == null ) {
throw new NullPointerException("null predicate");
}
return new Model<>(values -> {
final int n=find(values);
return n > 0 && values.subList(0, n).stream().allMatch(predicate) ? n : -1;
});
}
public Model some(final Predicate predicate) {
if ( predicate == null ) {
throw new NullPointerException("null predicate");
}
return new Model<>(values -> {
final int n=find(values);
return n > 0 && values.subList(0, n).stream().anyMatch(predicate) ? n : -1;
});
}
public Model none(final Predicate predicate) {
if ( predicate == null ) {
throw new NullPointerException("null predicate");
}
return new Model<>(values -> {
final int n=find(values);
return n > 0 && values.subList(0, n).stream().noneMatch(predicate) ? n : -1;
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy