-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract handling of Japanese in SpellingVdalitor as JapaneseExpressio…
…nVariationValidator
- Loading branch information
Showing
6 changed files
with
117 additions
and
55 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...core/src/main/java/cc/redpen/validator/sentence/JapaneseExpressionVariationValidator.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,62 @@ | ||
/** | ||
* redpen: a text inspection tool | ||
* Copyright (c) 2014-2015 Recruit Technologies Co., Ltd. and contributors | ||
* (see CONTRIBUTORS.md) | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 cc.redpen.validator.sentence; | ||
|
||
import cc.redpen.model.Sentence; | ||
import cc.redpen.tokenizer.TokenElement; | ||
import cc.redpen.validator.Validator; | ||
|
||
import java.util.*; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
public class JapaneseExpressionVariationValidator extends Validator { | ||
private Map<String, List<TokenElement>> words = new HashMap<>(); | ||
|
||
@Override | ||
public void validate(Sentence sentence) { | ||
for (TokenElement token : sentence.getTokens()) { | ||
String reading = token.getTags().get(7); | ||
if (this.words.containsKey(reading)) { | ||
List<TokenElement> tokens = this.words.get(reading); | ||
for (TokenElement candidate : tokens) { | ||
if (candidate != token && !token.getSurface().equals(candidate.getSurface())) { | ||
addLocalizedErrorFromToken(sentence, token); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void preValidate(Sentence sentence) { | ||
for (TokenElement token : sentence.getTokens()) { | ||
String reading = token.getTags().get(7); | ||
if (!this.words.containsKey(reading)) { | ||
this.words.put(reading, new LinkedList<TokenElement>()); | ||
} | ||
this.words.get(reading).add(token); | ||
|
||
} | ||
} | ||
|
||
@Override | ||
public List<String> getSupportedLanguages() { | ||
return singletonList(Locale.JAPANESE.getLanguage()); | ||
} | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
.../src/test/java/cc/redpen/validator/sentence/JapaneseExpressionVariationValidatorTest.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,52 @@ | ||
/** | ||
* redpen: a text inspection tool | ||
* Copyright (c) 2014-2015 Recruit Technologies Co., Ltd. and contributors | ||
* (see CONTRIBUTORS.md) | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 cc.redpen.validator.sentence; | ||
|
||
import cc.redpen.RedPen; | ||
import cc.redpen.RedPenException; | ||
import cc.redpen.config.Configuration; | ||
import cc.redpen.config.ValidatorConfiguration; | ||
import cc.redpen.model.Document; | ||
import cc.redpen.validator.BaseValidatorTest; | ||
import cc.redpen.validator.ValidationError; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class JapaneseExpressionVariationValidatorTest extends BaseValidatorTest { | ||
protected JapaneseExpressionVariationValidatorTest() { | ||
super("JapaneseExpressionVariation"); | ||
} | ||
|
||
@Test | ||
void japanese() throws RedPenException { | ||
config = Configuration.builder("ja") | ||
.addValidatorConfig(new ValidatorConfiguration(validatorName)) | ||
.build(); | ||
|
||
Document document = prepareSimpleDocument("之は山です。これは川です。"); | ||
|
||
RedPen redPen = new RedPen(config); | ||
Map<Document, List<ValidationError>> errors = redPen.validate(singletonList(document)); | ||
assertEquals(2, errors.get(document).size()); | ||
} | ||
} |
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