Revision: 12056
http://sourceforge.net/p/foray/code/12056
Author: victormote
Date: 2021-11-13 18:00:37 +0000 (Sat, 13 Nov 2021)
Log Message:
-----------
Add simple dictionary implementation, primarily for testing, but perhaps useful other places.
Added Paths:
-----------
trunk/foray/foray-orthography/src/main/java/org/foray/orthography/SimpleDictionary.java
Added: trunk/foray/foray-orthography/src/main/java/org/foray/orthography/SimpleDictionary.java
===================================================================
--- trunk/foray/foray-orthography/src/main/java/org/foray/orthography/SimpleDictionary.java (rev 0)
+++ trunk/foray/foray-orthography/src/main/java/org/foray/orthography/SimpleDictionary.java 2021-11-13 18:00:37 UTC (rev 12056)
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2021 The FOray Project.
+ * http://www.foray.org
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * This work is in part derived from the following work(s), used with the
+ * permission of the licensor:
+ * Apache FOP, licensed by the Apache Software Foundation
+ *
+ */
+
+/*
+ * $LastChangedRevision$
+ * $LastChangedDate$
+ * $LastChangedBy$
+ */
+
+package org.foray.orthography;
+
+import org.axsl.orthography.Word;
+import org.axsl.orthography.Word.PartOfSpeech;
+import org.axsl.orthography.Word.PosQualifier;
+import org.axsl.orthography.optional.Dictionary;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * A simple dictionary implementation, possibly useful only for testing.
+ */
+public class SimpleDictionary implements Dictionary {
+
+ /** Constant needed for serialization. */
+ private static final long serialVersionUID = 1797657088308334420L;
+
+ /** Regex pattern used to break compound words into their components. */
+ private Pattern wordBreaker;
+
+ /** The map of words. */
+ private Map<String, Word> words = new HashMap<String, Word>();
+
+ public SimpleDictionary(final char softHyphenChar) {
+ this.wordBreaker = Pattern.compile(Character.toString(softHyphenChar));
+ }
+
+ /**
+ * Add a word to this dictionary.
+ * @param wordChars The characters in the word, e.g. "running", "apple", etc.
+ * @param word The word.
+ */
+ public void addWord(final CharSequence wordChars, final Word word) {
+ final String wordString = wordChars.toString();
+ if (this.words.containsKey(wordString)) {
+ throw new IllegalArgumentException("Word already exists in dictionary: " + wordString);
+ }
+ this.words.put(wordString, word);
+ }
+
+ /**
+ * Add a word to this dictionary.
+ * @param wordChars The characters in the word, including raw hyphenation points, e.g. "run-ning", "ap-ple", etc.
+ */
+ public void addWord(final CharSequence wordChars) {
+ final char partsOfSpeech = 0;
+ final String[] components = this.wordBreaker.split(wordChars);
+ final StringWord word = new StringWord(partsOfSpeech, components);
+ final String wordString = word.getActualContent().toString();
+ if (this.words.containsKey(wordString)) {
+ throw new IllegalArgumentException("Word already exists in dictionary: " + wordString);
+ }
+ this.words.put(wordString, word);
+ }
+
+ @Override
+ public int qtyAlternatives(final CharSequence wordChars) {
+ if (this.words.containsKey(wordChars.toString())) {
+ return 1;
+ }
+ return 0;
+ }
+
+ @Override
+ public Word getWord(final CharSequence wordChars, final int index) {
+ if (index != 0) {
+ throw new IndexOutOfBoundsException("Index must be zero.");
+ }
+ return this.words.get(wordChars.toString());
+ }
+
+ @Override
+ public boolean supportsQualifiedType(final PartOfSpeech arg0, final PosQualifier arg1) {
+ return false;
+ }
+
+}
Property changes on: trunk/foray/foray-orthography/src/main/java/org/foray/orthography/SimpleDictionary.java
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Rev
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|