OSDN Git Service

Added the ability to search for phrases
authorRandy Baumgarte <randy@fbn.cx>
Fri, 3 Sep 2010 16:29:53 +0000 (12:29 -0400)
committerRandy Baumgarte <randy@fbn.cx>
Tue, 14 Sep 2010 10:24:01 +0000 (06:24 -0400)
src/cx/fbn/nevernote/sql/REnSearch.java

index b45a3cb..3378af8 100644 (file)
@@ -27,6 +27,8 @@ import java.util.GregorianCalendar;
 import java.util.List;\r
 import java.util.regex.Pattern;\r
 \r
+import org.apache.commons.lang.StringEscapeUtils;\r
+\r
 import com.evernote.edam.type.Note;\r
 import com.evernote.edam.type.Notebook;\r
 import com.evernote.edam.type.Tag;\r
@@ -37,6 +39,7 @@ import cx.fbn.nevernote.utilities.ApplicationLogger;
 public class REnSearch {\r
        \r
        private final List<String>      searchWords;\r
+       private final List<String>  searchPhrases;\r
        private final List<String>      notebooks;\r
        private final List<String>      tags;\r
        private final List<String>      intitle;\r
@@ -67,6 +70,7 @@ public class REnSearch {
                minimumWordLength = m;\r
                minimumRecognitionWeight = r;\r
                searchWords = new ArrayList<String>();\r
+               searchPhrases = new ArrayList<String>();\r
                notebooks = new ArrayList<String>();\r
                tags = new ArrayList<String>();\r
                intitle = new ArrayList<String>();\r
@@ -214,11 +218,35 @@ public class REnSearch {
        }\r
        // Match notebooks in search terms against notes\r
        private boolean matchContentAny(Note n) {\r
-               if (todo.size() == 0 && resource.size() == 0)\r
+               if (todo.size() == 0 && resource.size() == 0 && searchPhrases.size() == 0)\r
                        return true;\r
-               \r
-               \r
+\r
+               // pull back the record\r
                n = conn.getNoteTable().getNote(n.getGuid(), true, true, false, false, false);\r
+\r
+               // Check for search phrases\r
+               String text = StringEscapeUtils.unescapeHtml(n.getContent().replaceAll("\\<.*?\\>", "")).toLowerCase();\r
+               boolean negative = false;\r
+               for (int i=0; i<searchPhrases.size(); i++) {\r
+                       String phrase = searchPhrases.get(i);\r
+                       if (phrase.startsWith("-")) {\r
+                               negative = true;\r
+                               phrase = phrase.substring(1);\r
+                       } else\r
+                               negative = false;\r
+                       phrase = phrase.substring(1);\r
+                       phrase = phrase.substring(0,phrase.length()-1);\r
+                       System.out.println(phrase);\r
+                       if (text.indexOf(phrase)>=0) {\r
+                               if (negative)\r
+                                       return false;\r
+                               else\r
+                                       return true;\r
+                       }\r
+                       if (text.indexOf(phrase)<0 && negative)\r
+                               return true;\r
+               }\r
+               \r
                for (int i=0; i<todo.size(); i++) {\r
                        String value = todo.get(i);\r
                        value = value.replace("\"", "");\r
@@ -255,7 +283,6 @@ public class REnSearch {
                for (int i=0; i<resource.size(); i++) {\r
                        String resourceString = resource.get(i);\r
                        resourceString = resourceString.replace("\"", "");\r
-                       boolean negative = false;\r
                        if (resourceString.startsWith("-"))\r
                                negative = true;\r
                        resourceString = resourceString.substring(resourceString.indexOf(":")+1);\r
@@ -341,7 +368,12 @@ public class REnSearch {
                                word = word.substring(4).trim();\r
                                pos = word.indexOf(":");\r
                        }\r
-                       if (pos < 0 && (word.length() >= minLen || word.indexOf('*')>=0)) \r
+                       boolean searchPhrase = false;\r
+                       if (pos < 0 && word.indexOf(" ") > 0) {\r
+                               searchPhrase=true;\r
+                               searchPhrases.add(word.toLowerCase());\r
+                       }\r
+                       if (!searchPhrase && pos < 0 && (word.length() >= minLen || word.indexOf('*')>=0)) \r
                                getWords().add(word);\r
                        if (word.startsWith("intitle:")) \r
                                intitle.add("*"+word+"*");\r
@@ -405,7 +437,7 @@ public class REnSearch {
                                created.add(word);\r
                        if (word.startsWith("-subjectdate:")) \r
                                created.add(word);\r
-               \r
+\r
                }\r
        }\r
        // Match notebooks in search terms against notes\r
@@ -433,19 +465,45 @@ public class REnSearch {
        }\r
        // Match notebooks in search terms against notes\r
        private boolean matchContentAll(Note n) {\r
-               if (todo.size() == 0 && resource.size() == 0)\r
+               if (todo.size() == 0 && resource.size() == 0 && searchPhrases.size() == 0)\r
                        return true;\r
                \r
                boolean returnTodo = false;\r
                boolean returnResource = false;\r
+               boolean returnPhrase = false;\r
                \r
                if (todo.size() == 0)\r
                        returnTodo = true;\r
                if (resource.size() == 0)\r
                        returnResource = true;\r
+               if (searchPhrases.size() == 0)\r
+                       returnPhrase = true;\r
                \r
                \r
                n = conn.getNoteTable().getNote(n.getGuid(), true, true, false, false, false);\r
+               \r
+               // Check for search phrases\r
+               String text = StringEscapeUtils.unescapeHtml(n.getContent().replaceAll("\\<.*?\\>", "")).toLowerCase();\r
+               boolean negative = false;\r
+               for (int i=0; i<searchPhrases.size(); i++) {\r
+                       String phrase = searchPhrases.get(i);\r
+                       if (phrase.startsWith("-")) {\r
+                               negative = true;\r
+                               phrase = phrase.substring(1);\r
+                       } else\r
+                               negative = false;\r
+                       phrase = phrase.substring(1);\r
+                       phrase = phrase.substring(0,phrase.length()-1);\r
+                       System.out.println(phrase);\r
+                       if (text.indexOf(phrase)>=0) {\r
+                               if (!negative)\r
+                                       returnPhrase = true;\r
+                       }\r
+                       if (text.indexOf(phrase)<0 && negative)\r
+                               returnPhrase = true;\r
+               }\r
+\r
+               \r
                for (int i=0; i<todo.size(); i++) {\r
                        String value = todo.get(i);\r
                        value = value.replace("\"", "");\r
@@ -486,7 +544,7 @@ public class REnSearch {
                for (int i=0; i<resource.size(); i++) {\r
                        String resourceString = resource.get(i);\r
                        resourceString = resourceString.replace("\"", "");\r
-                       boolean negative = false;\r
+                       negative = false;\r
                        if (resourceString.startsWith("-"))\r
                                negative = true;\r
                        resourceString = resourceString.substring(resourceString.indexOf(":")+1);\r
@@ -500,8 +558,7 @@ public class REnSearch {
                        }\r
                }\r
                \r
-               \r
-               return returnResource && returnTodo;\r
+               return returnResource && returnTodo && returnPhrase;\r
        }\r
        \r
        private boolean stringMatch(String content, String text, boolean negative) {\r