Skip to content

Commit 814f5a7

Browse files
author
caodongping
committed
article search dao
1 parent 4ec2d12 commit 814f5a7

File tree

16 files changed

+1888
-13
lines changed

16 files changed

+1888
-13
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ dependencies {
3030
compile 'com.google.code.gson:gson:2.5'
3131
compile 'net.steamcrafted:materialiconlib:1.0.9'
3232
compile 'com.pnikosis:materialish-progress:1.7'
33+
compile project(':snowball')
3334
}

app/src/main/java/com/github/mzule/androidweekly/dao/ArticleDao.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import android.database.Cursor;
55
import android.database.sqlite.SQLiteDatabase;
66
import android.database.sqlite.SQLiteOpenHelper;
7+
import android.support.annotation.NonNull;
78

89
import com.github.mzule.androidweekly.App;
910
import com.github.mzule.androidweekly.entity.Article;
11+
import com.github.mzule.androidweekly.util.StemUtil;
1012

1113
import java.util.ArrayList;
1214
import java.util.List;
@@ -33,20 +35,36 @@ public List<Article> read(String issue) {
3335
List<Article> articles = new ArrayList<>();
3436
Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM ARTICLE WHERE ISSUE=?", new String[]{issue});
3537
while (cursor.moveToNext()) {
36-
Article article = new Article();
37-
article.setTitle(cursor.getString(INDEX_TITLE));
38-
article.setBrief(cursor.getString(INDEX_BRIEF));
39-
article.setLink(cursor.getString(INDEX_LINK));
40-
article.setImageUrl(cursor.getString(INDEX_IMAGE_URL));
41-
article.setDomain(cursor.getString(INDEX_DOMAIN));
42-
article.setIssue(cursor.getString(INDEX_ISSUE));
43-
article.setSection(cursor.getString(INDEX_SECTION));
44-
articles.add(article);
38+
articles.add(read(cursor));
4539
}
4640
cursor.close();
4741
return articles;
4842
}
4943

44+
public List<Article> search(String q) {
45+
q = StemUtil.stem(q);
46+
List<Article> articles = new ArrayList<>();
47+
Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM ARTICLE WHERE FTS MATCH ?", new String[]{q});
48+
while (cursor.moveToNext()) {
49+
articles.add(read(cursor));
50+
}
51+
cursor.close();
52+
return articles;
53+
}
54+
55+
@NonNull
56+
private Article read(Cursor cursor) {
57+
Article article = new Article();
58+
article.setTitle(cursor.getString(INDEX_TITLE));
59+
article.setBrief(cursor.getString(INDEX_BRIEF));
60+
article.setLink(cursor.getString(INDEX_LINK));
61+
article.setImageUrl(cursor.getString(INDEX_IMAGE_URL));
62+
article.setDomain(cursor.getString(INDEX_DOMAIN));
63+
article.setIssue(cursor.getString(INDEX_ISSUE));
64+
article.setSection(cursor.getString(INDEX_SECTION));
65+
return article;
66+
}
67+
5068
public void save(Article article) {
5169
if (!checkExist(article)) {
5270
ContentValues cv = new ContentValues();
@@ -58,6 +76,7 @@ public void save(Article article) {
5876
cv.put("ISSUE", article.getIssue());
5977
cv.put("SECTION", article.getSection());
6078
cv.put("TYPE", 0);// TODO add type for article
79+
cv.put("FTS", StemUtil.stem(article.getFTS()));
6180
getWritableDatabase().insert("ARTICLE", null, cv);
6281
}
6382
}
@@ -72,7 +91,7 @@ private boolean checkExist(Article article) {
7291

7392
@Override
7493
public void onCreate(SQLiteDatabase db) {
75-
db.execSQL("CREATE TABLE IF NOT EXISTS ARTICLE(" +
94+
db.execSQL("CREATE VIRTUAL TABLE ARTICLE USING fts3(" +
7695
"ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
7796
"TITLE VARCHAR(255), " +
7897
"BRIEF TEXT, " +
@@ -81,7 +100,8 @@ public void onCreate(SQLiteDatabase db) {
81100
"DOMAIN VARCHAR(255), " +
82101
"ISSUE VARCHAR(255), " +
83102
"SECTION VARCHAR(255), " +
84-
"TYPE INTEGER)");
103+
"TYPE INTEGER, " +
104+
"FTS TEXT)");
85105
}
86106

87107
@Override

app/src/main/java/com/github/mzule/androidweekly/entity/Article.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public void setDomain(String domain) {
7171
this.domain = domain;
7272
}
7373

74+
public String getFTS() {
75+
return title + "\n" + brief;
76+
}
77+
7478
@Override
7579
public String toString() {
7680
return Arrays.toString(new String[]{title, "\n", brief, "\n", link, "\n", imageUrl, "\n", domain, "\n", "\n"});

app/src/main/java/com/github/mzule/androidweekly/ui/view/ProgressView.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public ProgressView(Context context) {
2525
}
2626

2727
private void init() {
28+
if (isInEditMode()) {
29+
return;
30+
}
2831
setBarColor(getResources().getColor(R.color.colorPrimary));
2932
setBarWidth(DensityUtil.dp2px(2));
3033
spin();
@@ -40,7 +43,7 @@ public void stop() {
4043

4144
@Override
4245
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
43-
if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT && getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
46+
if (!isInEditMode() && getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT && getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
4447
super.onMeasure(DensityUtil.dp2px(40), DensityUtil.dp2px(40));
4548
} else {
4649
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.github.mzule.androidweekly.util;
2+
3+
import org.tartarus.snowball.ext.EnglishStem;
4+
5+
/**
6+
* Created by CaoDongping on 4/2/16.
7+
*/
8+
public class StemUtil {
9+
public static String stem(String input) {
10+
String[] words = input.replaceAll("[^a-zA-Z ]", " ").toLowerCase().split("\\s+");
11+
StringBuilder sb = new StringBuilder();
12+
EnglishStem stem = new EnglishStem();
13+
for (String w : words) {
14+
stem.setCurrent(w);
15+
stem.stem();
16+
sb.append(stem.getCurrent()).append(" ");
17+
}
18+
return sb.toString();
19+
}
20+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@
110110
style="@style/NormalControl"
111111
app:materialIcon="star" />
112112

113+
<com.github.mzule.androidweekly.ui.view.IconButton
114+
android:id="@+id/searchButton"
115+
style="@style/NormalControl"
116+
app:materialIcon="magnify" />
117+
113118
<com.github.mzule.androidweekly.ui.view.IconButton
114119
style="@style/NormalControl"
115120
app:materialIcon="code_braces" />

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':app'
1+
include ':app', ':snowball'

snowball/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

snowball/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apply plugin: 'java'
2+
3+
dependencies {
4+
compile fileTree(dir: 'libs', include: ['*.jar'])
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.tartarus.snowball;
2+
3+
import java.lang.reflect.Method;
4+
5+
public class Among {
6+
public final int s_size; /* search string */
7+
public final char[] s; /* search string */
8+
public final int substring_i; /* index to longest matching substring */
9+
public final int result; /* result of the lookup */
10+
public final Method method; /* method to use if substring matches */
11+
public final SnowballProgram methodobject; /* object to invoke method on */
12+
13+
public Among(String s, int substring_i, int result,
14+
String methodname, SnowballProgram methodobject) {
15+
this.s_size = s.length();
16+
this.s = s.toCharArray();
17+
this.substring_i = substring_i;
18+
this.result = result;
19+
this.methodobject = methodobject;
20+
if (methodname.length() == 0) {
21+
this.method = null;
22+
} else {
23+
try {
24+
this.method = methodobject.getClass().
25+
getDeclaredMethod(methodname, new Class[0]);
26+
} catch (NoSuchMethodException e) {
27+
throw new RuntimeException(e);
28+
}
29+
}
30+
}
31+
};

0 commit comments

Comments
 (0)