`
DavyJones2010
  • 浏览: 154784 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

A simple implementation of "Input Method" in Java

阅读更多

 

package edu.xmu.guava.jcip;

import java.util.Map;

public interface Traversable {
	public void addWord(String word, int index);
	public Traversable getNext(Character c);
	public Map<Character, ? extends Traversable> getChildren();
}

 

package edu.xmu.guava.jcip;

import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeMap;

public class Dictionary {

	public static void main(String[] args) {

		Traversable rootNode = new VirtualHeaderNode();
		rootNode.addWord("Hello", 0);
		rootNode.addWord("Welcome", 0);
		rootNode.addWord("Yang", 0);
		rootNode.addWord("Yes", 0);
		rootNode.addWord("Good", 0);
		rootNode.addWord("God", 0);
		rootNode.addWord("Yeah", 0);
		rootNode.addWord("Yep", 0);

		System.out.println(rootNode.getChildren().keySet());

		Stack<Traversable> wordStack = new Stack<Traversable>();

		Scanner scanner = new Scanner(System.in);
		String str = scanner.next();
		Traversable currentNode = rootNode;
		wordStack.push(currentNode);
		while (true) {
			if ("POP".equals(str)) {
				if (!(wordStack.peek() instanceof VirtualHeaderNode)) {
					wordStack.pop();
					currentNode = wordStack.peek();
				}
			} else if ("EXIT".equals(str)) {
				scanner.close();
				break;
			} else {
				char alpha = str.charAt(0);
				if (currentNode.getChildren().containsKey(alpha)) {
					currentNode = currentNode.getChildren().get(alpha);
					wordStack.push(currentNode);
				}
			}
			Map<Character, ? extends Traversable> next = currentNode
					.getChildren();
			System.out.println(next.keySet());
			str = scanner.next();
		};
	}

	public static class VirtualHeaderNode implements Traversable {
		private Map<Character, Traversable> children = new TreeMap<Character, Traversable>();
		@Override
		public void addWord(String word, int index) {
			char c = word.charAt(0);
			Traversable headerNode;
			if (children.containsKey(c)) {
				headerNode = children.get(c);
			} else {
				headerNode = new AlphabetNode();
				children.put(c, headerNode);
			}
			headerNode.addWord(word, index);
		}

		@Override
		public Traversable getNext(Character c) {
			return children.get(c);
		}

		@Override
		public Map<Character, Traversable> getChildren() {
			return children;
		}

	}
	public static class AlphabetNode implements Traversable {
		private AlphabetNode parent;
		private Map<Character, AlphabetNode> children = new TreeMap<Character, AlphabetNode>();

		private char value;

		public void addWord(String word, int i) {
			this.value = word.charAt(i);

			if (i >= (word.length() - 1)) {
				return;
			}

			char childValue = word.charAt(i + 1);

			AlphabetNode childNode;
			if (children.containsKey(childValue)) {
				childNode = children.get(childValue);
			} else {
				childNode = new AlphabetNode();
				childNode.setParent(this);
				childNode.setValue(childValue);
				children.put(childValue, childNode);
			}
			childNode.addWord(word, i + 1);
		}
		public AlphabetNode getParent() {
			return parent;
		}

		public void setParent(AlphabetNode parent) {
			this.parent = parent;
		}

		public Map<Character, AlphabetNode> getChildren() {
			return children;
		}
		public void setChildren(Map<Character, AlphabetNode> children) {
			this.children = children;
		}
		public char getValue() {
			return value;
		}

		public void setValue(char value) {
			this.value = value;
		}

		@Override
		public String toString() {
			return "AlphabetNode [value=" + value + "]";
		}
		@Override
		public AlphabetNode getNext(Character c) {
			return children.get(c);
		}
	}

}

 

 

 

 

 

 

分享到:
评论

相关推荐

    Java邮件开发Fundamentals of the JavaMail API

    the source of a lot of confusion. Much of what people are familiar with when using POP, like the ability to see how many new mail messages they have, are not supported by POP at all. These ...

    jdk-9.0.1_doc-all 最新版

    The Java Development Kit (JDK) APIs are specific to the JDK and will not necessarily be available in all implementations of the Java SE Platform. These APIs are in modules whose names start with jdk....

    android sdk 自带 实例(samples)

    An example of writing an input method for a software keyboard. Spinner A simple application that serves as an application-under-test for the SpinnerTest sample application. SpinnerTest An example ...

    Python Cookbook, 2nd Edition

    Processing All of a List's Items in Random Order Recipe 5.7. Keeping a Sequence Ordered as Items Are Added Recipe 5.8. Getting the First Few Smallest Items of a Sequence Recipe 5.9. Looking for...

    Fuzzy Control Systems

    Chapter 9—A Self Generating and Tuning Method for Fuzzy Modeling using Interior Penalty Method and its Application to Knowledge Acquisition of Fuzzy Controller 1 Introduction 2 Self Tuning ...

    Fuzzy and Neuro-Fuzzy Systems in Medicine

    Combining the Automated Generation of Rules and Membership Functions and the Adjustment of their Parameters in a Parallel Implementation (FUNNY-ALGORAM) 4. In Vitro Experiments and Application to ...

    Information-Theoretic Aspects of Neural Networks

    2.5.2 Representation of a Neuron as an Input-Dictated Cybernetic Regulator with a Quadratic Cost-function 2.5.3 Generalized Information-Theoretic Entropy Measure 2.5.4 Influence of Nonlinear ...

    Java Network Programming 3rd Edition By Elliotte Rusty Harold 2004

    Classic Java book. Chapter 1. Why Networked Java? Section 1.1. What Can a Network Program Do? Section 1.2. Security Section 1.3. But Wait! There's More! Chapter 2. Basic Network Concepts ...

    GPU Pro 360 Guide to Geometry Manipulation pdf2018

    stages of DirectX 11, explains a simple implementation of terrain rendering, and implements the techniques from the ShaderX6 article “Procedural Ocean Effects” by L´aszl´o Sz´ecsi and Khashayar ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    More complex inline functions may also be put in a .h file for the convenience of the implementer and callers, though if this makes the .h file too unwieldy you can instead put that code in a ...

    Loving Lisp

    **Common Lisp**, in particular, is a standardized dialect of Lisp that provides a rich set of features and a comprehensive standard library, making it suitable for a wide range of applications. ...

    Object-Oriented Software Construction 2nd

    1.2 A REVIEW OF EXTERNAL FACTORS 4 1.3 ABOUT SOFTWARE MAINTENANCE 17 1.4 KEY CONCEPTS INTRODUCED IN THIS CHAPTER 19 1.5 BIBLIOGRAPHICAL NOTES 19 Chapter 2: Criteria of object orientation 21 2.1 ON THE...

    restful restful所需要的jar包

    This results in a very flexible yet simple routing with automatic extraction of URI variables into request attributes. * Tunneling service lets browsers issue any HTTP method (PUT, DELETE, MOVE, etc...

    IntroductiontoAlgorithms,.Edition Solutions

    An implementation of the LINEAR-SEARCH algorithm is provided, which takes an array \(A\) and a value \(v\) as inputs and returns the index \(i\) such that \(v = A[i]\) or nil if \(v\) is not found in ...

    Sakemail

    Some stupid mail servers put tabs in some fields (CC:, TO:) when they want to make a new line, the correct is to put at least a space in the beginning of the line, added a little code to &quot;...

    Problem Solving with C++ (7th edition)

    - **Selection Sort Walkthrough**: Step-by-step explanation of the selection sort algorithm, a simple but inefficient sorting technique. **Programming Project 7.3**: This project likely involves ...

    数位板压力测试

    The availability of drivers that support the features of the specification will simplify the process of developing Windows appli¬cation programs that in-corporate absolute coordinate input, and ...

    The art of Molecular Dynamics Simulations ,2nd Edition,Rapaport

    4 Equilibrium properties of simple fluids 83 4.1 Introduction 83 4.2 Thermodynamic measurements 84 4.3 Structure 90 4.4 Packing studies 96 4.5 Cluster analysis 112 4.6 Further study 118 5 Dynamical ...

Global site tag (gtag.js) - Google Analytics