package org.ansj.app.keyword;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.NlpAnalysis;
import org.nlpcn.commons.lang.util.StringUtil;
import org.nlpcn.commons.lang.util.WordAlert;

public class KeyWordComputer {

	private static final Map<String, Double> POS_SCORE = new HashMap<String, Double>();

	static {
		POS_SCORE.put("null", 0.0);
		POS_SCORE.put("w", 0.0);
		POS_SCORE.put("en", 0.0);
		POS_SCORE.put("num", 0.0);
		POS_SCORE.put("nr", 3.0);
		POS_SCORE.put("nrf", 3.0);
		POS_SCORE.put("nw", 3.0);
		POS_SCORE.put("nt", 3.0);
		POS_SCORE.put("l", 0.2);
		POS_SCORE.put("a", 0.2);
		POS_SCORE.put("nz", 3.0);
		POS_SCORE.put("v", 0.2);

	}

	private int nKeyword = 5;

	public KeyWordComputer() {
	}

	/**
	 * 返回关键词个数
	 * 
	 * @param nKeyword
	 */
	public KeyWordComputer(int nKeyword) {
		this.nKeyword = nKeyword;

	}

	/**
	 * 
	 * @param content
	 *            正文
	 * @return
	 */
	private List<Keyword> computeArticleTfidf(String content, int titleLength) {
		Map<String, Keyword> tm = new HashMap<String, Keyword>();

		List<Term> parse = NlpAnalysis.parse(content);
		for (Term term : parse) {
			double weight = getWeight(term, content.length(), titleLength);
			if (weight == 0)
				continue;
			// 判断是否是数字
			char c = WordAlert.CharCover(term.getName().charAt(0));
			if (c >= '0' && c <= '9') {
				continue;
			}

			Keyword keyword = tm.get(term.getName());
			if (keyword == null) {
				keyword = new Keyword(term.getName(), term.natrue().allFrequency, weight);
				tm.put(term.getName(), keyword);
			} else {
				keyword.updateWeight(1);
			}
		}

		TreeSet<Keyword> treeSet = new TreeSet<Keyword>(tm.values());

		ArrayList<Keyword> arrayList = new ArrayList<Keyword>(treeSet);
		if (treeSet.size() <= nKeyword) {
			return arrayList;
		} else {
			return arrayList.subList(0, nKeyword);
		}

	}

	/**
	 * 
	 * @param title
	 *            标题
	 * @param content
	 *            正文
	 * @return
	 */
	public List<Keyword> computeArticleTfidf(String title, String content) {
		if (StringUtil.isBlank(title)) {
			title = "";
		}
		if (StringUtil.isBlank(content)) {
			content = "";
		}
		return computeArticleTfidf(title + "\t" + content, title.length());
	}

	/**
	 * 只有正文
	 * 
	 * @param content
	 * @return
	 */
	public List<Keyword> computeArticleTfidf(String content) {
		return computeArticleTfidf(content, 0);
	}

	private double getWeight(Term term, int length, int titleLength) {
		if (term.getName().trim().length() < 2) {
			return 0;
		}

		String pos = term.natrue().natureStr;

		Double posScore = POS_SCORE.get(pos);

		if (posScore == null) {
			posScore = 1.0;
		} else if (posScore == 0) {
			return 0;
		}

		if (titleLength > term.getOffe()) {
			return 5 * posScore;
		}
		return (length - term.getOffe()) * posScore / (double) length;
	}

}

@@ -237,16 +237,55 @@
 lysis;%0D%0A
+import org.ansj.util.FilterModifWord;%0D%0A
 import o
@@ -324,16 +324,16 @@
 gUtil;%0D%0A
-
 import o
@@ -586,32 +586,60 @@
 ut(%22en%22, 0.0);%0D%0A
+%09%09POS_SCORE.put(%22m%22, 0.0);%0D%0A
 %09%09POS_SCORE.put(
@@ -882,18 +882,53 @@
  0.2);%0D%0A
+%09%09POS_SCORE.put(%22kw%22, 6.0); //%E5%85%B3%E9%94%AE%E8%AF%8D%E8%AF%8D%E6%80%A7
 %0D%0A
-
 %09%7D%0D%0A%0D%0A%09p
@@ -1382,16 +1382,72 @@
 tent);%0D%0A
+%09%09%0D%0A%09%09parse = FilterModifWord.modifResult(parse) ;%0D%0A%09%09%0D%0A
 %09%09for (T
@@ -1578,133 +1578,8 @@
 %0A%09%09%09
-// %E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%98%AF%E6%95%B0%E5%AD%97%0D%0A%09%09%09char c = WordAlert.CharCover(term.getName().charAt(0));%0D%0A%09%09%09if (c %3E= '0' && c %3C= '9') %7B%0D%0A%09%09%09%09continue;%0D%0A%09%09%09%7D%0D%0A
 %0D%0A%09%09
@@ -1618,24 +1618,34 @@
 etName());%0D%0A
+%09%09%09%0D%0A%09%09%09%0D%0A
 %09%09%09if (keywo

add keyword to userdefine library