Skip to content

Commit

Permalink
fixed bug with not all terms have been shown in terms occurrences
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Oct 27, 2015
1 parent 2617224 commit e66fd79
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
id 'org.asciidoctor.gradle.asciidoctor' version '1.5.1'
}

String currentVersion = '0.1.1'
String currentVersion = '0.1.2'

version = currentVersion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected String getReplacement(String matchedString, CandidateTerm term) {
StringBuilder builder = new StringBuilder();

if (term.getUrl() != null) {
builder.append("<a");
builder.append("<a target=\"_blank\"");
appendAttribute(builder, "href", term.getUrl().toExternalForm());
} else {
builder.append("<span");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected final TextWithOccurrences highlight(String letter, Map<String, Candida
final StringBuilder patternBuilder = new StringBuilder();
for (Map.Entry<String, CandidateTerm> term : normalizedMap.entrySet()) {
if (term.getValue().getPattern() == null) {
patternBuilder.append("(?<=[\\W$])");
patternBuilder.append(Pattern.quote(term.getKey()).replaceAll("\\s+", " "));
patternBuilder.append("\\w*");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Collector increment(CandidateTerm term) {
}

public Set<TermOccurrence> getOccurrences() {
TreeSet<TermOccurrence> result = new TreeSet<TermOccurrence>(Collections.reverseOrder());
TreeSet<TermOccurrence> result = new TreeSet<TermOccurrence>();
for (Builder builder : occurrences.values()) {
result.add(builder.build());
}
Expand Down Expand Up @@ -91,7 +91,11 @@ public int compareTo(TermOccurrence o) {
if (o == null) {
return 1;
}
return ((Integer) occurrence).compareTo(o.occurrence);
int occurrenceComparison = - ((Integer) occurrence).compareTo(o.occurrence);
if (occurrenceComparison != 0) {
return occurrenceComparison;
}
return term.getTerm().compareTo(o.term.getTerm());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class HtmlHighlighterSpec extends Specification {
"""
}

def "test match only from the beginning of the word"() {
expect:
Highlighter.HTML.highlight("""
Let's talk about the melancholy grail! It is so interesting!
""", CandidateTerm.create("Holy Grail").build()).text == """
Let's talk about the melancholy grail! It is so interesting!
"""
}

def "test ref replacement"() {
expect:
Highlighter.HTML.highlight("""
Expand Down

0 comments on commit e66fd79

Please sign in to comment.