forked from volodya-lombrozo/jtcop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
205 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
138 changes: 138 additions & 0 deletions
138
src/main/java/com/github/lombrozo/testnames/rules/ml/CachedModelSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/java/com/github/lombrozo/testnames/rules/ml/CachedModelSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters