Getting started with JADT, Part 2

xiaoxiao2021-03-06  90

Use Java Dictionary and Dictionary API to make Java users can use your word reference

Level: Intermediate

Rakesh Midha (Mrakesh@in.ibm.com) Software Engineer, IBM Software Lab, Bangalore 2004 October

In the first article of JADT, a set of libraries from AlphaWorks is introduced to access language features in the Java application. Next, Rakesh MIDHA gives a detailed description of the JADT architecture and API herein. He also showed you how to start this API as a driver by analyzing an implementation example of a driver.

In Part 1 of this series, you have learned some basic knowledge of JADT, JADT provides a transparent way to access dictionary or non-structured words centered on Java and their related information. The main goal of JADT is to provide a dictionary provider with a standard technology that can be based so that they support the Java platform. Part 1 introduces JADT from the perspective of developers. In this article, I will give the details of the implementation, focusing on the perspective of dictionary developers. I will take you to the details of JADT and JADT architecture and provide information about all classes and interfaces included in JADT.

With the help of the JADT sample driver implementation, I will discuss the required interface implementation. After that, you will have a holistic impression on the JADT driver and the driver.

The JADT architecture JADT architecture provides any type of backend data that provides a driver access. The Dictionary provider implements the driver terminal interface, providing an access point for the dictionary, any program that uses this dictionary, the user-end interface of the dictionary service. As a result, you can easily integrate the JADT architecture with a variety of developers and third-party drivers. As shown in Figure 1, dedicated language data is always separated from JADT by JADT Driver:

Figure 1. JADT architecture

JADT developer's role If you want to use Jadt to disclose your dictionary or dictionary, you only need to provide JADT implementation. The JADT driver is located between developer data and Java programmers, allowing developers to provide services outside the situation in not fully disclose their data and data formats. in turn. Java programmers don't have to go deep into a developer's data and data format, you can use the developer's data. JADT driver developers can use JADT interfaces and implement them according to their own habits. Once the driver is ready, you can access your data, then enable it in your site. To this site, you can use it as a thread in the JADT access page. JADT works in all data dictionaries in a consistent way, so it is the responsibility of the developer to achieve all JADT interfaces and methods that meet JADT standards. JADT JavadoCS and interface definition documents are very clearly disclosed to the target of interfaces and methods.

JADT implementation JADT defines multi-group interfaces and classes. Some interfaces are helpless, some are optional, depending on the functions you have to support your driver. If you want to fully support JADT, you must implement all interfaces in accordance with the specifications defined in javadocs. In this section, I will give all of these interfaces and class implementation details.

The complete JADT interface and class set, as shown in the class diagram in Figure 2:

Figure 2. JADT class diagram

Below I will introduce various classes and interfaces.

The basic control class of the JadtdriverFactoryManagerJaddriverFactory object is com.ibm.jadt.jadtdriverfactoryManager, this class is like the connection between users and available drivers factories. You can use it to get Driver's instance, and then use the instance to get one or more JADT services implemented by a driver. After jadtdriverfactory is loaded, the Jadtdriver provider is responsible for registering JadtdriverFactory, so JADT users can also install Class.ForName (DRIVER_FAACTORY_CLASS) at any time, and explicitly load Jadtdriver.

The name of the driver factory class should be handed over to the user with JadtdriverFactoryName, and the user can get GetjadtdriverFactory, using this type of static method getjadtdriverfactory. You can register it in a static registerjadtdriverfactory method.

JADT has the following interfaces, these interfaces must be implemented:

com.ibm.jadt. jadtdriverfactory com.ibm.jadt.jadtdriver

JadtdriverFactorycom.ibm.jadt.jadtdriverFactory factory interface is used to establish driver instances, and should be registered by the implementation of the driver to the factory controller. Since JADT is a driver-based product, Jadtdriver's construct should always pass through the factory. All driver implementors should implement this interface. In the static block of the DriverFactory class, the factory should register with FactoryManager, as shown in Listing:

Listing 1. Static method of registered factory

Public Class JadtsampleDriverfactory Implements JadtdriverFactory

{

Private static jadtsampledriverfactory factory;

PRIVATE PROPERTIES PROPS;

Private Jadtsampledriver Driver DRIVER DRIVER DIVATE

STATIC {

Try {

IF (factory == NULL)

Factory = new jadtsampledriverfactory ();

JadtdriverFactoryManager.Registerjadtdriverfactory (Factory);

}

Catch (Exception EX) {

// Handle Exception

}

}

}

In addition to the manager registration factory, the JadtsampleDriverFactory class must implement the CREATEJADTDRIVER () method. You can separate the load and data access policing (or any of any type of access attribute) with the CreateJadtdriver () method. For example, Listing 2 shows how you can set the number of factories to initialize, which is determined by the loading_strategy property:

Listing 2. Example of load policy

Public Jadtdriver Createjadtdriver () throws jadtexception {

IF (this.getProperty)! = null&&

THIS.GETPROPERTY ("Loading_strategy"). Equals ("multiple") {

IF (this.getProperty ("jadtsampledriverdir") == null) Return Driver = new jadtsampledriver ();

Else Return Driver = New Jadtsampledriver

(this.getProperty ("jadtsampledriverdir");

}

Return new jadtsampledriver ();

Else

{

IF (this.getProperty ("jadtsampledriverdir")! = null)

IF (driver == null)

Driver = new jadttextdriver ("JADTSAMPLEDRIVERDIR");

Else

IF (driver == null)

Driver = new jadtsampledriver ();

Return Driver;

}

}

All attributes used by the driver must be programmed and provide a guide for drivers. The rest of this interface method is self-explanatory, so it can be easily implemented.

Jadtdriver interface com.ibm.jadt.jadtdriver interface is a primary drive interface that must be implemented all drivers. It is created by JadtdriverFactory, providing you with a class access to the driver service, including a dictionary, dictionary, spellper, a word table, a translator, a syntax detector, and a word dispenser.

This interface represents the driver of specific manufacturers. In addition, the same driver manufacturers can serve different languages.

The JADTDriver class must implement basic constructor methods, and constructor can be used to specify attributes, such as databases, directories, or data access methods. The basic connection of the database can also be initialized with a constructor. The structure of the class is shown in Listing 3:

Listing 3. JADTSAMpleDriver basic sample code

Public Class Jadtsampledriver Implements Jadtdriver, PUBLIC CLASS JADTSAMPLIVER

{

PRIVATE PROPERTIES PROPS;

Public JadtsampleDriver ()

{

PROPS = New Properties ();

}

Public JadtsampleDriver (String DataPath)

{

PROPS = New Properties ();

SetProperty ("JADTSAMPLEDRIVERDIR", FILEPATH;

}

/ *

REST All Methods

* /

}

The Jadtdriver class also provides access to various services, so this class implements public GET> Service

Listing 4. Dictionary service method implementation

Public Dictionary GetDictionary (String Langfrom, String Langto) THROWS JADTEXCEPTION

{

Try {

IF ("jadtsampledriverdir")! = null) {

Return New Sampledictionary

(Langfrom, langto, getproperty ("jadtsampledriverdir));

}

Else Return New Sampledictionary (Langfrom, Langto);

}

Catch (jadtexception exp) {

JADTEXCEPTION EX1 = New Jadtexception ("DictionaryFail", "Could Not Create Dictionary");

EX1.SETNEXTEXCEPTION (EXP);

Throw EX1;

}

}

In addition, the methods shown in Listing 5 must also be implemented in a similar style:

Listing 5. Other service methods list

Public Wordbook getWordBook

"String language"

Public SpellChecker GetspellChecker

"String language"

Public WordLister getWordLister

"String language"

Public Translator GetTranslator

"String Langfrom, String Langto) Throws jadtexception;

Public grammarchecker getgrammarchecker

"String language"

Public Anagrammizer Getanagrammizer

"String language"

An additional set of classes and interfaces are also provided using the data structure JADT for general methods for providing organizational data. Because all services use the database structure, JADT users know that the data structure is very important. In the next section, I will review the data structure.

The word table interface WordList interface represents a list of words. Because it is the word container, it does not extract data from the backend resource. WordList is generally used to deliver, get, set words, or contain certain words.

The structure of the WordList class is shown in Listing 6:

Listing 6. SampleWordList implementation

Public Class SampleWordList Implements WordList

{

PRIVATE Vector WORDS;

Private int currentpos = 0;

Public textWordList () {/ * default constructor * /}

Public int size () {return Words.size ();

Public Void Addword (Word Word)

{

Words.addeElement (Word);

}

Public Void Addwords (Vector VEC)

{

Words.Addall (VEC);

}

Public Vector getAllWords ()

{

Return Words;

}

Public void start ()

{

CurrentPos = 0;

}

Public Boolean HasmoreWords ()

{

IF (currentpos == words.size ()) Return False;

Else Return True;

}

Public WordList FindwithPrefix (String Find)

{

/ * Implementation * /

}

Public WordList FindwithSuffix (String Find)

{

/ * Implementation * /

}

Public WordList FindwithSUBString (String Find)

{

/ * Implementation * /

}

}

The word interface The Word interface is a combination of characters, representing the language unit that the native language user can identify. The Word interface packages the information about the word. The information saved in the Word interface includes: spelling, type, source, pronunciation, and record. Because this interface represents a word in real format, implementation should provide a container, and the container has a necessary GET and SET methods. The information displayed by Listing 7 is necessary to form a word:

Listing 7. Example word attribute

PRIVATE STRING WORD;

PRIVATE STRING TYPE;

Private DictionaryRecord Meaning;

Private DictionaryRecord synonym;

PRIVATE DICTIONARYRECORD ANTONYM;

PRIVATE STRING PRONUNCIATION;

Public static final int meanings = 1;

Public static final int synonyms = 2;

Public static final int antonyms = 3;

Private string Source = "";

These fields can be accessed through their GET and SET methods.

JADT service JADT defines the interface for a variety of service objects. In the next section, I will introduce the service objects included in the current JADT version one by one.

Dictionary Interface Dictionary interface represents a reference book, and the reference book contains a word table arranged in alphabetical order. Each entry contains interpretation, type, pronunciation, source, and usage.

Dictionary implements all methods in the dictionary interface, but does not include constructor, while using the appropriate load mechanism. The load mechanism can be a negative (lazy), a multiple load policy, depending on the attribute, and can correct the load mechanism.

For example, constructor should load data from a database or file, and then save the data in a dictionary to store, as shown in Listing 8:

Listing 8. SampleDictionary class

Public Class Sampledictionary Implements Dictionary

{

Private hashtable word;

Private string langfrom;

Private string langto;

Public static final int loadWholeOne = 1;

Public static final int loadWholemultiple = 2;

Public static final int runtime = 3;

Public static final int indexing = 4;

Private Int Technique;

Public Sampledictionary (String Strlangfrom, String Strlangto,

String Dir, int method) THROWS JADTEXCEPTION {

/ *

Load the data from storage unit, accounting to statate defined.

* /

}

/ *

REST All Methods

* /

}

The remaining method is just a variant of the getMeaning () method, which is responsible for extracting a single or multiple words from the loaded data. Listing 9 shows an example:

Listing 9. Example GetMeaning method

Public com.ibm.jadt.dictionaryRecord getMeaning (Word Word)

{

IF (Words.Containskey (Word.toString (). Toolcase ())))))

Return (Word.Tostring (). ToolowerCase ())). GetRecord (TextWord.meanings);

Else Return NULL;

}

Dictionary Component Wordbook is a service component that provides information about all related words. These words can be associated according to usage, source, pronunciation, etc. The current version of JADT provides synonyms, antonyms, upper words, next words, overall nouns, partial nouns interface, we have discussed in Part 1.

Implementation requires access to all such data, establish the necessary mechanism from data source load data, as shown in Listing 10:

Listing 10. Example dictionary

Public Class Samplewordbook Implements Wordbook

{

Private hashtable wordssyn;

/ * Private HashTable for REST All Services Data, this Act Like a repository for data * /

Public samplewordbook () {

/ * Loads Required Data INTO Repository * /

}

Public DictionaryRecord GetSynyms (Word Word) throws jadtexception {

WordList Syn = (WordList) Wordssyn.Get (Word.ToString ());

IF (SYN == Null || SYN.SIZE () == 0)

Return NULL;

DictionaryRecord first = NULL;

DictionaryRecord Curr = NULL;

Enumeration enum = syn.getallwords (). Elements ();

While (enum.hasmoreElements ())

{

IF (first == null)

{

Curr = new dictionaryRecord (); first = curr;

}

Else

{

Curr.setNextRecord (New DictionaryRecord ());

Curr = curr.getNextRecord ();

}

Curr.SetWordName ((Word) Enum.nexTelement ()). TOSTRING ());

}

Return first;

}

/ *

Similarly Implement REST All Services Methods

* /

}

Other services, I have already introduced in Part 1 in detail. Here is a quick review, including additional implementation details.

Spelling Inspector SpellChecker class is used to capture words that are missing. This class is nothing new in progress.

It saves data in a private hash table and implements other methods. It also implements an algorithm for check spelling. This service class is the same as other services.

Word table WordLister can be used to obtain words from backend resources. JADT WordLister also provides an option that looks for all words that meet a rule (such as similar prefix, similar suffix, and public sub-string).

The WordLister class saves the data in a private hash table and implements other methods. It also implements an algorithm for check spelling. This service class is the same as other services.

Word Positioner Anagrammizer can give all variations in words or phrases, which is very useful for text gaming applications. This class also implements an algorithm for obtaining the displacement of the word.

GrammarChecker Check the words in the sentence, check the usage of words in a particular context. It also implements an algorithm for checking the syntax. This service class is the same as other services. Translator class TRANSLATOR class is used to convert words or messages from a language into another language. This feature can be used in localization and international implementation. With this characteristics, the resource binding file written in a language can be converted into another language.

Translator implements algorithms that translate words and sentences from a language into another language. This service class is the same as other services.

Once you have implemented all the service interfaces and driver classes, you can bundle all classes in a package. The final package structure looks similar to the list 11:

Listing 11. JADTDRIVER package structure

COM / IBM / JADT

Anagrammizer

Dictionary

DictionaryRecord

Grammarchecker

Jadtdriver

Jadtdriverfactory

JadtdriverFactoryManager

JADTEXCEPTION

SpellChecker

TRANSLATOR

Word

Wordbook

Wordlist

WordLister

COM / IBM / JADTDRIVERS / SAMPLEDRIVER

JadtsampleDriver

JadtsampleDriverfactory

Sampleword

COM / IBM / JADTDRIVERS / SAMPLEDRIVER / Anagrammizer

Sampeanagrammizer

COM / IBM / JADTDRIVERS / SAMPLEDRIVER / DICTIONARY

Sampledictionary

COM / IBM / JADTDRIVERS / SAMPLEDRIVER / SPELLCHECKER

SamplespellChecker

COM / IBM / JADTDRIVERS / SAMPLEDRIVER / WORDBOOK

Samplewordbook

COM / IBM / JADTDRIVERS / SAMPLEDRIVER / WORDLIST

SamplewordList

SamplewordLister

Belting these files into a JAR file, you can deploy it.

Connect the language through this series of articles, you have a comprehensive understanding of JADT technology. This document describes the basic JADT structure, and how to use different JADT services and components to build Java applications related to words related to words.

Reference

You can see this article in our website on our world. Download the Dictionary and theseaurus API for Java from AlphaWorks. Part 1 (DeveloperWorks, September 2004) of this series, introduced JADT and concepts, discussed the various classes and design included in the API, and the available features of JADT. Download IBM XML4J from AlphaWorks. Hundreds of technical articles about Java are found in the DeveloperWorks Java technology area. Please visit developer bookstore to get a complete list of technical books, including hundreds of Java-related topics. Is it interested in IBM test products that don't require usually high cost entrance (Entry Point) or short-term evaluation licenses? DeveloperWorks Kit provides a low-cost 12-month single-user license for WebSphere, DB2, Lotus, Rational, and Tivoli products, including Eclipse-based WebSphere Studio IDE for development, testing, evaluation, and showing your application.

转载请注明原文地址:https://www.9cbs.com/read-105857.html

New Post(0)