├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── libs │ └── hanlp-1.6.8.jar ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hankcs │ │ └── hanlpandroiddemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ ├── README.md │ │ └── data │ │ │ └── dictionary │ │ │ ├── CoreNatureDictionary.ngram.txt.table.bin │ │ │ ├── CoreNatureDictionary.txt.bin │ │ │ ├── custom │ │ │ └── CustomDictionary.txt.bin │ │ │ ├── other │ │ │ └── CharType.bin │ │ │ └── person │ │ │ ├── nr.tr.txt │ │ │ ├── nr.txt.bin │ │ │ └── nrf.txt.trie.dat │ ├── java │ │ └── com │ │ │ └── hankcs │ │ │ └── hanlpandroiddemo │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ └── content_main.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── hankcs │ └── hanlpandroiddemo │ └── ExampleUnitTest.java └── build.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HanLP Android 示例 2 | 3 | ## portable版 4 | 5 | portable版零配置,仅提供中文分词、简繁拼音、同义词等功能。只需在`build.gradle`中加入依赖: 6 | 7 | ``` 8 | dependencies { 9 | compile 'com.hankcs:hanlp:portable-1.6.8' 10 | } 11 | ``` 12 | 13 | ## 自定义版 14 | 15 | HanLP的**全部**功能(分词、简繁、拼音、文本分类、句法分析)都兼容安卓,具体配置方法如下: 16 | 17 | 1. 下载[hanlp.jar](http://nlp.hankcs.com/download.php?file=jar)放入`app/libs`。 18 | 2. 下载[data.zip](http://nlp.hankcs.com/download.php?file=data)解压到`app/src/main/assets `,按需删除不需要的文件以减小apk体积。 19 | 3. 在程序启动时(通常是`MainApplication`或`MainActivity`的`onCreate`方法)执行初始化代码: 20 | 21 | ```java 22 | private void initHanLP() 23 | { 24 | try 25 | { 26 | Os.setenv("HANLP_ROOT", "", true); 27 | } 28 | catch (ErrnoException e) 29 | { 30 | throw new RuntimeException(e); 31 | } 32 | final AssetManager assetManager = getAssets(); 33 | HanLP.Config.IOAdapter = new IIOAdapter() 34 | { 35 | @Override 36 | public InputStream open(String path) throws IOException 37 | { 38 | return assetManager.open(path); 39 | } 40 | 41 | @Override 42 | public OutputStream create(String path) throws IOException 43 | { 44 | throw new IllegalAccessError("不支持写入" + path + "!请在编译前将需要的数据放入app/src/main/assets/data"); 45 | } 46 | }; 47 | } 48 | ``` 49 | 50 | 之后就可以像普通Java项目一样调用HanLP的全部功能了。 51 | 52 | 欢迎参考[HanLP文档](https://github.com/hankcs/HanLP)以了解更多信息。 53 | 54 | ![screenshot](http://wx1.sinaimg.cn/large/006Fmjmcly1fudp7hx5gnj30u01hcwgm.jpg) 55 | 56 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.0" 6 | defaultConfig { 7 | applicationId "com.hankcs.hanlpandroiddemo" 8 | minSdkVersion 21 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.1.0' 28 | compile 'com.android.support:design:25.1.0' 29 | testCompile 'junit:junit:4.12' 30 | } 31 | -------------------------------------------------------------------------------- /app/libs/hanlp-1.6.8.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hankcs/HanLPAndroidDemo/182aec6901b42136851cce9b139de8ad2532684c/app/libs/hanlp-1.6.8.jar -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/hankcs/SDK/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/hankcs/hanlpandroiddemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.hankcs.hanlpandroiddemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest 19 | { 20 | @Test 21 | public void useAppContext() throws Exception 22 | { 23 | // Context of the app under test. 24 | Context appContext = InstrumentationRegistry.getTargetContext(); 25 | 26 | assertEquals("com.hankcs.hanlpandroiddemo", appContext.getPackageName()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/assets/README.md: -------------------------------------------------------------------------------- 1 | 本目录为HanLP的数据文件,本演示项目仅仅保留了bigram分词相关文件。 2 | 3 | 更多功能请下载[data.zip](http://nlp.hankcs.com/download.php?file=data)解压到此目录,按需删除不需要的文件以减小apk体积。 4 | 5 | HanLP中的数据分为*词典*和*模型*,其中*词典*是词法分析必需的,*模型*是句法分析必需的。 6 | 7 | data 8 | │ 9 | ├─dictionary 10 | └─model 11 | 12 | 用户可以自行增删替换,如果不需要句法分析等功能的话,随时可以删除model文件夹。 13 | 14 | 欢迎参考[HanLP文档](https://github.com/hankcs/HanLP)以了解更多信息。 -------------------------------------------------------------------------------- /app/src/main/assets/data/dictionary/CoreNatureDictionary.ngram.txt.table.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hankcs/HanLPAndroidDemo/182aec6901b42136851cce9b139de8ad2532684c/app/src/main/assets/data/dictionary/CoreNatureDictionary.ngram.txt.table.bin -------------------------------------------------------------------------------- /app/src/main/assets/data/dictionary/CoreNatureDictionary.txt.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hankcs/HanLPAndroidDemo/182aec6901b42136851cce9b139de8ad2532684c/app/src/main/assets/data/dictionary/CoreNatureDictionary.txt.bin -------------------------------------------------------------------------------- /app/src/main/assets/data/dictionary/custom/CustomDictionary.txt.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hankcs/HanLPAndroidDemo/182aec6901b42136851cce9b139de8ad2532684c/app/src/main/assets/data/dictionary/custom/CustomDictionary.txt.bin -------------------------------------------------------------------------------- /app/src/main/assets/data/dictionary/other/CharType.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hankcs/HanLPAndroidDemo/182aec6901b42136851cce9b139de8ad2532684c/app/src/main/assets/data/dictionary/other/CharType.bin -------------------------------------------------------------------------------- /app/src/main/assets/data/dictionary/person/nr.tr.txt: -------------------------------------------------------------------------------- 1 | ,A,B,C,D,E,F,G,K,L,M,S,U,V,X,Z 2 | A,20637728,0,0,0,0,0,0,195129,0,0,0,453,0,0,0 3 | B,0,1,155650,0,94221,0,2964,0,19,0,0,24,0,0,43757 4 | C,0,0,0,155393,0,0,0,0,0,0,0,47,461,0,0 5 | D,2409,3427,0,0,0,0,0,0,141568,16990,0,0,0,63,0 6 | E,1491,2198,0,0,1000,0,0,0,81654,9104,0,4,0,45,0 7 | F,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0 8 | G,6,0,0,1000,0,0,0,0,2907,1000,0,0,0,0,0 9 | K,0,217510,0,0,0,18,0,0,0,0,0,0,0,7506,11 10 | L,256487,1000,0,0,0,0,0,0,0,0,0,7,0,0,0 11 | M,0,29629,0,0,0,0,0,0,0,0,0,0,0,526,0 12 | S,1097701,43031,0,0,1000,2,0,21858,0,0,0,32,0,928,1 13 | U,0,0,251,0,275,0,0,0,0,0,0,0,0,0,46 14 | V,447,13,0,0,0,1,0,19,0,0,0,0,0,0,0 15 | X,0,0,0,9064,0,0,0,0,0,0,0,5,18,0,0 16 | Z,594,807,0,0,0,0,0,0,38385,4010,0,0,0,19,0 17 | -------------------------------------------------------------------------------- /app/src/main/assets/data/dictionary/person/nr.txt.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hankcs/HanLPAndroidDemo/182aec6901b42136851cce9b139de8ad2532684c/app/src/main/assets/data/dictionary/person/nr.txt.bin -------------------------------------------------------------------------------- /app/src/main/assets/data/dictionary/person/nrf.txt.trie.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hankcs/HanLPAndroidDemo/182aec6901b42136851cce9b139de8ad2532684c/app/src/main/assets/data/dictionary/person/nrf.txt.trie.dat -------------------------------------------------------------------------------- /app/src/main/java/com/hankcs/hanlpandroiddemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.hankcs.hanlpandroiddemo; 2 | 3 | import android.content.Intent; 4 | import android.content.res.AssetManager; 5 | import android.net.Uri; 6 | import android.os.Bundle; 7 | import android.support.design.widget.FloatingActionButton; 8 | import android.support.design.widget.Snackbar; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.support.v7.widget.Toolbar; 11 | import android.system.ErrnoException; 12 | import android.system.Os; 13 | import android.view.View; 14 | import android.view.Menu; 15 | import android.view.MenuItem; 16 | import android.widget.Button; 17 | import android.widget.EditText; 18 | import android.widget.TextView; 19 | import com.hankcs.hanlp.HanLP; 20 | import com.hankcs.hanlp.corpus.io.IIOAdapter; 21 | import com.hankcs.hanlp.seg.common.Term; 22 | import com.hankcs.hanlp.utility.Predefine; 23 | 24 | import java.io.IOException; 25 | import java.io.InputStream; 26 | import java.io.OutputStream; 27 | import java.util.List; 28 | 29 | public class MainActivity extends AppCompatActivity 30 | { 31 | EditText editText; 32 | TextView textView; 33 | Button button; 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) 36 | { 37 | super.onCreate(savedInstanceState); 38 | setContentView(R.layout.activity_main); 39 | initHanLP(); 40 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 41 | editText = (EditText) findViewById(R.id.editText); 42 | textView = (TextView) findViewById(R.id.textView); 43 | button = (Button) findViewById(R.id.button); 44 | setSupportActionBar(toolbar); 45 | 46 | button.setOnClickListener(new View.OnClickListener() 47 | { 48 | @Override 49 | public void onClick(View v) 50 | { 51 | String text = editText.getText().toString(); 52 | List termList = HanLP.segment(text); 53 | textView.setText(termList.toString()); 54 | } 55 | }); 56 | 57 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 58 | fab.setOnClickListener(new View.OnClickListener() 59 | { 60 | @Override 61 | public void onClick(View view) 62 | { 63 | Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/hankcs/HanLP")); 64 | startActivity(browserIntent); 65 | } 66 | }); 67 | } 68 | 69 | private void initHanLP() 70 | { 71 | try 72 | { 73 | Os.setenv("HANLP_ROOT", "", true); 74 | } 75 | catch (ErrnoException e) 76 | { 77 | throw new RuntimeException(e); 78 | } 79 | final AssetManager assetManager = getAssets(); 80 | HanLP.Config.IOAdapter = new IIOAdapter() 81 | { 82 | @Override 83 | public InputStream open(String path) throws IOException 84 | { 85 | return assetManager.open(path); 86 | } 87 | 88 | @Override 89 | public OutputStream create(String path) throws IOException 90 | { 91 | throw new IllegalAccessError("不支持写入" + path + "!请在编译前将需要的数据放入app/src/main/assets/data"); 92 | } 93 | }; 94 | } 95 | 96 | @Override 97 | public boolean onCreateOptionsMenu(Menu menu) 98 | { 99 | // Inflate the menu; this adds items to the action bar if it is present. 100 | getMenuInflater().inflate(R.menu.menu_main, menu); 101 | return true; 102 | } 103 | 104 | @Override 105 | public boolean onOptionsItemSelected(MenuItem item) 106 | { 107 | // Handle action bar item clicks here. The action bar will 108 | // automatically handle clicks on the Home/Up button, so long 109 | // as you specify a parent activity in AndroidManifest.xml. 110 | int id = item.getItemId(); 111 | 112 | //noinspection SimplifiableIfStatement 113 | if (id == R.id.action_settings) 114 | { 115 | return true; 116 | } 117 | 118 | return super.onOptionsItemSelected(item); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 23 | 24 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 28 | 29 |