Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
rultor committed Sep 13, 2024
2 parents 4196f04 + dafd243 commit 6928922
Showing 6 changed files with 205 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -32,3 +32,6 @@ hs_err_pid*
target
!src/it/all-have-production-class/target
!src/it/empty-project/target

# Bin file
*.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* MIT License
*
* Copyright (c) 2022-2024 Volodya Lombrozo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// @checkstyle OuterTypeNumberCheck (1 line)
package com.github.lombrozo.testnames.rules.ml;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import opennlp.tools.postag.POSModel;
import org.cactoos.Scalar;
import org.cactoos.scalar.Sticky;

/**
* Cached model source.
*
* @since 1.3.2
*/
public final class CachedModelSource implements ModelSource {

/**
* Model scalar.
*/
private final Scalar<POSModel> scalar;

/**
* Constructor.
* @param source Model source
*/
public CachedModelSource(final ModelSource source) {
this(new Sticky<>(new CachedModelFs(source)));
}

/**
* Constructor.
* @param source Model source
* @param location Cache location
*/
public CachedModelSource(final ModelSource source, final String location) {
this(new Sticky<>(new CachedModelFs(source, location)));
}

/**
* Primary constructor.
* @param sclr Model scalar
*/
public CachedModelSource(final Scalar<POSModel> sclr) {
this.scalar = sclr;
}

@Override
public POSModel model() throws Exception {
return this.scalar.value();
}
}

/**
* Model cached in file system.
* @since 1.3.2
*/
final class CachedModelFs implements Scalar<POSModel> {

/**
* Origin.
*/
private final ModelSource origin;

/**
* Cached file.
*/
private final File cached;

/**
* Constructor.
* @param orgn Origin
*/
CachedModelFs(final ModelSource orgn) {
this(orgn, "src/test/resources/ml/cached.bin");
}

/**
* Constructor.
* @param orgn Origin
* @param location Location of cached file
*/
CachedModelFs(final ModelSource orgn, final String location) {
this(orgn, Paths.get(location).toFile());
}

/**
* Primary constructor.
* @param orgn Origin
* @param ccd Cached file
*/
CachedModelFs(final ModelSource orgn, final File ccd) {
this.origin = orgn;
this.cached = ccd;
}

@Override
public POSModel value() throws Exception {
final POSModel model;
if (this.cached.exists()) {
model = new POSModel(this.cached);
} else {
if (!this.cached.toPath().isAbsolute() && !this.cached.exists()) {
Files.createDirectory(
Paths.get(
this.cached.getParent()
)
);
}
model = this.origin.model();
model.serialize(this.cached);
}
return model;
}
}
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@
*/
package com.github.lombrozo.testnames.rules.ml;

import java.io.IOException;
import opennlp.tools.postag.POSModel;

/**
@@ -36,8 +35,8 @@ public interface ModelSource {
/**
* Returns Model {@link opennlp.tools.postag.POSModel} from source.
* @return Model {@link opennlp.tools.postag.POSModel} from source.
* @throws IOException If there is a problem with reading the model.
* @throws Exception If there is a problem with reading the model.
*/
POSModel model() throws IOException;
POSModel model() throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* MIT License
*
* Copyright (c) 2022-2024 Volodya Lombrozo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.lombrozo.testnames.rules.ml;

import java.nio.file.Paths;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link CachedModelSource}.
*
* @since 1.3.2
*/
final class CachedModelSourceTest {

@Test
void cachesModelInFile() throws Exception {
final String location = "src/test/resources/ml/cached.bin";
new CachedModelSource(
new ModelSourceInternet(),
location
).model();
MatcherAssert.assertThat(
String.format(
"Model from %s is NULL, but it shouldn't",
location
),
new ModelSourceFileSystem(Paths.get(location)).model(),
Matchers.notNullValue()
);
}
}
Original file line number Diff line number Diff line change
@@ -23,29 +23,24 @@
*/
package com.github.lombrozo.testnames.rules.ml;

import java.io.IOException;
import java.nio.file.Path;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Tests for {@link ModelSourceFileSystem}.
* @since 0.10
* @todo #1:90min Enable ML tests when caching will be implemented.
* For not tests related to ML take lots of time and it's hard to develop in that mode.
* We have to implement a caching or increase the speed of tests in order to use them in
* every-day development.
*/
@Disabled
final class ModelSourceFileSystemTest {

@Test
void loadsFromFileSystem(@TempDir final Path temp) throws IOException {
void loadsFromFileSystem(@TempDir final Path temp) throws Exception {
final Path path = temp.resolve("model.bin");
new ModelSourceInternet().model().serialize(path);
new CachedModelSource(new ModelSourceInternet()).model().serialize(
path
);
MatcherAssert.assertThat(
String.format("Model from %s is null", path),
new ModelSourceFileSystem(path).model(),
Original file line number Diff line number Diff line change
@@ -24,12 +24,10 @@
package com.github.lombrozo.testnames.rules.ml;

import com.github.lombrozo.testnames.TestCase;
import java.io.IOException;
import opennlp.tools.postag.POSTaggerME;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

@@ -38,7 +36,6 @@
*
* @since 0.10
*/
@Disabled
final class RulePresentSimpleMlTest {

/**
@@ -47,8 +44,10 @@ final class RulePresentSimpleMlTest {
private static POSTaggerME model;

@BeforeAll
static void setUp() throws IOException {
RulePresentSimpleMlTest.model = new POSTaggerME(new ModelSourceInternet().model());
static void setUp() throws Exception {
RulePresentSimpleMlTest.model = new POSTaggerME(
new CachedModelSource(new ModelSourceInternet()).model()
);
}

@CsvSource({

0 comments on commit 6928922

Please sign in to comment.