`
DavyJones2010
  • 浏览: 147723 次
  • 性别: 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);
		}
	}

}

 

 

 

 

 

 

分享到:
评论

相关推荐

    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....

    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 ...

    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...

    Python Cookbook英文版

    5.4 Calling a Superclass Implementation of a Method 5.5 Implementing Properties 5.6 Implementing Static Methods 5.7 Implementing Class Methods 5.8 Delegating Automatically as an Alternative ...

    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 ...

    Thinking in Java 4th Edition

    Arrays in Java .............................. 44 You never need to destroy an object .................. 45 Scoping ........................................ 45 Scope of objects ...........................

    Principles of Communication System Simulation with Wireless Aplications (英文版PDF)

    1.4.1 An Example of a Deterministic Simulation 16 1.4.2 An Example of a Stochastic Simulation 17 1.5 The Role of Simulation 19 1.5.1 Link Budget and System-Level Specification Process 20 1.5.2 ...

    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 ...

    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...

    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;...

    数位板压力测试

    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 ...

    Professional C# 3rd Edition

    The Role of C# in the .NET Enterprise Architecture 24 Summary 26 Chapter 2: C# Basics 29 Before We Start 30 Our First C# Program 30 The Code 30 Compiling and Running the Program 31 Contents A Closer ...

    王小平版遗传算法的光盘源代码

    a separate set of test cases which were not used in training (i.e., in generating the fitness values which drive selection). The best-of-run individual is determined using the figure returned from ...

Global site tag (gtag.js) - Google Analytics