From 7844ad1740990272319be72d9d39b45be6ca313f Mon Sep 17 00:00:00 2001 From: Federico Iosue Date: Sat, 22 Dec 2018 01:32:12 +0100 Subject: [PATCH] Fixed tags parsing and removal (fix #640) --- omniNotes/build.gradle | 2 +- .../omninotes/utils/ConstantsBase.java | 2 + .../android/omninotes/utils/TagsHelper.java | 45 ++++++++++++++----- .../omninotes/utils/TagsHelperTest.java | 23 +++++----- 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/omniNotes/build.gradle b/omniNotes/build.gradle index aa5d7f2436..ca247c26af 100644 --- a/omniNotes/build.gradle +++ b/omniNotes/build.gradle @@ -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' diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/ConstantsBase.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/ConstantsBase.java index 111c116582..79ed3c9ef3 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/ConstantsBase.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/ConstantsBase.java @@ -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 = "----------------------"; diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java index f53dcab1ff..bd563ec71b 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java @@ -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 { @@ -40,10 +45,12 @@ public static List getAllTags() { public static HashMap retrieveTags(Note note) { HashMap 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; @@ -87,9 +94,23 @@ private static boolean mapContainsTag(HashMap tagsMap, Tag tag) public static Pair removeTag(String noteTitle, String noteContent, List 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); } diff --git a/omniNotes/src/test/java/it/feio/android/omninotes/utils/TagsHelperTest.java b/omniNotes/src/test/java/it/feio/android/omninotes/utils/TagsHelperTest.java index 8b60674d6c..c156ba7f95 100644 --- a/omniNotes/src/test/java/it/feio/android/omninotes/utils/TagsHelperTest.java +++ b/omniNotes/src/test/java/it/feio/android/omninotes/utils/TagsHelperTest.java @@ -18,8 +18,7 @@ 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; @@ -27,6 +26,10 @@ 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; @@ -37,6 +40,8 @@ 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; @@ -44,18 +49,16 @@ public class TagsHelperTest { @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 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)); } @@ -73,14 +76,14 @@ public void retrievesTagsFromNoteMultilanguage() { @Test public void removesTagsFromNote() { - Pair pair = TagsHelper.removeTag(note.getTitle(), note.getContent(), java.util.Collections - .singletonList(new Tag(TAG2, 4))); + Pair pair = TagsHelper.removeTag(note.getTitle(), note.getContent(), singletonList(new Tag(TAG4, 4))); note.setTitle(pair.first); note.setContent(pair.second); HashMap tags = TagsHelper.retrieveTags(note); assertTrue(tags.containsKey(TAG1)); assertFalse(tags.containsKey(TAG2)); assertTrue(tags.containsKey(TAG3)); + assertFalse(tags.containsKey(TAG4)); }