All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ansj.recognition.impl.BookRecognition Maven / Gradle / Ivy

There is a newer version: 5.1.6
Show newest version
package org.ansj.recognition.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.ansj.domain.Nature;
import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.recognition.Recognition;

/**
 * 基于规则的新词发现 jijiang feidiao
 * 
 * @author ansj
 * 
 */
public class BookRecognition implements Recognition {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Nature nature = new Nature("book");

	private static Map ruleMap = new HashMap();

	static {
		ruleMap.put("《", "》");
	}

	public void recognition(Result result) {
		List terms = result.getTerms() ;
		String end = null;
		String name;

		LinkedList mergeList = null;

		List list = new LinkedList();

		for (Term term : terms) {
			name = term.getName();
			if (end == null) {
				if ((end = ruleMap.get(name)) != null) {
					mergeList = new LinkedList();
					mergeList.add(term);
				} else {
					list.add(term);
				}
			} else {
				mergeList.add(term);
				if (end.equals(name)) {

					Term ft = mergeList.pollFirst();
					for (Term sub : mergeList) {
						ft.merage(sub);
					}
					ft.setNature(nature);
					list.add(ft);
					mergeList = null;
					end = null;
				}
			}
		}

		if (mergeList != null) {
			for (Term term : list) {
				list.add(term);
			}
		}

		result.setTerms(list) ;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy