OSDN Git Service

Correct problem parsing notes where carriage returns did not separate some XML lines...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / filters / WordFilter.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.filters;\r
21 \r
22 import java.util.List;\r
23 \r
24 import com.trolltech.qt.sql.QSqlDatabase;\r
25 import com.trolltech.qt.sql.QSqlQuery;\r
26 \r
27 import cx.fbn.nevernote.Global;\r
28 \r
29 public class WordFilter {\r
30         private final List<String> wordList;\r
31         QSqlDatabase db;\r
32                 \r
33         public WordFilter(QSqlDatabase d, List<String> list) {\r
34                 wordList = list;\r
35                 db = d;\r
36                 QSqlQuery query = new QSqlQuery(db);\r
37                 QSqlQuery insert = new QSqlQuery(db);\r
38                 \r
39                 if (wordList == null) \r
40                         return;\r
41                 if (wordList.size() == 0)\r
42                         return;\r
43                 query.exec("create temporary table guidList (guid text)");\r
44                 query.clear();\r
45                 query.exec("delete from guidList");\r
46                 query.clear();\r
47                 query.prepare("Select guid from words where word like :word and weight>=:weight");\r
48                 insert.prepare("insert into guidList (guid) values (:guid)");\r
49                 for (int i=0; i<wordList.size(); i++) {\r
50                         query.bindValue(":word", wordList.get(i)+"%"); \r
51                         query.bindValue(":weight", Global.getRecognitionWeight());\r
52                         if (!query.exec()) {\r
53                                 Global.logger.log(Global.logger.LOW, query.lastError().toString());\r
54                         } else {\r
55                                 while (query.next()) {\r
56                                         insert.bindValue(":guid", query.value(0).toString());\r
57                                         insert.exec();\r
58                                 }\r
59                         }\r
60                 }\r
61                 query.clear();\r
62                 insert.clear();\r
63                 insert.finish();\r
64                 query.finish();\r
65         }\r
66         \r
67         public boolean contains(String guid) {\r
68                 if (wordList.size() == 0) \r
69                         return true;\r
70                 QSqlQuery query = new QSqlQuery(db);\r
71                 query.prepare("select count(guid) from guidList where guid=:guid");\r
72                 query.bindValue("guid", guid);\r
73                 query.exec();\r
74                 while (query.next()) {\r
75                         Integer count = new Integer(query.value(0).toString());\r
76                         if (count >= wordList.size()) {\r
77                                 query.clear();\r
78                                 return true;\r
79                         }\r
80                 }\r
81                 \r
82                 query.clear();\r
83                 return false;\r
84         }\r
85         \r
86 }\r