(size);
56 | for (int i = 0; i < size; i++) {
57 | tags.add(new FavoritesTag(list.getJSONObject(i)));
58 | }
59 | return tags;
60 | } catch (JSONException jsone) {
61 | throw new WeiboException(jsone);
62 | } catch (WeiboException te) {
63 | throw te;
64 | }
65 | }
66 |
67 |
68 |
69 | @Override
70 | public int hashCode() {
71 | final int prime = 31;
72 | int result = 1;
73 | result = prime * result + ((id == null) ? 0 : id.hashCode());
74 | return result;
75 | }
76 |
77 |
78 | @Override
79 | public boolean equals(Object obj) {
80 | if (this == obj)
81 | return true;
82 | if (obj == null)
83 | return false;
84 | if (getClass() != obj.getClass())
85 | return false;
86 | FavoritesTag other = (FavoritesTag) obj;
87 | if (id == null) {
88 | if (other.id != null)
89 | return false;
90 | } else if (!id.equals(other.id))
91 | return false;
92 | return true;
93 | }
94 |
95 |
96 | public String getId() {
97 | return id;
98 | }
99 |
100 |
101 | public void setId(String id) {
102 | this.id = id;
103 | }
104 |
105 |
106 | public String getTag() {
107 | return tag;
108 | }
109 |
110 |
111 | public void setTag(String tag) {
112 | this.tag = tag;
113 | }
114 |
115 |
116 | public int getCount() {
117 | return count;
118 | }
119 |
120 |
121 | public void setCount(int count) {
122 | this.count = count;
123 | }
124 |
125 |
126 | @Override
127 | public String toString() {
128 | return "FavoritesTag [id=" + id + ", tag=" + tag + ", count=" + count
129 | + "]";
130 | }
131 |
132 | }
133 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/ShortUrl.java:
--------------------------------------------------------------------------------
1 | package weibo4j;
2 |
3 | import weibo4j.model.PostParameter;
4 | import weibo4j.model.WeiboException;
5 | import weibo4j.org.json.JSONObject;
6 | import weibo4j.util.WeiboConfig;
7 |
8 | public class ShortUrl extends Weibo{
9 | /**
10 | *
11 | */
12 | private static final long serialVersionUID = 1L;
13 | /**
14 | * 长链接转为短链接
15 | *
16 | *
17 | */
18 | public JSONObject longToShortUrl (String url_long) throws WeiboException {
19 | return client.get(WeiboConfig.getValue("baseURL") + "short_url/shorten.json",new PostParameter[] {
20 | new PostParameter("url_long",url_long)
21 | }).asJSONObject();
22 | }
23 |
24 | /**
25 | * 短链接转为长链接
26 | *
27 | */
28 | public JSONObject shortToLongUrl (String url_short) throws WeiboException {
29 | return client.get(WeiboConfig.getValue("baseURL") + "short_url/expand.json",new PostParameter[] {
30 | new PostParameter("url_short",url_short)
31 | }).asJSONObject();
32 | }
33 |
34 | /**
35 | * 获取短链接的总点击数
36 | *
37 | *
38 | */
39 | public JSONObject clicksOfUrl (String url_short) throws WeiboException {
40 | return client.get(WeiboConfig.getValue("baseURL") + "short_url/clicks.json",new PostParameter[] {
41 | new PostParameter("url_short",url_short)
42 | }).asJSONObject();
43 | }
44 |
45 | /**
46 | * 获取一个短链接点击的referer来源和数量
47 | *
48 | *
49 | */
50 | public JSONObject referersOfUrl (String url_short) throws WeiboException {
51 | return client.get(WeiboConfig.getValue("baseURL") + "short_url/referers.json",new PostParameter[] {
52 | new PostParameter("url_short",url_short)
53 | }).asJSONObject();
54 | }
55 |
56 | /**
57 | *
58 | * 获取一个短链接点击的地区来源和数量
59 | *
60 | */
61 | public JSONObject locationsOfUrl (String url_short) throws WeiboException {
62 | return client.get(WeiboConfig.getValue("baseURL") + "short_url/locations.json",new PostParameter[] {
63 | new PostParameter("url_short",url_short)
64 | }).asJSONObject();
65 | }
66 |
67 | /**
68 | * 获取短链接在微博上的微博分享数
69 | *
70 | *
71 | *
72 | */
73 | public JSONObject shareCountsOfUrl (String url_short) throws WeiboException {
74 | return client.get(WeiboConfig.getValue("baseURL") + "short_url/share/counts.json",new PostParameter[] {
75 | new PostParameter("url_short",url_short)
76 | }).asJSONObject();
77 | }
78 | /**
79 | * 获取包含指定单个短链接的最新微博内容
80 | *
81 | *
82 | */
83 | public JSONObject statusesContentUrl (String url_short) throws WeiboException {
84 | return client.get(WeiboConfig.getValue("baseURL") + "short_url/share/statuses.json",new PostParameter[] {
85 | new PostParameter("url_short",url_short)
86 | }).asJSONObject();
87 | }
88 | /**
89 | * 获取短链接在微博上的微博评论数
90 | *
91 | *
92 | */
93 | public JSONObject commentCountOfUrl (String url_short) throws WeiboException {
94 | return client.get(WeiboConfig.getValue("baseURL") + "short_url/comment/counts.json",new PostParameter[] {
95 | new PostParameter("url_short",url_short)
96 | }).asJSONObject();
97 | }
98 | /**
99 | * 获取包含指定单个短链接的最新微博评论
100 | *
101 | */
102 | public JSONObject commentsContentUrl (String url_short) throws WeiboException {
103 | return client.get(WeiboConfig.getValue("baseURL") + "short_url/comment/comments.json",new PostParameter[] {
104 | new PostParameter("url_short",url_short)
105 | }).asJSONObject();
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/org/wltea/analyzer/core/CharacterUtil.java:
--------------------------------------------------------------------------------
1 | /**
2 | * IK 中文分词 版本 5.0
3 | * IK Analyzer release 5.0
4 | *
5 | * Licensed to the Apache Software Foundation (ASF) under one or more
6 | * contributor license agreements. See the NOTICE file distributed with
7 | * this work for additional information regarding copyright ownership.
8 | * The ASF licenses this file to You under the Apache License, Version 2.0
9 | * (the "License"); you may not use this file except in compliance with
10 | * the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing, software
15 | * distributed under the License is distributed on an "AS IS" BASIS,
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 | * See the License for the specific language governing permissions and
18 | * limitations under the License.
19 | *
20 | * 源代码由林良益(linliangyi2005@gmail.com)提供
21 | * 版权声明 2012,乌龙茶工作室
22 | * provided by Linliangyi and copyright 2012 by Oolong studio
23 | *
24 | * 字符集识别工具类
25 | */
26 | package org.wltea.analyzer.core;
27 |
28 | /**
29 | *
30 | * 字符集识别工具类
31 | */
32 | class CharacterUtil {
33 |
34 | public static final int CHAR_USELESS = 0;
35 |
36 | public static final int CHAR_ARABIC = 0X00000001;
37 |
38 | public static final int CHAR_ENGLISH = 0X00000002;
39 |
40 | public static final int CHAR_CHINESE = 0X00000004;
41 |
42 | public static final int CHAR_OTHER_CJK = 0X00000008;
43 |
44 |
45 | /**
46 | * 识别字符类型
47 | * @param input
48 | * @return int CharacterUtil定义的字符类型常量
49 | */
50 | static int identifyCharType(char input){
51 | if(input >= '0' && input <= '9'){
52 | return CHAR_ARABIC;
53 |
54 | }else if((input >= 'a' && input <= 'z')
55 | || (input >= 'A' && input <= 'Z')){
56 | return CHAR_ENGLISH;
57 |
58 | }else {
59 | Character.UnicodeBlock ub = Character.UnicodeBlock.of(input);
60 |
61 | if(ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
62 | || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
63 | || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A){
64 | //目前已知的中文字符UTF-8集合
65 | return CHAR_CHINESE;
66 |
67 | }else if(ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS //全角数字字符和日韩字符
68 | //韩文字符集
69 | || ub == Character.UnicodeBlock.HANGUL_SYLLABLES
70 | || ub == Character.UnicodeBlock.HANGUL_JAMO
71 | || ub == Character.UnicodeBlock.HANGUL_COMPATIBILITY_JAMO
72 | //日文字符集
73 | || ub == Character.UnicodeBlock.HIRAGANA //平假名
74 | || ub == Character.UnicodeBlock.KATAKANA //片假名
75 | || ub == Character.UnicodeBlock.KATAKANA_PHONETIC_EXTENSIONS){
76 | return CHAR_OTHER_CJK;
77 |
78 | }
79 | }
80 | //其他的不做处理的字符
81 | return CHAR_USELESS;
82 | }
83 |
84 | /**
85 | * 进行字符规格化(全角转半角,大写转小写处理)
86 | * @param input
87 | * @return char
88 | */
89 | static char regularize(char input){
90 | if (input == 12288) {
91 | input = (char) 32;
92 |
93 | }else if (input > 65280 && input < 65375) {
94 | input = (char) (input - 65248);
95 |
96 | }else if (input >= 'A' && input <= 'Z') {
97 | input += 32;
98 | }
99 |
100 | return input;
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/org/json/JSONStringer.java:
--------------------------------------------------------------------------------
1 | package weibo4j.org.json;
2 |
3 | /*
4 | Copyright (c) 2006 JSON.org
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | The Software shall be used for Good, not Evil.
17 |
18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | SOFTWARE.
25 | */
26 |
27 | import java.io.StringWriter;
28 |
29 | /**
30 | * JSONStringer provides a quick and convenient way of producing JSON text.
31 | * The texts produced strictly conform to JSON syntax rules. No whitespace is
32 | * added, so the results are ready for transmission or storage. Each instance of
33 | * JSONStringer can produce one JSON text.
34 | *
35 | * A JSONStringer instance provides a value method for appending
36 | * values to the
37 | * text, and a key
38 | * method for adding keys before values in objects. There are array
39 | * and endArray methods that make and bound array values, and
40 | * object and endObject methods which make and bound
41 | * object values. All of these methods return the JSONWriter instance,
42 | * permitting cascade style. For example,
43 | * myString = new JSONStringer()
44 | * .object()
45 | * .key("JSON")
46 | * .value("Hello, World!")
47 | * .endObject()
48 | * .toString(); which produces the string
49 | * {"JSON":"Hello, World!"}
50 | *
51 | * The first method called must be array or object.
52 | * There are no methods for adding commas or colons. JSONStringer adds them for
53 | * you. Objects and arrays can be nested up to 20 levels deep.
54 | *
55 | * This can sometimes be easier than using a JSONObject to build a string.
56 | * @author JSON.org
57 | * @version 2008-09-18
58 | */
59 | public class JSONStringer extends JSONWriter {
60 | /**
61 | * Make a fresh JSONStringer. It can be used to build one JSON text.
62 | */
63 | public JSONStringer() {
64 | super(new StringWriter());
65 | }
66 |
67 | /**
68 | * Return the JSON text. This method is used to obtain the product of the
69 | * JSONStringer instance. It will return null if there was a
70 | * problem in the construction of the JSON text (such as the calls to
71 | * array were not properly balanced with calls to
72 | * endArray).
73 | * @return The JSON text.
74 | */
75 | public String toString() {
76 | return this.mode == 'd' ? this.writer.toString() : null;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/model/WeiboException.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2007-2009, Yusuke Yamamoto
3 | All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without
6 | modification, are permitted provided that the following conditions are met:
7 | * Redistributions of source code must retain the above copyright
8 | notice, this list of conditions and the following disclaimer.
9 | * Redistributions in binary form must reproduce the above copyright
10 | notice, this list of conditions and the following disclaimer in the
11 | documentation and/or other materials provided with the distribution.
12 | * Neither the name of the Yusuke Yamamoto nor the
13 | names of its contributors may be used to endorse or promote products
14 | derived from this software without specific prior written permission.
15 |
16 | THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
17 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 | DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 | */
27 | package weibo4j.model;
28 |
29 | import weibo4j.org.json.JSONException;
30 | import weibo4j.org.json.JSONObject;
31 |
32 | /**
33 | * An exception class that will be thrown when WeiboAPI calls are failed.
34 | * In case the Weibo server returned HTTP error code, you can get the HTTP status code using getStatusCode() method.
35 | * @author Yusuke Yamamoto - yusuke at mac.com
36 | */
37 | public class WeiboException extends Exception {
38 | private int statusCode = -1;
39 | private int errorCode = -1;
40 | private String request;
41 | private String error;
42 | private static final long serialVersionUID = -2623309261327598087L;
43 |
44 | public WeiboException(String msg) {
45 | super(msg);
46 | }
47 |
48 | public WeiboException(Exception cause) {
49 | super(cause);
50 | }
51 |
52 | public WeiboException(String msg , int statusCode) throws JSONException {
53 | super(msg);
54 | this.statusCode = statusCode;
55 | }
56 |
57 | public WeiboException(String msg , JSONObject json, int statusCode) throws JSONException {
58 | super(msg + "\n error:" + json.getString("error") +" error_code:" + json.getInt("error_code") + json.getString("request"));
59 | this.statusCode = statusCode;
60 | this.errorCode = json.getInt("error_code");
61 | this.error = json.getString("error");
62 | this.request = json.getString("request");
63 |
64 | }
65 |
66 | public WeiboException(String msg, Exception cause) {
67 | super(msg, cause);
68 | }
69 |
70 | public WeiboException(String msg, Exception cause, int statusCode) {
71 | super(msg, cause);
72 | this.statusCode = statusCode;
73 |
74 | }
75 |
76 | public int getStatusCode() {
77 | return this.statusCode;
78 | }
79 |
80 | public int getErrorCode() {
81 | return errorCode;
82 | }
83 |
84 | public String getRequest() {
85 | return request;
86 | }
87 |
88 | public String getError() {
89 | return error;
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/org/json/CookieList.java:
--------------------------------------------------------------------------------
1 | package weibo4j.org.json;
2 |
3 | /*
4 | Copyright (c) 2002 JSON.org
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | The Software shall be used for Good, not Evil.
17 |
18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | SOFTWARE.
25 | */
26 |
27 | import java.util.Iterator;
28 |
29 | /**
30 | * Convert a web browser cookie list string to a JSONObject and back.
31 | * @author JSON.org
32 | * @version 2008-09-18
33 | */
34 | public class CookieList {
35 |
36 | /**
37 | * Convert a cookie list into a JSONObject. A cookie list is a sequence
38 | * of name/value pairs. The names are separated from the values by '='.
39 | * The pairs are separated by ';'. The names and the values
40 | * will be unescaped, possibly converting '+' and '%' sequences.
41 | *
42 | * To add a cookie to a cooklist,
43 | * cookielistJSONObject.put(cookieJSONObject.getString("name"),
44 | * cookieJSONObject.getString("value"));
45 | * @param string A cookie list string
46 | * @return A JSONObject
47 | * @throws JSONException
48 | */
49 | public static JSONObject toJSONObject(String string) throws JSONException {
50 | JSONObject o = new JSONObject();
51 | JSONTokener x = new JSONTokener(string);
52 | while (x.more()) {
53 | String name = Cookie.unescape(x.nextTo('='));
54 | x.next('=');
55 | o.put(name, Cookie.unescape(x.nextTo(';')));
56 | x.next();
57 | }
58 | return o;
59 | }
60 |
61 |
62 | /**
63 | * Convert a JSONObject into a cookie list. A cookie list is a sequence
64 | * of name/value pairs. The names are separated from the values by '='.
65 | * The pairs are separated by ';'. The characters '%', '+', '=', and ';'
66 | * in the names and values are replaced by "%hh".
67 | * @param o A JSONObject
68 | * @return A cookie list string
69 | * @throws JSONException
70 | */
71 | public static String toString(JSONObject o) throws JSONException {
72 | boolean b = false;
73 | Iterator keys = o.keys();
74 | String s;
75 | StringBuffer sb = new StringBuffer();
76 | while (keys.hasNext()) {
77 | s = keys.next().toString();
78 | if (!o.isNull(s)) {
79 | if (b) {
80 | sb.append(';');
81 | }
82 | sb.append(Cookie.escape(s));
83 | sb.append("=");
84 | sb.append(Cookie.escape(o.getString(s)));
85 | b = true;
86 | }
87 | }
88 | return sb.toString();
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/util/URLEncodeUtils.java:
--------------------------------------------------------------------------------
1 | package weibo4j.util;
2 | import java.io.UnsupportedEncodingException;
3 | import java.net.URLDecoder;
4 | import java.net.URLEncoder;
5 | import java.util.BitSet;
6 | /**
7 | * @author sinaWeibo
8 | *
9 | */
10 | public class URLEncodeUtils {
11 |
12 | static BitSet dontNeedEncoding;
13 |
14 | static {
15 |
16 | /*
17 | * The list of characters that are not encoded has been determined as
18 | * follows:
19 | *
20 | * RFC 2396 states: ----- Data characters that are allowed in a URI but
21 | * do not have a reserved purpose are called unreserved. These include
22 | * upper and lower case letters, decimal digits, and a limited set of
23 | * punctuation marks and symbols.
24 | *
25 | * unreserved = alphanum | mark
26 | *
27 | * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
28 | *
29 | * Unreserved characters can be escaped without changing the semantics
30 | * of the URI, but this should not be done unless the URI is being used
31 | * in a context that does not allow the unescaped character to appear.
32 | * -----
33 | *
34 | * It appears that both Netscape and Internet Explorer escape all
35 | * special characters from this list with the exception of "-", "_",
36 | * ".", "*". While it is not clear why they are escaping the other
37 | * characters, perhaps it is safest to assume that there might be
38 | * contexts in which the others are unsafe if not escaped. Therefore, we
39 | * will use the same list. It is also noteworthy that this is consistent
40 | * with O'Reilly's "HTML: The Definitive Guide" (page 164).
41 | *
42 | * As a last note, Intenet Explorer does not encode the "@" character
43 | * which is clearly not unreserved according to the RFC. We are being
44 | * consistent with the RFC in this matter, as is Netscape.
45 | */
46 |
47 | dontNeedEncoding = new BitSet(256);
48 | int i;
49 | for (i = 'a'; i <= 'z'; i++) {
50 | dontNeedEncoding.set(i);
51 | }
52 | for (i = 'A'; i <= 'Z'; i++) {
53 | dontNeedEncoding.set(i);
54 | }
55 | for (i = '0'; i <= '9'; i++) {
56 | dontNeedEncoding.set(i);
57 | }
58 | dontNeedEncoding.set(' '); /*
59 | * encoding a space to a + is done in the
60 | * encode() method
61 | */
62 | dontNeedEncoding.set('-');
63 | dontNeedEncoding.set('_');
64 | dontNeedEncoding.set('.');
65 | dontNeedEncoding.set('*');
66 |
67 | dontNeedEncoding.set('+');
68 | dontNeedEncoding.set('%');
69 |
70 | }
71 |
72 | /**
73 | * 判断段落文本是否被urlencode过
74 | *
75 | * @param str
76 | * @return
77 | */
78 | public static final boolean isURLEncoded(String str) {
79 | if (str==null||"".equals(str)) {
80 | return false;
81 | }
82 | char[] chars = str.toCharArray();
83 | boolean containsPercent = false;
84 | for (char c : chars) {
85 | if (Character.isWhitespace(c)) {
86 | return false;
87 | }
88 | if (!dontNeedEncoding.get(c)) {
89 | return false;
90 | }
91 | if(c == '%'){
92 | containsPercent = true;
93 | }
94 | }
95 | if(!containsPercent){
96 | return false;
97 | }
98 | return true;
99 | }
100 |
101 | public static final String encodeURL(String str) {
102 | try {
103 | return URLEncoder.encode(str, "utf-8");
104 | } catch (UnsupportedEncodingException e) {
105 | throw new RuntimeException(e);
106 | }
107 | }
108 | public static final String decodeURL(String str) {
109 | try {
110 | return URLDecoder.decode(str, "utf-8");
111 | } catch (UnsupportedEncodingException e) {
112 | throw new RuntimeException(e);
113 | }
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/Place.java:
--------------------------------------------------------------------------------
1 | package weibo4j;
2 |
3 | import weibo4j.model.PostParameter;
4 | import weibo4j.model.Status;
5 | import weibo4j.model.StatusWapper;
6 | import weibo4j.model.User;
7 | import weibo4j.model.WeiboException;
8 | import weibo4j.org.json.JSONObject;
9 | import weibo4j.util.WeiboConfig;
10 |
11 | public class Place extends Weibo{
12 |
13 | /**
14 | *
15 | */
16 | private static final long serialVersionUID = 1L;
17 |
18 | /****************动态读取************************/
19 | /**
20 | * 注:没有写完
21 | *
22 | * 获取当前登录用户与其好友的位置动态
23 | */
24 | public StatusWapper friendsTimeLine () throws WeiboException {
25 | return Status.constructWapperStatus(client.get(WeiboConfig.getValue("baseURL") + "place/friends_timeline.json"));
26 | }
27 |
28 |
29 | /****************用户读取************************/
30 | /**
31 | * 获取LBS位置服务内的用户信息
32 | *
33 | *
34 | */
35 | public JSONObject userInfoInLBS (String uid) throws WeiboException {
36 | return client.get(WeiboConfig.getValue("baseURL") + "place/users/show.json",new PostParameter[] {
37 | new PostParameter("uid", uid)
38 | }).asJSONObject();
39 | }
40 |
41 | public JSONObject userInfoInLBS (String uid,int base_app) throws WeiboException {
42 | return client.get(WeiboConfig.getValue("baseURL") + "place/users/show.json",new PostParameter[] {
43 | new PostParameter("uid", uid),
44 | new PostParameter("base_app", base_app)
45 | }).asJSONObject();
46 | }
47 |
48 | /**
49 | * 获取用户签到过的地点列表
50 | *
51 | * 注:没有写完
52 | */
53 | public JSONObject checkinsList (String uid) throws WeiboException {
54 | return client.get(WeiboConfig.getValue("baseURL") + "place/users/checkins.json",new PostParameter[] {
55 | new PostParameter("uid", uid)
56 | }).asJSONObject();
57 | }
58 |
59 | /**
60 | * 获取用户的照片列表
61 | *
62 | * 注:没有写完
63 | */
64 |
65 | public JSONObject photoList (String uid) throws WeiboException {
66 | return client.get(WeiboConfig.getValue("baseURL") + "place/users/photos.json",new PostParameter[] {
67 | new PostParameter("uid", uid)
68 | }).asJSONObject();
69 | }
70 |
71 | /**
72 | * 获取用户的点评列表
73 | *
74 | * 注:没有写完
75 | */
76 |
77 | public JSONObject tipsList (String uid) throws WeiboException {
78 | return client.get(WeiboConfig.getValue("baseURL") + "place/users/tips.json",new PostParameter[] {
79 | new PostParameter("uid", uid)
80 | }).asJSONObject();
81 | }
82 |
83 | /****************地点读取************************/
84 | /**
85 | * 获取地点详情
86 | *
87 | *
88 | */
89 | public JSONObject poisShow (String poiid) throws WeiboException {
90 | return client.get(WeiboConfig.getValue("baseURL") + "place/pois/show.json",new PostParameter[] {
91 | new PostParameter("poiid", poiid)
92 | }).asJSONObject();
93 | }
94 |
95 | public JSONObject poisShow (String poiid,int base_app) throws WeiboException {
96 | return client.get(WeiboConfig.getValue("baseURL") + "place/pois/show.json",new PostParameter[] {
97 | new PostParameter("poiid", poiid),
98 | new PostParameter("base_app",base_app)
99 | }).asJSONObject();
100 | }
101 |
102 | /**
103 | * 获取在某个地点签到的人的列表
104 | *
105 | * 注:没写完
106 | */
107 | public JSONObject poisUsersList (String poiid) throws WeiboException {
108 | return client.get(WeiboConfig.getValue("baseURL") + "place/pois/show.json",new PostParameter[] {
109 | new PostParameter("poiid", poiid) }).asJSONObject();
110 | }
111 |
112 | /**
113 | * 获取在某个地点点评的列表
114 | *
115 | * 注:没写完
116 | */
117 | public User poisTipsList (String poiid) throws WeiboException {
118 | return new User(client.get(WeiboConfig.getValue("baseURL") + "place/pois/tips.json",new PostParameter[] {
119 | new PostParameter("poiid", poiid) }).asJSONObject());
120 | }
121 |
122 | }
123 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/org/wltea/analyzer/lucene/IKTokenizer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * IK 中文分词 版本 5.0.1
3 | * IK Analyzer release 5.0.1
4 | *
5 | * Licensed to the Apache Software Foundation (ASF) under one or more
6 | * contributor license agreements. See the NOTICE file distributed with
7 | * this work for additional information regarding copyright ownership.
8 | * The ASF licenses this file to You under the Apache License, Version 2.0
9 | * (the "License"); you may not use this file except in compliance with
10 | * the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing, software
15 | * distributed under the License is distributed on an "AS IS" BASIS,
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 | * See the License for the specific language governing permissions and
18 | * limitations under the License.
19 | *
20 | * 源代码由林良益(linliangyi2005@gmail.com)提供
21 | * 版权声明 2012,乌龙茶工作室
22 | * provided by Linliangyi and copyright 2012 by Oolong studio
23 | *
24 |
25 | *
26 | */
27 | package org.wltea.analyzer.lucene;
28 |
29 | import java.io.IOException;
30 | import java.io.Reader;
31 |
32 | import org.apache.lucene.analysis.Tokenizer;
33 | import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
34 | import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
35 | import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
36 |
37 | import org.wltea.analyzer.core.IKSegmenter;
38 | import org.wltea.analyzer.core.Lexeme;
39 |
40 | /**
41 | * IK分词器 Lucene Tokenizer适配器类
42 | * 兼容Lucene 4.0版本
43 | */
44 | public final class IKTokenizer extends Tokenizer {
45 |
46 | //IK分词器实现
47 | private IKSegmenter _IKImplement;
48 |
49 | //词元文本属性
50 | private final CharTermAttribute termAtt;
51 | //词元位移属性
52 | private final OffsetAttribute offsetAtt;
53 | //词元分类属性(该属性分类参考org.wltea.analyzer.core.Lexeme中的分类常量)
54 | private final TypeAttribute typeAtt;
55 | //记录最后一个词元的结束位置
56 | private int endPosition;
57 |
58 | /**
59 | * Lucene 4.0 Tokenizer适配器类构造函数
60 | * @param in
61 | * @param useSmart
62 | */
63 | public IKTokenizer(Reader in , boolean useSmart){
64 | super(in);
65 | offsetAtt = addAttribute(OffsetAttribute.class);
66 | termAtt = addAttribute(CharTermAttribute.class);
67 | typeAtt = addAttribute(TypeAttribute.class);
68 | _IKImplement = new IKSegmenter(input , useSmart);
69 | }
70 |
71 | /* (non-Javadoc)
72 | * @see org.apache.lucene.analysis.TokenStream#incrementToken()
73 | */
74 | @Override
75 | public boolean incrementToken() throws IOException {
76 | //清除所有的词元属性
77 | clearAttributes();
78 | Lexeme nextLexeme = _IKImplement.next();
79 | if(nextLexeme != null){
80 | //将Lexeme转成Attributes
81 | //设置词元文本
82 | termAtt.append(nextLexeme.getLexemeText());
83 | //设置词元长度
84 | termAtt.setLength(nextLexeme.getLength());
85 | //设置词元位移
86 | offsetAtt.setOffset(nextLexeme.getBeginPosition(), nextLexeme.getEndPosition());
87 | //记录分词的最后位置
88 | endPosition = nextLexeme.getEndPosition();
89 | //记录词元分类
90 | typeAtt.setType(nextLexeme.getLexemeTypeString());
91 | //返会true告知还有下个词元
92 | return true;
93 | }
94 | //返会false告知词元输出完毕
95 | return false;
96 | }
97 |
98 | /*
99 | * (non-Javadoc)
100 | * @see org.apache.lucene.analysis.Tokenizer#reset(java.io.Reader)
101 | */
102 | @Override
103 | public void reset() throws IOException {
104 | super.reset();
105 | _IKImplement.reset(input);
106 | }
107 |
108 | @Override
109 | public final void end() {
110 | // set final offset
111 | int finalOffset = correctOffset(this.endPosition);
112 | offsetAtt.setOffset(finalOffset, finalOffset);
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/Account.java:
--------------------------------------------------------------------------------
1 | package weibo4j;
2 |
3 | import java.util.List;
4 | import weibo4j.model.PostParameter;
5 | import weibo4j.model.RateLimitStatus;
6 | import weibo4j.model.School;
7 | import weibo4j.model.WeiboException;
8 | import weibo4j.org.json.JSONObject;
9 | import weibo4j.util.WeiboConfig;
10 |
11 | public class Account extends Weibo{
12 |
13 | private static final long serialVersionUID = 3816005087976772682L;
14 |
15 | /**
16 | * OAuth授权之后,获取授权用户的UID
17 | *
18 | * @return uid
19 | * @throws WeiboException
20 | * when Weibo service or network is unavailable
21 | * @version weibo4j-V2 1.0.1
22 | * @see account/get_uid
24 | * @since JDK 1.5
25 | */
26 | public JSONObject getUid() throws WeiboException {
27 | return client.get(
28 | WeiboConfig.getValue("baseURL") + "account/get_uid.json").asJSONObject();
29 | }
30 |
31 | /**
32 | * 获取当前登录用户的隐私设置
33 | *
34 | * @param uid
35 | * @return User's privacy
36 | * @throws WeiboException
37 | * when Weibo service or network is unavailable
38 | * @version weibo4j-V2 1.0.1
39 | * @see account/get_privacy
41 | * @since JDK 1.5
42 | */
43 | public JSONObject getAccountPrivacy() throws WeiboException {
44 | return client.get(
45 | WeiboConfig.getValue("baseURL") + "account/get_privacy.json")
46 | .asJSONObject();
47 | }
48 |
49 | /**
50 | * 获取所有学校列表
51 | *
52 | * @return list of school
53 | * @throws WeiboException
54 | * when Weibo service or network is unavailable
55 | * @version weibo4j-V2 1.0.1
56 | * @see account/profile/school_list
58 | * @since JDK 1.5
59 | */
60 | public List getAccountPrpfileSchoolList() throws WeiboException {
61 | return School.constructSchool(client.get(WeiboConfig
62 | .getValue("baseURL") + "account/profile/school_list.json"));
63 | }
64 |
65 | /**
66 | * 获取所有学校列表
67 | *
68 | * @param province
69 | * ,city,area,type,capital,keyword,count
70 | * @return list of school
71 | * @throws WeiboException
72 | * when Weibo service or network is unavailable
73 | * @version weibo4j-V2 1.0.1
74 | * @see account/profile/school_list
76 | * @since JDK 1.5
77 | */
78 | public List getAccountPrpfileSchoolList(Integer province,
79 | Integer city, Integer area, Integer type, String capital,
80 | String keyword, Integer count) throws WeiboException {
81 | return School.constructSchool(client.get(
82 | WeiboConfig.getValue("baseURL")
83 | + "account/profile/school_list.json",
84 | new PostParameter[] {
85 | new PostParameter("province", province.toString()),
86 | new PostParameter("city", city.toString()),
87 | new PostParameter("area", area.toString()),
88 | new PostParameter("type", type.toString()),
89 | new PostParameter("capital", capital),
90 | new PostParameter("keyword", keyword),
91 | new PostParameter("count", count.toString()) }));
92 | }
93 |
94 | /**
95 | * 获取当前登录用户的API访问频率限制情况
96 | *
97 | * @return ratelimit
98 | * @throws WeiboException
99 | * when Weibo service or network is unavailable
100 | * @version weibo4j-V2 1.0.1
101 | * @see account/rate_limit_status
103 | * @since JDK 1.5
104 | */
105 | public RateLimitStatus getAccountRateLimitStatus() throws WeiboException {
106 | return new RateLimitStatus(client.get(WeiboConfig
107 | .getValue("baseURL") + "account/rate_limit_status.json"));
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/Oauth.java:
--------------------------------------------------------------------------------
1 | package weibo4j;
2 |
3 | import java.io.IOException;
4 | import java.security.InvalidKeyException;
5 | import java.security.NoSuchAlgorithmException;
6 |
7 | import javax.crypto.Mac;
8 | import javax.crypto.SecretKey;
9 | import javax.crypto.spec.SecretKeySpec;
10 |
11 | import weibo4j.http.AccessToken;
12 | import weibo4j.http.BASE64Encoder;
13 | import weibo4j.model.PostParameter;
14 | import weibo4j.model.WeiboException;
15 | import weibo4j.org.json.JSONException;
16 | import weibo4j.org.json.JSONObject;
17 | import weibo4j.util.WeiboConfig;
18 |
19 | public class Oauth extends Weibo{
20 | /**
21 | *
22 | */
23 | private static final long serialVersionUID = 7003420545330439247L;
24 | // ----------------------------针对站内应用处理SignedRequest获取accesstoken----------------------------------------
25 | public String access_token;
26 | public String user_id;
27 |
28 | public String getToken() {
29 | return access_token;
30 | }
31 |
32 | /*
33 | * 解析站内应用post的SignedRequest split为part1和part2两部分
34 | */
35 | public String parseSignedRequest(String signed_request) throws IOException,
36 | InvalidKeyException, NoSuchAlgorithmException {
37 | String[] t = signed_request.split("\\.", 2);
38 | // 为了和 url encode/decode 不冲突,base64url 编码方式会将
39 | // '+','/'转换成'-','_',并且去掉结尾的'='。 因此解码之前需要还原到默认的base64编码,结尾的'='可以用以下算法还原
40 | int padding = (4 - t[0].length() % 4);
41 | for (int i = 0; i < padding; i++)
42 | t[0] += "=";
43 | String part1 = t[0].replace("-", "+").replace("_", "/");
44 |
45 | SecretKey key = new SecretKeySpec(WeiboConfig
46 | .getValue("client_SERCRET").getBytes(), "hmacSHA256");
47 | Mac m;
48 | m = Mac.getInstance("hmacSHA256");
49 | m.init(key);
50 | m.update(t[1].getBytes());
51 | String part1Expect = BASE64Encoder.encode(m.doFinal());
52 |
53 | sun.misc.BASE64Decoder decode = new sun.misc.BASE64Decoder();
54 | String s = new String(decode.decodeBuffer(t[1]));
55 | if (part1.equals(part1Expect)) {
56 | return ts(s);
57 | } else {
58 | return null;
59 | }
60 | }
61 |
62 | /*
63 | * 处理解析后的json解析
64 | */
65 | public String ts(String json) {
66 | try {
67 | JSONObject jsonObject = new JSONObject(json);
68 | access_token = jsonObject.getString("oauth_token");
69 | user_id = jsonObject.getString("user_id");
70 | } catch (JSONException e) {
71 | e.printStackTrace();
72 | }
73 | return access_token;
74 |
75 | }
76 |
77 | /*----------------------------Oauth接口--------------------------------------*/
78 |
79 | public AccessToken getAccessTokenByCode(String code) throws WeiboException {
80 | return new AccessToken(client.post(
81 | WeiboConfig.getValue("accessTokenURL"),
82 | new PostParameter[] {
83 | new PostParameter("client_id", WeiboConfig
84 | .getValue("client_ID")),
85 | new PostParameter("client_secret", WeiboConfig
86 | .getValue("client_SERCRET")),
87 | new PostParameter("grant_type", "authorization_code"),
88 | new PostParameter("code", code),
89 | new PostParameter("redirect_uri", WeiboConfig
90 | .getValue("redirect_URI")) }, false));
91 | }
92 |
93 | public String authorize(String response_type,String state) throws WeiboException {
94 | return WeiboConfig.getValue("authorizeURL").trim() + "?client_id="
95 | + WeiboConfig.getValue("client_ID").trim() + "&redirect_uri="
96 | + WeiboConfig.getValue("redirect_URI").trim()
97 | + "&response_type=" + response_type
98 | + "&state="+state;
99 | }
100 | public String authorize(String response_type,String state,String scope) throws WeiboException {
101 | return WeiboConfig.getValue("authorizeURL").trim() + "?client_id="
102 | + WeiboConfig.getValue("client_ID").trim() + "&redirect_uri="
103 | + WeiboConfig.getValue("redirect_URI").trim()
104 | + "&response_type=" + response_type
105 | + "&state="+state
106 | + "&scope="+scope;
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/Search.java:
--------------------------------------------------------------------------------
1 | package weibo4j;
2 |
3 | import weibo4j.model.PostParameter;
4 | import weibo4j.model.WeiboException;
5 | import weibo4j.org.json.JSONArray;
6 | import weibo4j.util.WeiboConfig;
7 |
8 | public class Search extends Weibo{
9 | //---------------------------------搜索接口-----------------------------------------------
10 |
11 | /**
12 | *
13 | */
14 | private static final long serialVersionUID = 1060145395982699914L;
15 | public JSONArray searchSuggestionsUsers(String q) throws WeiboException{
16 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/users.json",new PostParameter[]{
17 | new PostParameter("q", q)
18 | }).asJSONArray();
19 | }
20 | public JSONArray searchSuggestionsUsers(String q,int count) throws WeiboException{
21 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/users.json",new PostParameter[]{
22 | new PostParameter("q", q),
23 | new PostParameter("count", count)
24 | }).asJSONArray();
25 | }
26 | public JSONArray searchSuggestionsStatuses(String q) throws WeiboException{
27 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/statuses.json",new PostParameter[]{
28 | new PostParameter("q", q)
29 | }).asJSONArray();
30 | }
31 | public JSONArray searchSuggestionsStatuses(String q,int count) throws WeiboException{
32 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/statuses.json",new PostParameter[]{
33 | new PostParameter("q", q),
34 | new PostParameter("count", count)
35 | }).asJSONArray();
36 | }
37 | public JSONArray searchSuggestionsCompanies(String q) throws WeiboException{
38 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/companies.json",new PostParameter[]{
39 | new PostParameter("q", q)
40 | }).asJSONArray();
41 | }
42 | public JSONArray searchSuggestionsCompanies(String q,int count) throws WeiboException{
43 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/companies.json",new PostParameter[]{
44 | new PostParameter("q", q),
45 | new PostParameter("count", count)
46 | }).asJSONArray();
47 | }
48 | public JSONArray searchSuggestionsApps(String q) throws WeiboException{
49 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/apps.json",new PostParameter[]{
50 | new PostParameter("q", q)
51 | }).asJSONArray();
52 | }
53 | public JSONArray searchSuggestionsApps(String q,int count) throws WeiboException{
54 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/apps.json",new PostParameter[]{
55 | new PostParameter("q", q),
56 | new PostParameter("count", count)
57 | }).asJSONArray();
58 | }
59 | public JSONArray searchSuggestionsSchools(String q) throws WeiboException{
60 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/schools.json",new PostParameter[]{
61 | new PostParameter("q", q)
62 | }).asJSONArray();
63 | }
64 | public JSONArray searchSuggestionsSchools(String q,int count,int type) throws WeiboException{
65 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/schools.json",new PostParameter[]{
66 | new PostParameter("q", q),
67 | new PostParameter("count", count),
68 | new PostParameter("type", type)
69 | }).asJSONArray();
70 | }
71 | public JSONArray searchSuggestionsAt_users(String q,int type) throws WeiboException{
72 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/at_users.json",new PostParameter[]{
73 | new PostParameter("q", q),
74 | new PostParameter("type", type)
75 | }).asJSONArray();
76 | }
77 | public JSONArray searchSuggestionsAt_users(String q,int count,int type,int range) throws WeiboException{
78 | return client.get(WeiboConfig.getValue("baseURL")+"search/suggestions/at_users.json",new PostParameter[]{
79 | new PostParameter("q", q),
80 | new PostParameter("count", count),
81 | new PostParameter("type", type),
82 | new PostParameter("range",range)
83 | }).asJSONArray();
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/model/Trend.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2007-2009, Yusuke Yamamoto
3 | All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without
6 | modification, are permitted provided that the following conditions are met:
7 | * Redistributions of source code must retain the above copyright
8 | notice, this list of conditions and the following disclaimer.
9 | * Redistributions in binary form must reproduce the above copyright
10 | notice, this list of conditions and the following disclaimer in the
11 | documentation and/or other materials provided with the distribution.
12 | * Neither the name of the Yusuke Yamamoto nor the
13 | names of its contributors may be used to endorse or promote products
14 | derived from this software without specific prior written permission.
15 |
16 | THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
17 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 | DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 | */
27 | package weibo4j.model;
28 |
29 | import weibo4j.org.json.JSONException;
30 | import weibo4j.org.json.JSONObject;
31 |
32 | /**
33 | * A data class representing Treand.
34 | *
35 | * @author Yusuke Yamamoto - yusuke at mac.com
36 | * @since Weibo4J 1.2.1
37 | */
38 | public class Trend implements java.io.Serializable{
39 | private String name;
40 | private String query = null;
41 | private long amount;
42 | private long delta;
43 | private static final long serialVersionUID = 1925956704460743946L;
44 |
45 | public Trend(JSONObject json) throws JSONException {
46 | this.name = json.getString("name");
47 | if (!json.isNull("query")) {
48 | this.query = json.getString("query");
49 | }
50 | this.amount =json.getLong("amount");
51 | this.delta = json.getLong("delta");
52 | }
53 |
54 | public String getName() {
55 | return name;
56 | }
57 |
58 |
59 | public String getQuery() {
60 | return query;
61 | }
62 |
63 | public long getAmount() {
64 | return amount;
65 | }
66 |
67 | public void setAmount(long amount) {
68 | this.amount = amount;
69 | }
70 |
71 | public long getDelta() {
72 | return delta;
73 | }
74 |
75 | public void setDelta(long delta) {
76 | this.delta = delta;
77 | }
78 |
79 | public void setName(String name) {
80 | this.name = name;
81 | }
82 |
83 | public void setQuery(String query) {
84 | this.query = query;
85 | }
86 |
87 | @Override
88 | public boolean equals(Object o) {
89 | if (this == o) return true;
90 | if (!(o instanceof Trend)) return false;
91 |
92 | Trend trend = (Trend) o;
93 |
94 | if (!name.equals(trend.name)) return false;
95 | if (query != null ? !query.equals(trend.query) : trend.query != null)
96 | return false;
97 |
98 | return true;
99 | }
100 |
101 | @Override
102 | public int hashCode() {
103 | int result = name.hashCode();
104 | result = 31 * result + (query != null ? query.hashCode() : 0);
105 | return result;
106 | }
107 |
108 | @Override
109 | public String toString() {
110 | return "Trend [name=" + name + ", query=" + query + ", amount="
111 | + amount + ", delta=" + delta + "]";
112 | }
113 |
114 | }
115 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/org/wltea/analyzer/core/CJKSegmenter.java:
--------------------------------------------------------------------------------
1 |
2 | /**
3 | * IK 中文分词 版本 5.0
4 | * IK Analyzer release 5.0
5 | *
6 | * Licensed to the Apache Software Foundation (ASF) under one or more
7 | * contributor license agreements. See the NOTICE file distributed with
8 | * this work for additional information regarding copyright ownership.
9 | * The ASF licenses this file to You under the Apache License, Version 2.0
10 | * (the "License"); you may not use this file except in compliance with
11 | * the License. You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | *
21 | * 源代码由林良益(linliangyi2005@gmail.com)提供
22 | * 版权声明 2012,乌龙茶工作室
23 | * provided by Linliangyi and copyright 2012 by Oolong studio
24 | *
25 | */
26 | package org.wltea.analyzer.core;
27 |
28 | import java.util.LinkedList;
29 | import java.util.List;
30 |
31 | import org.wltea.analyzer.dic.Dictionary;
32 | import org.wltea.analyzer.dic.Hit;
33 |
34 |
35 | /**
36 | * 中文-日韩文子分词器
37 | */
38 | class CJKSegmenter implements ISegmenter {
39 |
40 | //子分词器标签
41 | static final String SEGMENTER_NAME = "CJK_SEGMENTER";
42 | //待处理的分词hit队列
43 | private List tmpHits;
44 |
45 |
46 | CJKSegmenter(){
47 | this.tmpHits = new LinkedList();
48 | }
49 |
50 | /* (non-Javadoc)
51 | * @see org.wltea.analyzer.core.ISegmenter#analyze(org.wltea.analyzer.core.AnalyzeContext)
52 | */
53 | public void analyze(AnalyzeContext context) {
54 | if(CharacterUtil.CHAR_USELESS != context.getCurrentCharType()){
55 |
56 | //优先处理tmpHits中的hit
57 | if(!this.tmpHits.isEmpty()){
58 | //处理词段队列
59 | Hit[] tmpArray = this.tmpHits.toArray(new Hit[this.tmpHits.size()]);
60 | for(Hit hit : tmpArray){
61 | hit = Dictionary.getSingleton().matchWithHit(context.getSegmentBuff(), context.getCursor() , hit);
62 | if(hit.isMatch()){
63 | //输出当前的词
64 | Lexeme newLexeme = new Lexeme(context.getBufferOffset() , hit.getBegin() , context.getCursor() - hit.getBegin() + 1 , Lexeme.TYPE_CNWORD);
65 | context.addLexeme(newLexeme);
66 |
67 | if(!hit.isPrefix()){//不是词前缀,hit不需要继续匹配,移除
68 | this.tmpHits.remove(hit);
69 | }
70 |
71 | }else if(hit.isUnmatch()){
72 | //hit不是词,移除
73 | this.tmpHits.remove(hit);
74 | }
75 | }
76 | }
77 |
78 | //*********************************
79 | //再对当前指针位置的字符进行单字匹配
80 | Hit singleCharHit = Dictionary.getSingleton().matchInMainDict(context.getSegmentBuff(), context.getCursor(), 1);
81 | if(singleCharHit.isMatch()){//首字成词
82 | //输出当前的词
83 | Lexeme newLexeme = new Lexeme(context.getBufferOffset() , context.getCursor() , 1 , Lexeme.TYPE_CNWORD);
84 | context.addLexeme(newLexeme);
85 |
86 | //同时也是词前缀
87 | if(singleCharHit.isPrefix()){
88 | //前缀匹配则放入hit列表
89 | this.tmpHits.add(singleCharHit);
90 | }
91 | }else if(singleCharHit.isPrefix()){//首字为词前缀
92 | //前缀匹配则放入hit列表
93 | this.tmpHits.add(singleCharHit);
94 | }
95 |
96 |
97 | }else{
98 | //遇到CHAR_USELESS字符
99 | //清空队列
100 | this.tmpHits.clear();
101 | }
102 |
103 | //判断缓冲区是否已经读完
104 | if(context.isBufferConsumed()){
105 | //清空队列
106 | this.tmpHits.clear();
107 | }
108 |
109 | //判断是否锁定缓冲区
110 | if(this.tmpHits.size() == 0){
111 | context.unlockBuffer(SEGMENTER_NAME);
112 |
113 | }else{
114 | context.lockBuffer(SEGMENTER_NAME);
115 | }
116 | }
117 |
118 | /* (non-Javadoc)
119 | * @see org.wltea.analyzer.core.ISegmenter#reset()
120 | */
121 | public void reset() {
122 | //清空队列
123 | this.tmpHits.clear();
124 | }
125 |
126 | }
127 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/model/Favorites.java:
--------------------------------------------------------------------------------
1 | package weibo4j.model;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Date;
5 | import java.util.List;
6 |
7 | import weibo4j.http.Response;
8 | import weibo4j.org.json.JSONArray;
9 | import weibo4j.org.json.JSONException;
10 | import weibo4j.org.json.JSONObject;
11 |
12 | public class Favorites extends WeiboResponse implements java.io.Serializable{
13 |
14 | private static final long serialVersionUID = 3355536191107298448L;
15 | private Date favoritedTime; //添加收藏的时间
16 | private Status status; //收藏的status
17 | private List tags; //收藏的tags
18 | private static int totalNumber;
19 | public Favorites(Response res) throws WeiboException{
20 | super(res);
21 | JSONObject json = null;
22 | try {
23 | json = res.asJSONObject();
24 | favoritedTime = parseDate(json.getString("favorited_time"), "EEE MMM dd HH:mm:ss z yyyy");
25 | if(!json.isNull("status")){
26 | status = new Status(json.getJSONObject("status"));
27 | }
28 | if(!json.isNull("tags")){
29 | JSONArray list = json.getJSONArray("tags");
30 | int size = list.length();
31 | List tag = new ArrayList(size);
32 | for (int i = 0;i< size;i++){
33 | tag.add(new FavoritesTag(list.getJSONObject(i)));
34 | }
35 | }
36 | } catch (JSONException je) {
37 | throw new WeiboException(je.getMessage() + ":" + json.toString(), je);
38 | }
39 | }
40 | Favorites(JSONObject json) throws WeiboException, JSONException{
41 | favoritedTime = parseDate(json.getString("favorited_time"), "EEE MMM dd HH:mm:ss z yyyy");
42 | if(!json.isNull("status")){
43 | status = new Status(json.getJSONObject("status"));
44 | }
45 | if(!json.isNull("tags")){
46 | JSONArray list = json.getJSONArray("tags");
47 | int size = list.length();
48 | tags = new ArrayList(size);
49 | for (int i = 0;i< size;i++){
50 | tags.add(new FavoritesTag(list.getJSONObject(i)));
51 | }
52 | }
53 |
54 | }
55 | public static List constructFavorites(Response res) throws WeiboException{
56 | try {
57 | JSONArray list = res.asJSONObject().getJSONArray("favorites");
58 | int size = list.length();
59 | List favorites = new ArrayList(size);
60 | for (int i = 0; i < size; i++) {
61 | favorites.add(new Favorites(list.getJSONObject(i)));
62 | }
63 | totalNumber =res.asJSONObject().getInt("total_number");
64 | return favorites;
65 | } catch (JSONException jsone) {
66 | throw new WeiboException(jsone);
67 | }
68 | }
69 |
70 | public Status getStatus() {
71 | return status;
72 | }
73 |
74 | public void setStatus(Status status) {
75 | this.status = status;
76 | }
77 |
78 | public List getTags() {
79 | return tags;
80 | }
81 |
82 | public void setTags(List tags) {
83 | this.tags = tags;
84 | }
85 |
86 | public Date getFavoritedTime() {
87 | return favoritedTime;
88 | }
89 |
90 | public void setFavoritedTime(Date favoritedTime) {
91 | this.favoritedTime = favoritedTime;
92 | }
93 |
94 | @Override
95 | public int hashCode() {
96 | final int prime = 31;
97 | int result = 1;
98 | result = prime * result
99 | + ((favoritedTime == null) ? 0 : favoritedTime.hashCode());
100 | return result;
101 | }
102 | @Override
103 | public boolean equals(Object obj) {
104 | if (this == obj)
105 | return true;
106 | if (obj == null)
107 | return false;
108 | if (getClass() != obj.getClass())
109 | return false;
110 | Favorites other = (Favorites) obj;
111 | if (favoritedTime == null) {
112 | if (other.favoritedTime != null)
113 | return false;
114 | } else if (!favoritedTime.equals(other.favoritedTime))
115 | return false;
116 | return true;
117 | }
118 | @Override
119 | public String toString() {
120 | return "Favorites [" +
121 | "favorited_time=" + favoritedTime +
122 | ", status=" + status.toString() +
123 | ", FavoritesTag=" + ((tags==null)?"null":tags.toString()) +
124 | ", total_number = "+totalNumber+
125 | "]";
126 | }
127 |
128 | }
129 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/model/MySSLSocketFactory.java:
--------------------------------------------------------------------------------
1 | package weibo4j.model;
2 |
3 | import java.io.IOException;
4 | import java.net.InetAddress;
5 | import java.net.InetSocketAddress;
6 | import java.net.Socket;
7 | import java.net.SocketAddress;
8 | import java.net.UnknownHostException;
9 | import java.security.KeyManagementException;
10 | import java.security.NoSuchAlgorithmException;
11 | import java.security.cert.CertificateException;
12 | import java.security.cert.X509Certificate;
13 |
14 | import javax.net.SocketFactory;
15 | import javax.net.ssl.SSLContext;
16 | import javax.net.ssl.TrustManager;
17 | import javax.net.ssl.X509TrustManager;
18 |
19 | import org.apache.commons.httpclient.ConnectTimeoutException;
20 | import org.apache.commons.httpclient.params.HttpConnectionParams;
21 | import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
22 |
23 | /**
24 | Provide a custom socket factory that implements org.apache.commons.httpclient.protocol.ProtocolSocketFactory interface.
25 | The socket factory is responsible for opening a socket to the target server using either the standard or a third party
26 | SSL library and performing any required initialization such as performing the connection handshake. Generally the initialization
27 | is performed automatically when the socket is created.
28 | @author sinaWeibo
29 | */
30 | public class MySSLSocketFactory implements ProtocolSocketFactory {
31 | private SSLContext sslcontext = null;
32 | private SSLContext createSSLContext() {
33 | SSLContext sslcontext = null;
34 | try {
35 | sslcontext = SSLContext.getInstance("SSL");
36 | sslcontext.init(null,
37 | new TrustManager[] { new TrustAnyTrustManager() },
38 | new java.security.SecureRandom());
39 | } catch (NoSuchAlgorithmException e) {
40 | e.printStackTrace();
41 | } catch (KeyManagementException e) {
42 | e.printStackTrace();
43 | }
44 | return sslcontext;
45 | }
46 |
47 | private SSLContext getSSLContext() {
48 | if (this.sslcontext == null) {
49 | this.sslcontext = createSSLContext();
50 | }
51 | return this.sslcontext;
52 | }
53 |
54 | public Socket createSocket(Socket socket, String host, int port,
55 | boolean autoClose) throws IOException, UnknownHostException {
56 | return getSSLContext().getSocketFactory().createSocket(socket, host,
57 | port, autoClose);
58 | }
59 |
60 | public Socket createSocket(String host, int port) throws IOException,
61 | UnknownHostException {
62 | return getSSLContext().getSocketFactory().createSocket(host, port);
63 | }
64 |
65 | public Socket createSocket(String host, int port, InetAddress clientHost,
66 | int clientPort) throws IOException, UnknownHostException {
67 | return getSSLContext().getSocketFactory().createSocket(host, port,
68 | clientHost, clientPort);
69 | }
70 |
71 | public Socket createSocket(String host, int port, InetAddress localAddress,
72 | int localPort, HttpConnectionParams params) throws IOException,
73 | UnknownHostException, ConnectTimeoutException {
74 | if (params == null) {
75 | throw new IllegalArgumentException("Parameters may not be null");
76 | }
77 | int timeout = params.getConnectionTimeout();
78 | SocketFactory socketfactory = getSSLContext().getSocketFactory();
79 | if (timeout == 0) {
80 | return socketfactory.createSocket(host, port, localAddress,
81 | localPort);
82 | } else {
83 | Socket socket = socketfactory.createSocket();
84 | SocketAddress localaddr = new InetSocketAddress(localAddress,
85 | localPort);
86 | SocketAddress remoteaddr = new InetSocketAddress(host, port);
87 | socket.bind(localaddr);
88 | socket.connect(remoteaddr, timeout);
89 | return socket;
90 | }
91 | }
92 |
93 | private static class TrustAnyTrustManager implements X509TrustManager {
94 | public void checkClientTrusted(X509Certificate[] chain, String authType)
95 | throws CertificateException {
96 | }
97 |
98 | public void checkServerTrusted(X509Certificate[] chain, String authType)
99 | throws CertificateException {
100 | }
101 |
102 | public X509Certificate[] getAcceptedIssuers() {
103 | return new X509Certificate[] {};
104 | }
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/model/Emotion.java:
--------------------------------------------------------------------------------
1 | package weibo4j.model;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import weibo4j.http.Response;
7 | import weibo4j.org.json.JSONArray;
8 | import weibo4j.org.json.JSONException;
9 | import weibo4j.org.json.JSONObject;
10 |
11 | /**
12 | * @author SinaWeibo
13 | *
14 | */
15 | public class Emotion extends WeiboResponse {
16 | private static final long serialVersionUID = -4096813631691846494L;
17 | private String phrase; // 表情使用的替代文字
18 | private String type; // 表情类型,image为普通图片表情,magic为魔法表情
19 | private String url; // 表情图片存放的位置
20 | private boolean hot; // 是否为热门表情
21 | private boolean common; // 是否是常用表情
22 | private String value;
23 | private String category; // 表情分类
24 | private String picid;
25 | private String icon;
26 |
27 | public Emotion(Response res) throws WeiboException {
28 | super(res);
29 | JSONObject json = res.asJSONObject();
30 | try {
31 | phrase = json.getString("phrase");
32 | type = json.getString("type");
33 | url = json.getString("url");
34 | hot = json.getBoolean("hot");
35 | category = json.getString("category");
36 | common = json.getBoolean("common");
37 | value = json.getString("value");
38 | picid = json.getString("picid");
39 | icon = json.getString("icon");
40 | } catch (JSONException je) {
41 | throw new WeiboException(je.getMessage() + ":" + json.toString(),
42 | je);
43 | }
44 | }
45 |
46 | public Emotion(JSONObject json) throws WeiboException {
47 | try {
48 | phrase = json.getString("phrase");
49 | type = json.getString("type");
50 | url = json.getString("url");
51 | hot = json.getBoolean("hot");
52 | category = json.getString("category");
53 | common = json.getBoolean("common");
54 | value = json.getString("value");
55 | picid = json.getString("picid");
56 | icon = json.getString("icon");
57 | } catch (JSONException je) {
58 | throw new WeiboException(je.getMessage() + ":" + json.toString(),
59 | je);
60 | }
61 | }
62 |
63 | public static List constructEmotions(Response res)
64 | throws WeiboException {
65 | try {
66 | JSONArray list = res.asJSONArray();
67 | int size = list.length();
68 | List emotions = new ArrayList(size);
69 | for (int i = 0; i < size; i++) {
70 | emotions.add(new Emotion(list.getJSONObject(i)));
71 | }
72 | return emotions;
73 | } catch (JSONException jsone) {
74 | throw new WeiboException(jsone);
75 | } catch (WeiboException te) {
76 | throw te;
77 | }
78 |
79 | }
80 |
81 | public Emotion() {
82 | super();
83 | }
84 |
85 | public String getPhrase() {
86 | return phrase;
87 | }
88 |
89 | public void setPhrase(String phrase) {
90 | this.phrase = phrase;
91 | }
92 |
93 | public String getType() {
94 | return type;
95 | }
96 |
97 | public void setType(String type) {
98 | this.type = type;
99 | }
100 |
101 | public String getUrl() {
102 | return url;
103 | }
104 |
105 | public void setUrl(String url) {
106 | this.url = url;
107 | }
108 |
109 | public String getCategory() {
110 | return category;
111 | }
112 |
113 | public void setCategory(String category) {
114 | this.category = category;
115 | }
116 |
117 | public boolean isHot() {
118 | return hot;
119 | }
120 |
121 | public void setHot(boolean hot) {
122 | this.hot = hot;
123 | }
124 |
125 | public boolean isCommon() {
126 | return common;
127 | }
128 |
129 | public void setCommon(boolean common) {
130 | this.common = common;
131 | }
132 |
133 | public String getValue() {
134 | return value;
135 | }
136 |
137 | public void setValue(String value) {
138 | this.value = value;
139 | }
140 |
141 | public String getPicid() {
142 | return picid;
143 | }
144 |
145 | public void setPicid(String picid) {
146 | this.picid = picid;
147 | }
148 |
149 | public String getIcon() {
150 | return icon;
151 | }
152 |
153 | public void setIcon(String icon) {
154 | this.icon = icon;
155 | }
156 |
157 | @Override
158 | public String toString() {
159 | return "Emotion [phrase=" + phrase + ", type=" + type + ", url=" + url
160 | + ", hot=" + hot + ", common=" + common + ", value=" + value
161 | + ", category=" + category + ", picid=" + picid + ", icon="
162 | + icon + "]";
163 | }
164 |
165 | }
166 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/http/BASE64Encoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2007-2009, Yusuke Yamamoto
3 | All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without
6 | modification, are permitted provided that the following conditions are met:
7 | * Redistributions of source code must retain the above copyright
8 | notice, this list of conditions and the following disclaimer.
9 | * Redistributions in binary form must reproduce the above copyright
10 | notice, this list of conditions and the following disclaimer in the
11 | documentation and/or other materials provided with the distribution.
12 | * Neither the name of the Yusuke Yamamoto nor the
13 | names of its contributors may be used to endorse or promote products
14 | derived from this software without specific prior written permission.
15 |
16 | THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
17 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 | DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 | */
27 | package weibo4j.http;
28 | /**
29 | * A utility class encodes byte array into String using Base64 encoding scheme.
30 | * @see weibo4j.http.HttpClient
31 | * @author Yusuke Yamamoto - yusuke at mac.com
32 | */
33 | public class BASE64Encoder {
34 | private static final char last2byte = (char) Integer.parseInt("00000011", 2);
35 | private static final char last4byte = (char) Integer.parseInt("00001111", 2);
36 | private static final char last6byte = (char) Integer.parseInt("00111111", 2);
37 | private static final char lead6byte = (char) Integer.parseInt("11111100", 2);
38 | private static final char lead4byte = (char) Integer.parseInt("11110000", 2);
39 | private static final char lead2byte = (char) Integer.parseInt("11000000", 2);
40 | private static final char[] encodeTable = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
41 |
42 | public BASE64Encoder() {
43 | }
44 |
45 | public static String encode(byte[] from) {
46 | StringBuffer to = new StringBuffer((int) (from.length * 1.34) + 3);
47 | int num = 0;
48 | char currentByte = 0;
49 | for (int i = 0; i < from.length; i++) {
50 | num = num % 8;
51 | while (num < 8) {
52 | switch (num) {
53 | case 0:
54 | currentByte = (char) (from[i] & lead6byte);
55 | currentByte = (char) (currentByte >>> 2);
56 | break;
57 | case 2:
58 | currentByte = (char) (from[i] & last6byte);
59 | break;
60 | case 4:
61 | currentByte = (char) (from[i] & last4byte);
62 | currentByte = (char) (currentByte << 2);
63 | if ((i + 1) < from.length) {
64 | currentByte |= (from[i + 1] & lead2byte) >>> 6;
65 | }
66 | break;
67 | case 6:
68 | currentByte = (char) (from[i] & last2byte);
69 | currentByte = (char) (currentByte << 4);
70 | if ((i + 1) < from.length) {
71 | currentByte |= (from[i + 1] & lead4byte) >>> 4;
72 | }
73 | break;
74 | }
75 | to.append(encodeTable[currentByte]);
76 | num += 6;
77 | }
78 | }
79 | if (to.length() % 4 != 0) {
80 | for (int i = 4 - to.length() % 4; i > 0; i--) {
81 | to.append("=");
82 | }
83 | }
84 | return to.toString();
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/model/Tag.java:
--------------------------------------------------------------------------------
1 | package weibo4j.model;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Iterator;
5 | import java.util.List;
6 | import weibo4j.Weibo;
7 | import weibo4j.http.Response;
8 | import weibo4j.org.json.JSONArray;
9 | import weibo4j.org.json.JSONException;
10 | import weibo4j.org.json.JSONObject;
11 |
12 | /**
13 | * @author sinaWeibo
14 | *
15 | */
16 | public class Tag extends WeiboResponse implements java.io.Serializable {
17 |
18 | private static final long serialVersionUID = 2177657076940291492L;
19 |
20 | private String id; //标签id
21 |
22 | private String value; //标签value
23 |
24 | private String weight;
25 |
26 | public Tag(JSONObject json) throws WeiboException, JSONException {
27 | if (!json.getString("id").isEmpty()) {
28 | id = json.getString("id");
29 | }
30 | if(!json.getString("value").isEmpty()) {
31 | value = json.getString("value");
32 | }else {
33 | Iterator keys = json.sortedKeys();
34 | if (keys.hasNext()) {
35 | id = keys.next();
36 | value = json.getString(id);
37 | }
38 | }
39 | weight= json.getString("weight");
40 | }
41 | public Tag(JSONObject json , Weibo weibo) throws WeiboException,JSONException {
42 | System.out.println(json);
43 | id = json.getString("id");
44 | value = json.getString("count");
45 | weight= json.getString("weight");
46 | }
47 |
48 |
49 | public static List constructTags(Response res) throws WeiboException {
50 | try {
51 | JSONArray list = res.asJSONArray();
52 | int size = list.length();
53 | List tags = new ArrayList(size);
54 | for (int i = 0; i < size; i++) {
55 | tags.add(new Tag(list.getJSONObject(i)));
56 | }
57 | return tags;
58 | } catch (JSONException jsone) {
59 | throw new WeiboException(jsone);
60 | } catch (WeiboException te) {
61 | throw te;
62 | }
63 | }
64 | public static TagWapper constructTagWapper(Response res){
65 | try {
66 | JSONArray tags = res.asJSONArray();
67 | List tagList = new ArrayList();
68 | for(int i=0;i constructTag(Response res) throws WeiboException {
81 | try {
82 | JSONArray list = res.asJSONObject().getJSONArray("tags");
83 | int size = list.length();
84 | List tags = new ArrayList(size);
85 | for (int i = 0; i < size; i++) {
86 | tags.add(new FavoritesTag(list.getJSONObject(i)));
87 | }
88 | return tags;
89 | } catch (JSONException jsone) {
90 | throw new WeiboException(jsone);
91 | } catch (WeiboException te) {
92 | throw te;
93 | }
94 | }
95 |
96 | @Override
97 | public int hashCode() {
98 | final int prime = 31;
99 | int result = 1;
100 | result = prime * result + ((id == null) ? 0 : id.hashCode());
101 | result = prime * result + ((value == null) ? 0 : value.hashCode());
102 | return result;
103 | }
104 |
105 | @Override
106 | public boolean equals(Object obj) {
107 | if (this == obj)
108 | return true;
109 | if (obj == null)
110 | return false;
111 | if (getClass() != obj.getClass())
112 | return false;
113 | Tag other = (Tag) obj;
114 | if (id == null) {
115 | if (other.id != null)
116 | return false;
117 | } else if (!id.equals(other.id))
118 | return false;
119 | if (value == null) {
120 | if (other.value != null)
121 | return false;
122 | } else if (!value.equals(other.value))
123 | return false;
124 | return true;
125 | }
126 |
127 | /**
128 | * @return the id
129 | */
130 | public String getId() {
131 | return id;
132 | }
133 |
134 | public String getWeight() {
135 | return weight;
136 | }
137 | public void setWeight(String weight) {
138 | this.weight = weight;
139 | }
140 | public void setId(String id) {
141 | this.id = id;
142 | }
143 | public void setValue(String value) {
144 | this.value = value;
145 | }
146 | /**
147 | * @return the value
148 | */
149 | public String getValue() {
150 | return value;
151 | }
152 | @Override
153 | public String toString() {
154 | return "Tag [id=" + id + ", value=" + value + ", weight=" + weight
155 | + "]";
156 | }
157 |
158 | }
159 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/model/IDs.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2007-2009, Yusuke Yamamoto
3 | All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without
6 | modification, are permitted provided that the following conditions are met:
7 | * Redistributions of source code must retain the above copyright
8 | notice, this list of conditions and the following disclaimer.
9 | * Redistributions in binary form must reproduce the above copyright
10 | notice, this list of conditions and the following disclaimer in the
11 | documentation and/or other materials provided with the distribution.
12 | * Neither the name of the Yusuke Yamamoto nor the
13 | names of its contributors may be used to endorse or promote products
14 | derived from this software without specific prior written permission.
15 |
16 | THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
17 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 | DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 | */
27 | package weibo4j.model;
28 |
29 | import java.util.Arrays;
30 |
31 | import weibo4j.Weibo;
32 | import weibo4j.http.Response;
33 | import weibo4j.org.json.JSONArray;
34 | import weibo4j.org.json.JSONException;
35 | import weibo4j.org.json.JSONObject;
36 |
37 | /**
38 | * A data class representing array of numeric IDs.
39 | *
40 | * @author Yusuke Yamamoto - yusuke at mac.com
41 | * @editor SinaWeibo
42 | */
43 | public class IDs extends WeiboResponse {
44 | private long[] ids; //ids
45 | private long previousCursor; //当前cursor
46 | private long nextCursor; //下一个cursor
47 | private static final long serialVersionUID = -6585026560164704953L;
48 | /*package*/ IDs(Response res,Weibo w) throws WeiboException {
49 | super(res);
50 | if("[]\n".equals(res.asString())){
51 | previousCursor=0;
52 | nextCursor=0;
53 | ids= new long[0];
54 | return;
55 | }
56 | JSONObject json= res.asJSONObject();
57 | try {
58 | previousCursor = json.getLong("previous_cursor");
59 | nextCursor = json.getLong("next_cursor");
60 |
61 | if(!json.isNull("ids")){
62 | JSONArray jsona= json.getJSONArray("ids");
63 | int size=jsona.length();
64 | ids =new long[ size];
65 | for (int i = 0; i < size; i++) {
66 | ids[i] =jsona.getLong(i);
67 | }
68 | }
69 |
70 | } catch (JSONException jsone) {
71 | throw new WeiboException(jsone);
72 | }
73 |
74 | }
75 |
76 | public long[] getIDs() {
77 | return ids;
78 | }
79 |
80 | /**
81 | *
82 | * @since weibo4j-V2 1.0.0
83 | */
84 | public boolean hasPrevious(){
85 | return 0 != previousCursor;
86 | }
87 |
88 | /**
89 | *
90 | * @since weibo4j-V2 1.0.0
91 | */
92 | public long getPreviousCursor() {
93 | return previousCursor;
94 | }
95 |
96 | /**
97 | *
98 | * @since weibo4j-V2 1.0.0
99 | */
100 | public boolean hasNext(){
101 | return 0 != nextCursor;
102 | }
103 |
104 | /**
105 | *
106 | * @since weibo4j-V2 1.0.0
107 | */
108 | public long getNextCursor() {
109 | return nextCursor;
110 | }
111 |
112 | @Override
113 | public boolean equals(Object o) {
114 | if (this == o) return true;
115 | if (!(o instanceof IDs)) return false;
116 |
117 | IDs iDs = (IDs) o;
118 |
119 | if (!Arrays.equals(ids, iDs.ids)) return false;
120 |
121 | return true;
122 | }
123 |
124 | @Override
125 | public int hashCode() {
126 | return ids != null ? Arrays.hashCode(ids) : 0;
127 | }
128 |
129 | @Override
130 | public String toString() {
131 | return "IDs{" +
132 | "ids=" + ids +
133 | ", previousCursor=" + previousCursor +
134 | ", nextCursor=" + nextCursor +
135 | '}';
136 | }
137 | }
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/org/wltea/analyzer/core/IKArbitrator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * IK 中文分词 版本 5.0
3 | * IK Analyzer release 5.0
4 | *
5 | * Licensed to the Apache Software Foundation (ASF) under one or more
6 | * contributor license agreements. See the NOTICE file distributed with
7 | * this work for additional information regarding copyright ownership.
8 | * The ASF licenses this file to You under the Apache License, Version 2.0
9 | * (the "License"); you may not use this file except in compliance with
10 | * the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing, software
15 | * distributed under the License is distributed on an "AS IS" BASIS,
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 | * See the License for the specific language governing permissions and
18 | * limitations under the License.
19 | *
20 | * 源代码由林良益(linliangyi2005@gmail.com)提供
21 | * 版权声明 2012,乌龙茶工作室
22 | * provided by Linliangyi and copyright 2012 by Oolong studio
23 | *
24 | */
25 | package org.wltea.analyzer.core;
26 |
27 | import java.util.Stack;
28 | import java.util.TreeSet;
29 |
30 | /**
31 | * IK分词歧义裁决器
32 | */
33 | class IKArbitrator {
34 |
35 | IKArbitrator(){
36 |
37 | }
38 |
39 | /**
40 | * 分词歧义处理
41 | * @param orgLexemes
42 | * @param useSmart
43 | */
44 | void process(AnalyzeContext context , boolean useSmart){
45 | QuickSortSet orgLexemes = context.getOrgLexemes();
46 | Lexeme orgLexeme = orgLexemes.pollFirst();
47 |
48 | LexemePath crossPath = new LexemePath();
49 | while(orgLexeme != null){
50 | if(!crossPath.addCrossLexeme(orgLexeme)){
51 | //找到与crossPath不相交的下一个crossPath
52 | if(crossPath.size() == 1 || !useSmart){
53 | //crossPath没有歧义 或者 不做歧义处理
54 | //直接输出当前crossPath
55 | context.addLexemePath(crossPath);
56 | }else{
57 | //对当前的crossPath进行歧义处理
58 | QuickSortSet.Cell headCell = crossPath.getHead();
59 | LexemePath judgeResult = this.judge(headCell, crossPath.getPathLength());
60 | //输出歧义处理结果judgeResult
61 | context.addLexemePath(judgeResult);
62 | }
63 |
64 | //把orgLexeme加入新的crossPath中
65 | crossPath = new LexemePath();
66 | crossPath.addCrossLexeme(orgLexeme);
67 | }
68 | orgLexeme = orgLexemes.pollFirst();
69 | }
70 |
71 |
72 | //处理最后的path
73 | if(crossPath.size() == 1 || !useSmart){
74 | //crossPath没有歧义 或者 不做歧义处理
75 | //直接输出当前crossPath
76 | context.addLexemePath(crossPath);
77 | }else{
78 | //对当前的crossPath进行歧义处理
79 | QuickSortSet.Cell headCell = crossPath.getHead();
80 | LexemePath judgeResult = this.judge(headCell, crossPath.getPathLength());
81 | //输出歧义处理结果judgeResult
82 | context.addLexemePath(judgeResult);
83 | }
84 | }
85 |
86 | /**
87 | * 歧义识别
88 | * @param lexemeCell 歧义路径链表头
89 | * @param fullTextLength 歧义路径文本长度
90 | * @param option 候选结果路径
91 | * @return
92 | */
93 | private LexemePath judge(QuickSortSet.Cell lexemeCell , int fullTextLength){
94 | //候选路径集合
95 | TreeSet pathOptions = new TreeSet();
96 | //候选结果路径
97 | LexemePath option = new LexemePath();
98 |
99 | //对crossPath进行一次遍历,同时返回本次遍历中有冲突的Lexeme栈
100 | Stack lexemeStack = this.forwardPath(lexemeCell , option);
101 |
102 | //当前词元链并非最理想的,加入候选路径集合
103 | pathOptions.add(option.copy());
104 |
105 | //存在歧义词,处理
106 | QuickSortSet.Cell c = null;
107 | while(!lexemeStack.isEmpty()){
108 | c = lexemeStack.pop();
109 | //回滚词元链
110 | this.backPath(c.getLexeme() , option);
111 | //从歧义词位置开始,递归,生成可选方案
112 | this.forwardPath(c , option);
113 | pathOptions.add(option.copy());
114 | }
115 |
116 | //返回集合中的最优方案
117 | return pathOptions.first();
118 |
119 | }
120 |
121 | /**
122 | * 向前遍历,添加词元,构造一个无歧义词元组合
123 | * @param LexemePath path
124 | * @return
125 | */
126 | private Stack forwardPath(QuickSortSet.Cell lexemeCell , LexemePath option){
127 | //发生冲突的Lexeme栈
128 | Stack conflictStack = new Stack();
129 | QuickSortSet.Cell c = lexemeCell;
130 | //迭代遍历Lexeme链表
131 | while(c != null && c.getLexeme() != null){
132 | if(!option.addNotCrossLexeme(c.getLexeme())){
133 | //词元交叉,添加失败则加入lexemeStack栈
134 | conflictStack.push(c);
135 | }
136 | c = c.getNext();
137 | }
138 | return conflictStack;
139 | }
140 |
141 | /**
142 | * 回滚词元链,直到它能够接受指定的词元
143 | * @param lexeme
144 | * @param l
145 | */
146 | private void backPath(Lexeme l , LexemePath option){
147 | while(option.checkCross(l)){
148 | option.removeTail();
149 | }
150 |
151 | }
152 |
153 | }
154 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/weibo4j/PublicService.java:
--------------------------------------------------------------------------------
1 | package weibo4j;
2 |
3 | import weibo4j.model.PostParameter;
4 | import weibo4j.model.WeiboException;
5 | import weibo4j.org.json.JSONArray;
6 | import weibo4j.org.json.JSONObject;
7 | import weibo4j.util.WeiboConfig;
8 |
9 | public class PublicService extends Weibo{
10 |
11 | private static final long serialVersionUID = 1L;
12 |
13 | /**
14 | * 通过地址编码获取地址名称
15 | *
16 | *
17 | */
18 | public JSONArray getLocationByCode (String codes) throws WeiboException {
19 | return client.get(WeiboConfig.getValue("baseURL") + "common/code_to_location.json",new PostParameter[] {
20 | new PostParameter("codes", codes)
21 | }).asJSONArray();
22 | }
23 | /**
24 | * 获取省份列表
25 | *
26 | *
27 | */
28 | public JSONArray provinceList (String country) throws WeiboException {
29 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_province.json",new PostParameter[] {
30 | new PostParameter("country", country)
31 | }).asJSONArray();
32 | }
33 |
34 | public JSONArray provinceListOfCapital (String country,String capital) throws WeiboException {
35 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_province.json",new PostParameter[] {
36 | new PostParameter("country", country),
37 | new PostParameter("capital", capital)
38 | }).asJSONArray();
39 | }
40 |
41 | public JSONArray provinceList (String country,String language) throws WeiboException {
42 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_province.json",new PostParameter[] {
43 | new PostParameter("country", country),
44 | new PostParameter("language", language)
45 | }).asJSONArray();
46 | }
47 |
48 | public JSONArray provinceList (String country,String capital,String language) throws WeiboException {
49 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_province.json",new PostParameter[] {
50 | new PostParameter("country", country),
51 | new PostParameter("capital", capital),
52 | new PostParameter("language", language)
53 | }).asJSONArray();
54 | }
55 | /**
56 | * 获取城市列表
57 | *
58 | *
59 | */
60 | public JSONArray cityList (String province) throws WeiboException {
61 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_city.json",new PostParameter[] {
62 | new PostParameter("province", province)
63 | }).asJSONArray();
64 | }
65 |
66 | public JSONArray cityListOfCapital (String province,String capital) throws WeiboException {
67 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_city.json",new PostParameter[] {
68 | new PostParameter("province", province),
69 | new PostParameter("capital", capital)
70 | }).asJSONArray();
71 | }
72 |
73 | public JSONArray cityList (String province,String language) throws WeiboException {
74 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_city.json",new PostParameter[] {
75 | new PostParameter("province", province),
76 | new PostParameter("language", language)
77 | }).asJSONArray();
78 | }
79 |
80 | public JSONArray cityList (String province,String capital,String language) throws WeiboException {
81 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_city.json",new PostParameter[] {
82 | new PostParameter("province", province),
83 | new PostParameter("capital", capital),
84 | new PostParameter("language", language)
85 | }).asJSONArray();
86 | }
87 | /**
88 | * 获取国家列表
89 | *
90 | *
91 | */
92 | public JSONArray countryList () throws WeiboException {
93 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_country.json").asJSONArray();
94 | }
95 |
96 | public JSONArray countryListOfCapital (String capital) throws WeiboException {
97 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_country.json",new PostParameter[] {
98 | new PostParameter("capital", capital)
99 | }).asJSONArray();
100 | }
101 |
102 | public JSONArray countryList (String language) throws WeiboException {
103 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_country.json",new PostParameter[] {
104 | new PostParameter("language", language)
105 | }).asJSONArray();
106 | }
107 |
108 | public JSONArray countryList (String capital,String language) throws WeiboException {
109 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_country.json",new PostParameter[] {
110 | new PostParameter("capital", capital),
111 | new PostParameter("language", language)
112 | }).asJSONArray();
113 | }
114 |
115 | /**
116 | * 获取时区配置表
117 | *
118 | */
119 | public JSONObject getTomeZone () throws WeiboException {
120 | return client.get(WeiboConfig.getValue("baseURL") + "common/get_timezone.json").asJSONObject();
121 | }
122 |
123 | }
124 |
--------------------------------------------------------------------------------
/WeiboAnalyzer/src/org/wltea/analyzer/core/IKSegmenter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * IK 中文分词 版本 5.0
3 | * IK Analyzer release 5.0
4 | *
5 | * Licensed to the Apache Software Foundation (ASF) under one or more
6 | * contributor license agreements. See the NOTICE file distributed with
7 | * this work for additional information regarding copyright ownership.
8 | * The ASF licenses this file to You under the Apache License, Version 2.0
9 | * (the "License"); you may not use this file except in compliance with
10 | * the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing, software
15 | * distributed under the License is distributed on an "AS IS" BASIS,
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 | * See the License for the specific language governing permissions and
18 | * limitations under the License.
19 | *
20 | * 源代码由林良益(linliangyi2005@gmail.com)提供
21 | * 版权声明 2012,乌龙茶工作室
22 | * provided by Linliangyi and copyright 2012 by Oolong studio
23 | */
24 | package org.wltea.analyzer.core;
25 |
26 | import java.io.IOException;
27 | import java.io.Reader;
28 | import java.util.ArrayList;
29 | import java.util.List;
30 |
31 | import org.wltea.analyzer.cfg.Configuration;
32 | import org.wltea.analyzer.cfg.DefaultConfig;
33 | import org.wltea.analyzer.dic.Dictionary;
34 |
35 | /**
36 | * IK分词器主类
37 | *
38 | */
39 | public final class IKSegmenter {
40 |
41 | //字符窜reader
42 | private Reader input;
43 | //分词器配置项
44 | private Configuration cfg;
45 | //分词器上下文
46 | private AnalyzeContext context;
47 | //分词处理器列表
48 | private List segmenters;
49 | //分词歧义裁决器
50 | private IKArbitrator arbitrator;
51 |
52 |
53 | /**
54 | * IK分词器构造函数
55 | * @param input
56 | * @param useSmart 为true,使用智能分词策略
57 | *
58 | * 非智能分词:细粒度输出所有可能的切分结果
59 | * 智能分词: 合并数词和量词,对分词结果进行歧义判断
60 | */
61 | public IKSegmenter(Reader input , boolean useSmart){
62 | this.input = input;
63 | this.cfg = DefaultConfig.getInstance();
64 | this.cfg.setUseSmart(useSmart);
65 | this.init();
66 | }
67 |
68 | /**
69 | * IK分词器构造函数
70 | * @param input
71 | * @param cfg 使用自定义的Configuration构造分词器
72 | *
73 | */
74 | public IKSegmenter(Reader input , Configuration cfg){
75 | this.input = input;
76 | this.cfg = cfg;
77 | this.init();
78 | }
79 |
80 | /**
81 | * 初始化
82 | */
83 | private void init(){
84 | //初始化词典单例
85 | Dictionary.initial(this.cfg);
86 | //初始化分词上下文
87 | this.context = new AnalyzeContext(this.cfg);
88 | //加载子分词器
89 | this.segmenters = this.loadSegmenters();
90 | //加载歧义裁决器
91 | this.arbitrator = new IKArbitrator();
92 | }
93 |
94 | /**
95 | * 初始化词典,加载子分词器实现
96 | * @return List
97 | */
98 | private List loadSegmenters(){
99 | List segmenters = new ArrayList(4);
100 | //处理字母的子分词器
101 | segmenters.add(new LetterSegmenter());
102 | //处理中文数量词的子分词器
103 | segmenters.add(new CN_QuantifierSegmenter());
104 | //处理中文词的子分词器
105 | segmenters.add(new CJKSegmenter());
106 | return segmenters;
107 | }
108 |
109 | /**
110 | * 分词,获取下一个词元
111 | * @return Lexeme 词元对象
112 | * @throws IOException
113 | */
114 | public synchronized Lexeme next()throws IOException{
115 | Lexeme l = null;
116 | while((l = context.getNextLexeme()) == null ){
117 | /*
118 | * 从reader中读取数据,填充buffer
119 | * 如果reader是分次读入buffer的,那么buffer要 进行移位处理
120 | * 移位处理上次读入的但未处理的数据
121 | */
122 | int available = context.fillBuffer(this.input);
123 | if(available <= 0){
124 | //reader已经读完
125 | context.reset();
126 | return null;
127 |
128 | }else{
129 | //初始化指针
130 | context.initCursor();
131 | do{
132 | //遍历子分词器
133 | for(ISegmenter segmenter : segmenters){
134 | segmenter.analyze(context);
135 | }
136 | //字符缓冲区接近读完,需要读入新的字符
137 | if(context.needRefillBuffer()){
138 | break;
139 | }
140 | //向前移动指针
141 | }while(context.moveCursor());
142 | //重置子分词器,为下轮循环进行初始化
143 | for(ISegmenter segmenter : segmenters){
144 | segmenter.reset();
145 | }
146 | }
147 | //对分词进行歧义处理
148 | this.arbitrator.process(context, this.cfg.useSmart());
149 | //将分词结果输出到结果集,并处理未切分的单个CJK字符
150 | context.outputToResult();
151 | //记录本次分词的缓冲区位移
152 | context.markBufferOffset();
153 | }
154 | return l;
155 | }
156 |
157 | /**
158 | * 重置分词器到初始状态
159 | * @param input
160 | */
161 | public synchronized void reset(Reader input) {
162 | this.input = input;
163 | context.reset();
164 | for(ISegmenter segmenter : segmenters){
165 | segmenter.reset();
166 | }
167 | }
168 | }
169 |
--------------------------------------------------------------------------------