分享

ansj中英混搭提取关键字

Joker 2015-3-12 17:01:20 发表于 疑问解答 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 4 37361
  1. public static void main(String[] args) {
  2. KeyWordComputer kwc = new KeyWordComputer(6);
  3. String title = "马航";
  4. String content = "搜救。helloworld,hi,hi,boy,girl)";
  5. List<Keyword> list = kwc.computeArticleTfidf(title, content);
  6. if(list != null && list.size() > 0){
  7. for(Keyword kw : list){
  8. System.out.println(kw.getName() + kw.getScore());
  9. }
  10. }
  11. }
复制代码


上面代码,我要抽取6个关键字,但是如果是英文根本就获取不到
刚接触这个,深入的也不是很了解。大家有上面方法支持英文啊

已有(4)人评论

跳转到指定楼层
xuanxufeng 发表于 2015-3-12 17:32:14
需要改KeyWordComputer这个类的关于英语的POS_SCORE
回复

使用道具 举报

Joker 发表于 2015-3-12 17:45:16
xuanxufeng 发表于 2015-3-12 17:32
需要改KeyWordComputer这个类的关于英语的POS_SCORE

网上也是这样说,就是没有实际的例子,怎么去改啊?
回复

使用道具 举报

xuanxufeng 发表于 2015-3-12 18:31:38
Joker 发表于 2015-3-12 17:45
网上也是这样说,就是没有实际的例子,怎么去改啊?

该的部位应该是红字部分,具体怎么该,就看楼主工底了,也在找这方面的资料。
public class KeyWordComputerSub extends 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.1);
  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 KeyWordComputerSub() {
}

public KeyWordComputerSub(int nKeyword) {
  this.nKeyword = nKeyword;

}
  @Override
public List<Keyword> computeArticleTfidf(String content) {
   Map<String, Keyword> tm = new HashMap<String, Keyword>();

   List<Term> parse = NlpAnalysis.parse(content);
   for (Term term : parse) {
    double weight = getWeight(term, content.length(), 0);
    if (weight == 0)
     continue;
    Keyword keyword = tm.get(term.getName());
    if (keyword == null) {
     keyword = new Keyword(term.getName(), term.getNatrue().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);
   }
}

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

   String pos = term.getNatrue().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;
  }
}

回复

使用道具 举报

Joker 发表于 2015-3-12 23:03:48
xuanxufeng 发表于 2015-3-12 18:31
该的部位应该是红字部分,具体怎么该,就看楼主工底了,也在找这方面的资料。
public class KeyWordComp ...

你用2.0的版本你需要把en改成3.0而不是0.1那是1的版本的
而且你这个和现在最新的版本不是一直的源码KeyWordComputer你可以去github上看到最新的源码
老版本的一些方法找不到了,非常感谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条