Copilot commented on code in PR #778: URL: https://github.com/apache/opennlp/pull/778#discussion_r2073991833
########## opennlp-tools/src/test/java/opennlp/tools/cmdline/languagemodel/NGramLanguageModelToolTest.java: ########## @@ -0,0 +1,167 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 opennlp.tools.cmdline.languagemodel; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.function.Executable; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import opennlp.tools.AbstractLoggerTest; +import opennlp.tools.cmdline.TerminateToolException; +import opennlp.tools.ngram.NGramGenerator; +import opennlp.tools.ngram.NGramModel; +import opennlp.tools.util.StringList; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class NGramLanguageModelToolTest extends AbstractLoggerTest { + + private static final int NGRAM_MIN_LENGTH = 1; + private static final int NGRAM_MAX_LENGTH = 3; + + @TempDir + private static File testDir; + + private final InputStream sysInputStream = System.in; + + @BeforeAll + protected void beforeAll() { + setUp("opennlp", Level.INFO); + } + + @BeforeEach + protected void beforeEach() { + appender.list.clear(); + } + + @ParameterizedTest + @MethodSource("provideNgramDictionaryXML") + void testRunTool(String dataFileName, String[][] providedAndPredictedTokens) { + //Get test input-file. + File inputData = new File(testDir, String.format(dataFileName)); + + //Configure input stream to provide user-input. + StringBuilder userInput = new StringBuilder(); + for (String[] item : providedAndPredictedTokens) { + userInput.append(item[0]).append("\n"); + } + System.setIn(new ByteArrayInputStream(userInput.toString().getBytes())); + + //Invoke the tool. + NGramLanguageModelTool nGramLMTool = new NGramLanguageModelTool(); + nGramLMTool.run(new String[] {inputData.getPath()}); + + //Collect any LogStream Events generated via the tool. + List<String> actual = appender.list.stream().map(ILoggingEvent::getFormattedMessage). + toList(); + + List<Executable> assertions = new LinkedList<>(); + + //assert the expected and actual values of predicted next token for equality. + for (String[] item : providedAndPredictedTokens) { + assertions.add(() -> Assertions.assertTrue(actual.stream() + .filter(l -> l.contains(Arrays.asList(item[0].split(" ")).toString())) Review Comment: [nitpick] The use of Arrays.asList(...).toString() for filtering log messages is a bit opaque. Consider refactoring this logic to use a more direct substring match for clarity. ```suggestion .filter(l -> l.contains(String.join(" ", item[0].split(" ")))) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@opennlp.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org