Skip to content

Commit

Permalink
Fixed tags parsing and removal (fix #640)
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Iosue committed Dec 22, 2018
1 parent 77942a9 commit 7844ad1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
2 changes: 1 addition & 1 deletion omniNotes/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ dependencies {
implementation ('com.github.federicoiosue:checklistview:3.2.1') {
transitive=false
}
implementation 'com.github.federicoiosue:pixlui:2.6'
implementation 'com.github.federicoiosue:pixlui:3.0.0'

// Flavors specific dependencies
playImplementation 'io.nlopez.smartlocation:library:3.2.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public interface ConstantsBase {
String TIMESTAMP_UNIX_EPOCH = "0";
String TIMESTAMP_UNIX_EPOCH_FAR = "18464193800000";

String TAG_SPECIAL_CHARS_TO_REMOVE = "[<>\\[\\],-\\.\\(\\)\\[\\]\\{\\}\\!\\?]";

int MENU_SORT_GROUP_ID = 11998811;

String MERGED_NOTES_SEPARATOR = "----------------------";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@
package it.feio.android.omninotes.utils;

import android.support.v4.util.Pair;
import it.feio.android.omninotes.db.DbHelper;
import it.feio.android.omninotes.models.Note;
import it.feio.android.omninotes.models.Tag;
import it.feio.android.pixlui.links.RegexPatternsConstants;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;

import it.feio.android.omninotes.db.DbHelper;
import it.feio.android.omninotes.models.Note;
import it.feio.android.omninotes.models.Tag;
import it.feio.android.pixlui.links.UrlCompleter;
import rx.Observable;

import static it.feio.android.omninotes.utils.ConstantsBase.TAG_SPECIAL_CHARS_TO_REMOVE;


public class TagsHelper {
Expand All @@ -40,10 +45,12 @@ public static List<Tag> getAllTags() {

public static HashMap<String, Integer> retrieveTags(Note note) {
HashMap<String, Integer> tagsMap = new HashMap<>();
for (String token : (note.getTitle() + " " + note.getContent()).replaceAll("\n", " ").trim().split(" ")) {
if (RegexPatternsConstants.HASH_TAG.matcher(token).matches()) {
int count = tagsMap.get(token) == null ? 0 : tagsMap.get(token);
tagsMap.put(token, ++count);
String[] words = (note.getTitle() + " " + note.getContent()).replaceAll("\n", " ").trim().split(" ");
for (String word: words) {
String parsedHashtag = UrlCompleter.parseHashtag(word);
if (StringUtils.isNotEmpty(parsedHashtag)) {
int count = tagsMap.get(parsedHashtag) == null ? 0 : tagsMap.get(parsedHashtag);
tagsMap.put(parsedHashtag, ++count);
}
}
return tagsMap;
Expand Down Expand Up @@ -87,9 +94,23 @@ private static boolean mapContainsTag(HashMap<String, Integer> tagsMap, Tag tag)
public static Pair<String, String> removeTag(String noteTitle, String noteContent, List<Tag> tagsToRemove) {
String title = noteTitle, content = noteContent;
for (Tag tagToRemove : tagsToRemove) {
String tagRegex = tagToRemove.getText() + "(\\s)|" + tagToRemove.getText() + "$";
title = title.replaceAll(tagRegex, "");
content = content.replaceAll(tagRegex, "");
if (StringUtils.isNotEmpty(title)) {
title = Observable.from(title.replaceAll(TAG_SPECIAL_CHARS_TO_REMOVE, " ").split("\\s"))
.map(String::trim)
.filter(s -> !s.matches(tagToRemove.getText()))
.reduce((s, s2) -> s + " " + s2)
.toBlocking()
.singleOrDefault("");
}
if (StringUtils.isNotEmpty(content)) {
content = Observable.from(content.replaceAll(TAG_SPECIAL_CHARS_TO_REMOVE, " ").split("\\s"))
.map(String::trim)
.filter(s -> !s.matches(tagToRemove.getText()))
.reduce((s, s2) -> s + " " + s2)
.toBlocking()
.singleOrDefault("");
}

}
return new Pair<>(title, content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
package it.feio.android.omninotes.utils;

import android.support.v4.util.Pair;
import it.feio.android.omninotes.models.Note;
import it.feio.android.omninotes.models.Tag;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import it.feio.android.omninotes.models.Note;
import it.feio.android.omninotes.models.Tag;

import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand All @@ -37,25 +40,25 @@ public class TagsHelperTest {
private static String TAG1 = "#mixed";
private static String TAG2 = "#123numbered";
private static String TAG3 = "#tags";
private static String TAG4 = "#tag";
private static String TAG5 = "#numberedAfter123";

private Note note;


@Before
public void setup() {
note = new Note();
note.setContent("Random content with " + TAG1 + " " + TAG2 + " " + TAG3);
note.setContent("Random content with " + TAG1 + " " + TAG2 + " " + TAG3 + "(and another with similar prefix: " + TAG4 + ") and " + TAG5);
}


@Test
public void retrievesTagsFromNote() {
HashMap<String, Integer> tags = TagsHelper.retrieveTags(note);
assertEquals(tags.size(), 3);
assertTrue(tags.containsKey(TAG1));
assertTrue(tags.containsKey(TAG2));
assertTrue(tags.containsKey(TAG3));
assertFalse(tags.containsKey("#nonExistingTag"));
assertEquals(tags.size(), 4);
assertTrue(tags.containsKey(TAG1) && tags.containsKey(TAG3) && tags.containsKey(TAG4) && tags.containsKey(TAG5));
assertFalse(tags.containsKey(TAG2));
}


Expand All @@ -73,14 +76,14 @@ public void retrievesTagsFromNoteMultilanguage() {

@Test
public void removesTagsFromNote() {
Pair<String, String> pair = TagsHelper.removeTag(note.getTitle(), note.getContent(), java.util.Collections
.singletonList(new Tag(TAG2, 4)));
Pair<String, String> pair = TagsHelper.removeTag(note.getTitle(), note.getContent(), singletonList(new Tag(TAG4, 4)));
note.setTitle(pair.first);
note.setContent(pair.second);
HashMap<String, Integer> tags = TagsHelper.retrieveTags(note);
assertTrue(tags.containsKey(TAG1));
assertFalse(tags.containsKey(TAG2));
assertTrue(tags.containsKey(TAG3));
assertFalse(tags.containsKey(TAG4));
}


Expand Down

0 comments on commit 7844ad1

Please sign in to comment.