[FOray-commit] SF.net SVN: foray:[11980] trunk/foray/foray-hyphen/src
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2021-11-02 18:06:47
|
Revision: 11980
http://sourceforge.net/p/foray/code/11980
Author: victormote
Date: 2021-11-02 18:06:44 +0000 (Tue, 02 Nov 2021)
Log Message:
-----------
Handle "regular" flags. Add more tests.
Modified Paths:
--------------
trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/PosUtils.java
trunk/foray/foray-hyphen/src/test/java/org/foray/hyphen/PosUtilsTests.java
Modified: trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/PosUtils.java
===================================================================
--- trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/PosUtils.java 2021-11-02 17:19:13 UTC (rev 11979)
+++ trunk/foray/foray-hyphen/src/main/java/org/foray/hyphen/PosUtils.java 2021-11-02 18:06:44 UTC (rev 11980)
@@ -49,23 +49,23 @@
/** Mask suitable for accumulating multiple parts of speech in one char. */
private static final char[] MASKS = new char[16];
static {
- MASKS[PartOfSpeech.NOUN.getNumericValue()] = 0x0001; // Index 0.
- MASKS[PartOfSpeech.PRONOUN.getNumericValue()] = 0x0002; // Index 1.
- MASKS[PartOfSpeech.VERB.getNumericValue()] = 0x0004; // Index 2.
- MASKS[PartOfSpeech.ADJECTIVE.getNumericValue()] = 0x0008; // Index 3.
- MASKS[PartOfSpeech.ADVERB.getNumericValue()] = 0x0010; // Index 4.
- MASKS[PartOfSpeech.PREPOSITION.getNumericValue()] = 0x0020; // Index 5.
- MASKS[PartOfSpeech.CONJUNCTION.getNumericValue()] = 0x0040; // Index 6.
- MASKS[PartOfSpeech.ARTICLE.getNumericValue()] = 0x0080; // Index 7.
- MASKS[PartOfSpeech.INTERJECTION.getNumericValue()] = 0x0100; // Index 8.
+ MASKS[PartOfSpeech.NOUN.getNumericValue()] = 0x0001; // Index 0. 1
+ MASKS[PartOfSpeech.PRONOUN.getNumericValue()] = 0x0002; // Index 1. 2
+ MASKS[PartOfSpeech.VERB.getNumericValue()] = 0x0004; // Index 2. 4
+ MASKS[PartOfSpeech.ADJECTIVE.getNumericValue()] = 0x0008; // Index 3. 8
+ MASKS[PartOfSpeech.ADVERB.getNumericValue()] = 0x0010; // Index 4. 16
+ MASKS[PartOfSpeech.PREPOSITION.getNumericValue()] = 0x0020; // Index 5. 32
+ MASKS[PartOfSpeech.CONJUNCTION.getNumericValue()] = 0x0040; // Index 6. 64
+ MASKS[PartOfSpeech.ARTICLE.getNumericValue()] = 0x0080; // Index 7. 128
+ MASKS[PartOfSpeech.INTERJECTION.getNumericValue()] = 0x0100; // Index 8. 256
/* Leave some room in the middle for exapnsion from either end. */
- MASKS[9] = 0x0200; // Index 9.
- MASKS[10] = 0x0400; // Index 10.
- MASKS[11] = 0x0800; // Index 11.
- MASKS[12] = 0x1000; // Index 12.
- MASKS[REGULAR_NOUN_INDEX] = 0x2000; // Index 13.
- MASKS[REGULAR_VERB_INDEX] = 0x4000; // Index 14.
- MASKS[REGULAR_ADJECTIVE_INDEX] = 0x8000; // Index 15.
+ MASKS[9] = 0x0200; // Index 9. 512
+ MASKS[10] = 0x0400; // Index 10. 1,024
+ MASKS[11] = 0x0800; // Index 11. 2,048
+ MASKS[12] = 0x1000; // Index 12. 4,096
+ MASKS[REGULAR_NOUN_INDEX] = 0x2000; // Index 13. 8,192
+ MASKS[REGULAR_VERB_INDEX] = 0x4000; // Index 14. 16,384
+ MASKS[REGULAR_ADJECTIVE_INDEX] = 0x8000; // Index 15. 32,768
/* */
}
@@ -88,19 +88,85 @@
return (char) (existing | mask);
}
+ /**
+ * Adds the "regular noun" flag to the existing flags.
+ * @param existing The existing flags.
+ * @return The new flags, consisting of all existing flags plus the one just added.
+ */
public static char encodeRegularNoun(final char existing) {
+ if (! PosUtils.isPartOfSpeech(existing, PartOfSpeech.NOUN)) {
+ throw new IllegalStateException("Cannot set \"regular noun\" flag unless already a noun.");
+ }
final char mask = MASKS[REGULAR_NOUN_INDEX];
return (char) (existing | mask);
}
+ /**
+ * Adds the "regular verb" flag to the existing flags.
+ * @param existing The existing flags.
+ * @return The new flags, consisting of all existing flags plus the one just added.
+ */
public static char encodeRegularVerb(final char existing) {
+ if (! PosUtils.isPartOfSpeech(existing, PartOfSpeech.VERB)) {
+ throw new IllegalStateException("Cannot set \"regular verb\" flag unless already a verb.");
+ }
final char mask = MASKS[REGULAR_VERB_INDEX];
return (char) (existing | mask);
}
+ /**
+ * Adds the "regular adjective" flag to the existing flags.
+ * @param existing The existing flags.
+ * @return The new flags, consisting of all existing flags plus the one just added.
+ */
public static char encodeRegularAdjective(final char existing) {
+ if (! PosUtils.isPartOfSpeech(existing, PartOfSpeech.ADJECTIVE)) {
+ throw new IllegalStateException("Cannot set \"regular adjective\" flag unless already an adjective.");
+ }
final char mask = MASKS[REGULAR_ADJECTIVE_INDEX];
return (char) (existing | mask);
}
+ /**
+ * Indicates whether a given part of speech flag is set.
+ * @param flags The flags being tested.
+ * @param pos The part of speech being tested for.
+ * @return True if and only if the flag is set for {@code pos}.
+ */
+ public static boolean isPartOfSpeech(final char flags, final PartOfSpeech pos) {
+ final int index = pos.getNumericValue();
+ final char mask = MASKS[index];
+ return (flags & mask) != 0;
+ }
+
+ /**
+ * Indicates whether the "regular noun" flag is set.
+ * @param flags The flags being tested.
+ * @return True if and only if the "regular noun" flag is set.
+ */
+ public static boolean isRegularNoun(final char flags) {
+ final char mask = MASKS[REGULAR_NOUN_INDEX];
+ return (flags & mask) != 0;
+ }
+
+ /**
+ * Indicates whether the "regular verb" flag is set.
+ * @param flags The flags being tested.
+ * @return True if and only if the "regular verb" flag is set.
+ */
+ public static boolean isRegularVerb(final char flags) {
+ final char mask = MASKS[REGULAR_VERB_INDEX];
+ return (flags & mask) != 0;
+ }
+
+ /**
+ * Indicates whether the "regular adjective" flag is set.
+ * @param flags The flags being tested.
+ * @return True if and only if the "regular adjective" flag is set.
+ */
+ public static boolean isRegularAdjective(final char flags) {
+ final char mask = MASKS[REGULAR_ADJECTIVE_INDEX];
+ return (flags & mask) != 0;
+ }
+
}
Modified: trunk/foray/foray-hyphen/src/test/java/org/foray/hyphen/PosUtilsTests.java
===================================================================
--- trunk/foray/foray-hyphen/src/test/java/org/foray/hyphen/PosUtilsTests.java 2021-11-02 17:19:13 UTC (rev 11979)
+++ trunk/foray/foray-hyphen/src/test/java/org/foray/hyphen/PosUtilsTests.java 2021-11-02 18:06:44 UTC (rev 11980)
@@ -69,17 +69,17 @@
/* Add 8192. */
running = PosUtils.encodeRegularNoun(running);
- Assert.assertEquals(511 + 8192, running);
+ Assert.assertEquals(511 + 8_192, running);
/* Add 16,384. */
running = PosUtils.encodeRegularVerb(running);
- Assert.assertEquals(511 + 8192 + 16384, running);
+ Assert.assertEquals(511 + 8_192 + 16_384, running);
/* Add 32,768. */
running = PosUtils.encodeRegularAdjective(running);
- Assert.assertEquals(511 + 8192 + 16384 + 32768, running);
+ Assert.assertEquals(511 + 8_192 + 16_384 + 32_768, running);
}
/**
- * Test of {@link PosUtils#encodePosInfo(char, org.axsl.hyphen.PartOfSpeech)} and related encoding methods.
+ * Test of {@link PosUtils#encodePosInfo(char, org.axsl.hyphen.PartOfSpeech)}.
*/
@Test
public void testOfOrInsteadOfAdd() {
@@ -91,4 +91,86 @@
Assert.assertEquals(256, running);
}
+ /**
+ * Test of {@link PosUtils#encodeRegularNoun(char)} and related, to ensure that an exception is thrown if the
+ * related part-of-speech flag is not set.
+ */
+ @Test
+ public void testOfIllegalStates() {
+ char flags = 0;
+ try {
+ flags = PosUtils.encodeRegularNoun(flags);
+ Assert.fail("Exception expected.");
+ } catch (final IllegalStateException e) {
+ /* This is the expected case. */
+ Assert.assertEquals("Cannot set \"regular noun\" flag unless already a noun.", e.getMessage());
+ }
+
+ flags = 0;
+ try {
+ flags = PosUtils.encodeRegularVerb(flags);
+ Assert.fail("Exception expected.");
+ } catch (final IllegalStateException e) {
+ /* This is the expected case. */
+ Assert.assertEquals("Cannot set \"regular verb\" flag unless already a verb.", e.getMessage());
+ }
+
+ flags = 0;
+ try {
+ flags = PosUtils.encodeRegularAdjective(flags);
+ Assert.fail("Exception expected.");
+ } catch (final IllegalStateException e) {
+ /* This is the expected case. */
+ Assert.assertEquals("Cannot set \"regular adjective\" flag unless already an adjective.", e.getMessage());
+ }
+
+ /* Now make sure the normal cases work correctly. */
+ flags = 0;
+ flags = PosUtils.encodePosInfo(flags, PartOfSpeech.NOUN);
+ Assert.assertEquals(1, flags);
+ flags = PosUtils.encodeRegularNoun(flags);
+ Assert.assertEquals(1 + 8_192, flags);
+ Assert.assertTrue(PosUtils.isPartOfSpeech(flags, PartOfSpeech.NOUN));
+ Assert.assertTrue(PosUtils.isRegularNoun(flags));
+
+ /* Now make sure the normal cases work correctly. */
+ flags = 0;
+ flags = PosUtils.encodePosInfo(flags, PartOfSpeech.VERB);
+ Assert.assertEquals(4, flags);
+ flags = PosUtils.encodeRegularVerb(flags);
+ Assert.assertEquals(4 + 16_384, flags);
+ Assert.assertTrue(PosUtils.isPartOfSpeech(flags, PartOfSpeech.VERB));
+ Assert.assertTrue(PosUtils.isRegularVerb(flags));
+
+ /* Now make sure the normal cases work correctly. */
+ flags = 0;
+ flags = PosUtils.encodePosInfo(flags, PartOfSpeech.ADJECTIVE);
+ Assert.assertEquals(8, flags);
+ flags = PosUtils.encodeRegularAdjective(flags);
+ Assert.assertEquals(8 + 32_768, flags);
+ Assert.assertTrue(PosUtils.isPartOfSpeech(flags, PartOfSpeech.ADJECTIVE));
+ Assert.assertTrue(PosUtils.isRegularAdjective(flags));
+ }
+
+ /**
+ * Test of {@link PosUtils#isPartOfSpeech(char, PartOfSpeech)} and related encoding methods.
+ */
+ @Test
+ public void testIsPartOfSpeech() {
+ char flags = 0;
+ flags = PosUtils.encodePosInfo(flags, PartOfSpeech.PREPOSITION);
+ Assert.assertFalse(PosUtils.isPartOfSpeech(flags, PartOfSpeech.NOUN));
+ Assert.assertFalse(PosUtils.isPartOfSpeech(flags, PartOfSpeech.PRONOUN));
+ Assert.assertFalse(PosUtils.isPartOfSpeech(flags, PartOfSpeech.VERB));
+ Assert.assertFalse(PosUtils.isPartOfSpeech(flags, PartOfSpeech.ADJECTIVE));
+ Assert.assertFalse(PosUtils.isPartOfSpeech(flags, PartOfSpeech.ADVERB));
+ Assert.assertTrue(PosUtils.isPartOfSpeech(flags, PartOfSpeech.PREPOSITION));
+ Assert.assertFalse(PosUtils.isPartOfSpeech(flags, PartOfSpeech.CONJUNCTION));
+ Assert.assertFalse(PosUtils.isPartOfSpeech(flags, PartOfSpeech.ARTICLE));
+ Assert.assertFalse(PosUtils.isPartOfSpeech(flags, PartOfSpeech.INTERJECTION));
+ Assert.assertFalse(PosUtils.isRegularNoun(flags));
+ Assert.assertFalse(PosUtils.isRegularVerb(flags));
+ Assert.assertFalse(PosUtils.isRegularAdjective(flags));
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|