• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ljacqu / wordeval / 14575565697

21 Apr 2025 02:51PM UTC coverage: 59.907% (+1.9%) from 58.038%
14575565697

push

github

ljacqu
Create service to unmunch Hunspell dictionaries (work in progress)

330 of 635 branches covered (51.97%)

70 of 72 new or added lines in 5 files covered. (97.22%)

904 of 1509 relevant lines covered (59.91%)

3.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.67
/src/main/java/ch/jalu/wordeval/dictionary/hunspell/ParserToModelConverter.java
1
package ch.jalu.wordeval.dictionary.hunspell;
2

3
import ch.jalu.wordeval.dictionary.hunspell.condition.AffixCondition;
4
import ch.jalu.wordeval.dictionary.hunspell.condition.AnyTokenCondition;
5
import ch.jalu.wordeval.dictionary.hunspell.condition.CharSequenceConditions;
6
import ch.jalu.wordeval.dictionary.hunspell.condition.RegexCondition;
7
import ch.jalu.wordeval.dictionary.hunspell.condition.SingleCharCondition;
8
import ch.jalu.wordeval.dictionary.hunspell.parser.ParsedAffixes;
9
import ch.jalu.wordeval.dictionary.hunspell.parser.ParsedRule;
10
import org.jheaps.annotations.VisibleForTesting;
11
import org.springframework.stereotype.Component;
12

13
import java.util.List;
14
import java.util.Map;
15
import java.util.function.Function;
16
import java.util.stream.Collectors;
17

18
import static org.apache.commons.lang3.ObjectUtils.firstNonNull;
19

20
/**
21
 * Converter from {@link ParsedAffixes} to {@link HunspellAffixes}.
22
 */
23
@Component
24
public class ParserToModelConverter {
3✔
25

26
  public HunspellAffixes convert(ParsedAffixes parsedAffixes) {
27
    AffixFlagType flagType = firstNonNull(parsedAffixes.getFlagType(), AffixFlagType.SINGLE);
14✔
28
    Map<String, AffixRule> rulesByName = convertRules(parsedAffixes.getRules());
5✔
29

30
    HunspellAffixes affixesDefinition = new HunspellAffixes();
4✔
31
    affixesDefinition.setFlagType(flagType);
3✔
32
    affixesDefinition.setAffixRulesByName(rulesByName);
3✔
33
    return affixesDefinition;
2✔
34
  }
35

36
  private Map<String, AffixRule> convertRules(List<ParsedRule> rules) {
37
    return rules.stream()
5✔
38
        .map(this::convertRule)
2✔
39
        .collect(Collectors.toMap(AffixRule::getFlag, Function.identity()));
4✔
40
  }
41

42
  private AffixRule convertRule(ParsedRule rule) {
43
    AffixRule affixRule = new AffixRule();
4✔
44
    affixRule.setType(rule.type);
4✔
45
    affixRule.setFlag(rule.flag);
4✔
46
    affixRule.setCrossProduct(rule.crossProduct);
4✔
47

48
    for (ParsedRule.RuleEntry ruleEntry : rule.rules) {
11✔
49
      AffixCondition condition = convertCondition(ruleEntry.condition, rule.type);
7✔
50
      AffixRule.AffixRuleEntry affixRuleEntry = rule.type == AffixType.PFX
4!
NEW
51
          ? new AffixRule.PrefixRuleEntry(condition, ruleEntry.strip, ruleEntry.append)
×
52
          : new AffixRule.SuffixRuleEntry(condition, ruleEntry.strip, ruleEntry.append);
9✔
53
      affixRule.getRules().add(affixRuleEntry);
5✔
54
    }
1✔
55
    return affixRule;
2✔
56
  }
57

58
  @VisibleForTesting
59
  AffixCondition convertCondition(String pattern, AffixType type) {
60
    if (".".equals(pattern)) {
4✔
61
      return AnyTokenCondition.INSTANCE;
2✔
62
    }
63
    AffixCondition condition;
64
    if (pattern.charAt(0) == '[') {
5✔
65
      condition = SingleCharCondition.createConditionIfApplicable(pattern, type);
5✔
66
    } else {
67
      condition = CharSequenceConditions.createConditionIfApplicable(pattern, type);
4✔
68
    }
69

70
    return condition == null
3✔
71
        ? new RegexCondition(pattern, type)
6✔
72
        : condition;
1✔
73
  }
74
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc