From d9b0abfb3274c4ce60e77d220161ee2359966e83 Mon Sep 17 00:00:00 2001 From: Jan Keim Date: Tue, 19 Sep 2023 12:20:54 +0200 Subject: [PATCH] Remove duplicate PosTag class --- .../mcse/ardoco/core/api/text/POSTag.java | 20 ++++++- .../converter/ObjectToDtoConverter.java | 12 ++-- .../core/textproviderjson/dto/PosTag.java | 58 ------------------- .../core/textproviderjson/dto/WordDto.java | 10 ++-- .../core/textproviderjson/TestUtil.java | 56 +++++++++--------- .../converter/JsonConverterTest.java | 4 +- 6 files changed, 65 insertions(+), 95 deletions(-) delete mode 100644 framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/dto/PosTag.java diff --git a/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/api/text/POSTag.java b/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/api/text/POSTag.java index 935df071f..eed06d529 100644 --- a/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/api/text/POSTag.java +++ b/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/api/text/POSTag.java @@ -1,12 +1,15 @@ /* Licensed under MIT 2021-2023. */ package edu.kit.kastel.mcse.ardoco.core.api.text; +import java.io.IOException; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; /** * This class represents all valid part-of-speech (pos) tags - * */ public enum POSTag { //@formatter:off @@ -77,4 +80,19 @@ public boolean isVerb() { public boolean isNoun() { return getTag().startsWith("NN"); } + + @JsonValue + public String toValue() { + return getTag(); + } + + @JsonCreator + public static POSTag forValue(String value) throws IOException { + try { + return get(value); + } catch (IllegalArgumentException e) { + throw new IOException("Cannot deserialize PosTag"); + } + } + } diff --git a/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/converter/ObjectToDtoConverter.java b/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/converter/ObjectToDtoConverter.java index 0d19e99ff..6bf8b7ed2 100644 --- a/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/converter/ObjectToDtoConverter.java +++ b/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/converter/ObjectToDtoConverter.java @@ -9,10 +9,14 @@ import org.eclipse.collections.api.list.ImmutableList; -import edu.kit.kastel.mcse.ardoco.core.api.text.*; +import edu.kit.kastel.mcse.ardoco.core.api.text.DependencyTag; +import edu.kit.kastel.mcse.ardoco.core.api.text.POSTag; +import edu.kit.kastel.mcse.ardoco.core.api.text.Phrase; +import edu.kit.kastel.mcse.ardoco.core.api.text.Sentence; +import edu.kit.kastel.mcse.ardoco.core.api.text.Text; +import edu.kit.kastel.mcse.ardoco.core.api.text.Word; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.IncomingDependencyDto; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.OutgoingDependencyDto; -import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.PosTag; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.SentenceDto; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.TextDto; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.WordDto; @@ -27,7 +31,7 @@ public class ObjectToDtoConverter { /** * converts an ArDoCo text into a text DTO - * + * * @param text the ArDoCo text * @return the text DTO */ @@ -74,7 +78,7 @@ private WordDto convertToWordDTO(Word word) throws NotConvertableException { wordDTO.setText(word.getText()); wordDTO.setLemma(word.getLemma()); try { - wordDTO.setPosTag(PosTag.forValue(word.getPosTag().toString())); + wordDTO.setPosTag(POSTag.forValue(word.getPosTag().toString())); } catch (IOException e) { throw new NotConvertableException(String.format("IOException when converting word with id %d to WordDto: PosTag not found.", wordDTO.getId())); } diff --git a/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/dto/PosTag.java b/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/dto/PosTag.java deleted file mode 100644 index 8b9415729..000000000 --- a/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/dto/PosTag.java +++ /dev/null @@ -1,58 +0,0 @@ -/* Licensed under MIT 2023. */ -package edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto; - -import java.io.IOException; - -import com.fasterxml.jackson.annotation.*; - -public enum PosTag { - //@formatter:off - ADJECTIVE("JJ"), ADJECTIVE_COMPARATIVE(ADJECTIVE + "R"), ADJECTIVE_SUPERLATIVE(ADJECTIVE + "S"), ADVERB("RB"), ADVERB_COMPARATIVE(ADVERB + "R"), ADVERB_SUPERLATIVE(ADVERB + "S"), ADVERB_WH( - "W" + ADVERB), CONJUNCTION_COORDINATING("CC"), CONJUNCTION_SUBORDINATING("IN"), CARDINAL_NUMBER("CD"), DETERMINER("DT"), DETERMINER_WH("W" + DETERMINER), EXISTENTIAL_THERE( - "EX"), FOREIGN_WORD("FW"), LIST_ITEM_MARKER("LS"), NOUN("NN"), NOUN_PLURAL(NOUN + "S"), NOUN_PROPER_SINGULAR(NOUN + "P"), NOUN_PROPER_PLURAL(NOUN + "PS"), PREDETERMINER( - "PDT"), POSSESSIVE_ENDING("POS"), PRONOUN_PERSONAL("PRP"), PRONOUN_POSSESSIVE("PRP$"), PRONOUN_POSSESSIVE_WH("WP$"), PRONOUN_WH("WP"), PARTICLE("RP"), SYMBOL("SYM"), TO( - "TO"), INTERJECTION("UH"), VERB("VB"), VERB_PAST_TENSE(VERB + "D"), VERB_PARTICIPLE_PRESENT(VERB + "G"), VERB_PARTICIPLE_PAST(VERB + "N"), VERB_SINGULAR_PRESENT_NONTHIRD_PERSON( - VERB + "P"), VERB_SINGULAR_PRESENT_THIRD_PERSON(VERB + "Z"), VERB_MODAL("MD"), CLOSER("."), COMMA(","), COLON(":"), LEFT_PAREN("-LRB-"), RIGHT_PAREN("-RRB-"), NONE("-NONE-"), OPEN_QUOTE( - "``"), CLOSE_QUOTE("''"), DOLLAR("$"), HASHTAG("#"), HYPH("HYPH"), NFP("NFP"), ADD("ADD"), AFX("AFX"), GW("GW"), XX("XX"); - //@formatter:on - - private final String tag; - - PosTag(String tag) { - this.tag = tag; - } - - /** - * Returns the encoding for this part-of-speech. - * - * @return A string representing a Penn Treebank encoding for an English part-of-speech. - */ - @Override - public String toString() { - return getTag(); - } - - /** - * Gets the tag information. - * - * @return the tag - */ - public String getTag() { - return tag; - } - - @JsonValue - public String toValue() { - return getTag(); - } - - @JsonCreator - public static PosTag forValue(String value) throws IOException { - for (PosTag v : values()) { - if (value.equals(v.getTag())) { - return v; - } - } - throw new IOException("Cannot deserialize PosTag"); - } -} diff --git a/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/dto/WordDto.java b/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/dto/WordDto.java index cb3f0c069..0886c8ac3 100644 --- a/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/dto/WordDto.java +++ b/framework/text-provider-json/src/main/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/dto/WordDto.java @@ -5,7 +5,9 @@ import java.util.List; import java.util.Objects; -import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.annotation.JsonProperty; + +import edu.kit.kastel.mcse.ardoco.core.api.text.POSTag; /** * Definition of a word @@ -17,7 +19,7 @@ public class WordDto { private List outgoingDependencies = new ArrayList<>(); private long sentenceNo; private String text; - private PosTag posTag; + private POSTag posTag; /** * The id of the word. Should be ascending from 1 for the first word in the text. @@ -72,12 +74,12 @@ public void setOutgoingDependencies(List value) { } @JsonProperty("posTag") - public PosTag getPosTag() { + public POSTag getPosTag() { return posTag; } @JsonProperty("posTag") - public void setPosTag(PosTag value) { + public void setPosTag(POSTag value) { this.posTag = value; } diff --git a/framework/text-provider-json/src/test/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/TestUtil.java b/framework/text-provider-json/src/test/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/TestUtil.java index e362e16ca..5efae0cb4 100644 --- a/framework/text-provider-json/src/test/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/TestUtil.java +++ b/framework/text-provider-json/src/test/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/TestUtil.java @@ -7,10 +7,14 @@ import org.eclipse.collections.api.factory.Lists; -import edu.kit.kastel.mcse.ardoco.core.api.text.*; +import edu.kit.kastel.mcse.ardoco.core.api.text.DependencyTag; +import edu.kit.kastel.mcse.ardoco.core.api.text.POSTag; +import edu.kit.kastel.mcse.ardoco.core.api.text.Phrase; +import edu.kit.kastel.mcse.ardoco.core.api.text.PhraseType; +import edu.kit.kastel.mcse.ardoco.core.api.text.Sentence; +import edu.kit.kastel.mcse.ardoco.core.api.text.Text; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.IncomingDependencyDto; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.OutgoingDependencyDto; -import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.PosTag; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.SentenceDto; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.TextDto; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.WordDto; @@ -30,7 +34,7 @@ private TestUtil() { /** * generates a default textDTO without dependencies between the words - * + * * @return a default textDTO */ public static TextDto generateDefaultDTO() throws IOException { @@ -39,28 +43,28 @@ public static TextDto generateDefaultDTO() throws IOException { word1.setSentenceNo(1); word1.setLemma("this"); word1.setText("This"); - word1.setPosTag(PosTag.forValue("DT")); + word1.setPosTag(POSTag.forValue("DT")); WordDto word2 = new WordDto(); word2.setId(2); word2.setSentenceNo(1); word2.setLemma("be"); word2.setText("is"); - word2.setPosTag(PosTag.forValue("VBZ")); + word2.setPosTag(POSTag.forValue("VBZ")); WordDto word3 = new WordDto(); word3.setId(3); word3.setSentenceNo(1); word3.setLemma("I"); word3.setText("me"); - word3.setPosTag(PosTag.forValue("PRP")); + word3.setPosTag(POSTag.forValue("PRP")); WordDto word4 = new WordDto(); word4.setId(4); word4.setSentenceNo(1); word4.setLemma("."); word4.setText("."); - word4.setPosTag(PosTag.forValue(".")); + word4.setPosTag(POSTag.forValue(".")); List words = new ArrayList<>(List.of(word1, word2, word3, word4)); @@ -87,9 +91,9 @@ public static TextDto generateDefaultDTO() throws IOException { public static Text generateDefaultText() { TextImpl text = new TextImpl(); List words = new ArrayList<>(List.of(new WordImpl(text, 0, 0, "This", POSTag.DETERMINER, "this", new ArrayList<>(), new ArrayList<>()), - new WordImpl(text, 1, 0, "is", POSTag.VERB_SINGULAR_PRESENT_THIRD_PERSON, "be", new ArrayList<>(), new ArrayList<>()), new WordImpl(text, 2, 0, - "me", POSTag.PRONOUN_PERSONAL, "I", new ArrayList<>(), new ArrayList<>()), new WordImpl(text, 3, 0, ".", POSTag.CLOSER, ".", - new ArrayList<>(), new ArrayList<>()))); + new WordImpl(text, 1, 0, "is", POSTag.VERB_SINGULAR_PRESENT_THIRD_PERSON, "be", new ArrayList<>(), new ArrayList<>()), + new WordImpl(text, 2, 0, "me", POSTag.PRONOUN_PERSONAL, "I", new ArrayList<>(), new ArrayList<>()), + new WordImpl(text, 3, 0, ".", POSTag.CLOSER, ".", new ArrayList<>(), new ArrayList<>()))); SentenceImpl sentence1 = new SentenceImpl(0, "This is me.", Lists.immutable.ofAll(words)); @@ -126,28 +130,28 @@ public static TextDto generateDTOWithMultipleSentences() throws IOException { word1.setSentenceNo(1); word1.setLemma("this"); word1.setText("This"); - word1.setPosTag(PosTag.forValue("DT")); + word1.setPosTag(POSTag.forValue("DT")); WordDto word2 = new WordDto(); word2.setId(2); word2.setSentenceNo(1); word2.setLemma("be"); word2.setText("is"); - word2.setPosTag(PosTag.forValue("VBZ")); + word2.setPosTag(POSTag.forValue("VBZ")); WordDto word3 = new WordDto(); word3.setId(3); word3.setSentenceNo(1); word3.setLemma("I"); word3.setText("me"); - word3.setPosTag(PosTag.forValue("PRP")); + word3.setPosTag(POSTag.forValue("PRP")); WordDto word4 = new WordDto(); word4.setId(4); word4.setSentenceNo(1); word4.setLemma("."); word4.setText("."); - word4.setPosTag(PosTag.forValue(".")); + word4.setPosTag(POSTag.forValue(".")); List words = new ArrayList<>(List.of(word1, word2, word3, word4)); @@ -162,28 +166,28 @@ public static TextDto generateDTOWithMultipleSentences() throws IOException { word5.setSentenceNo(2); word5.setLemma("this"); word5.setText("This"); - word5.setPosTag(PosTag.forValue("DT")); + word5.setPosTag(POSTag.forValue("DT")); WordDto word6 = new WordDto(); word6.setId(6); word6.setSentenceNo(2); word6.setLemma("be"); word6.setText("is"); - word6.setPosTag(PosTag.forValue("VBZ")); + word6.setPosTag(POSTag.forValue("VBZ")); WordDto word7 = new WordDto(); word7.setId(7); word7.setSentenceNo(2); word7.setLemma("you"); word7.setText("you"); - word7.setPosTag(PosTag.forValue("PRP")); + word7.setPosTag(POSTag.forValue("PRP")); WordDto word8 = new WordDto(); word8.setId(8); word8.setSentenceNo(2); word8.setLemma("."); word8.setText("."); - word8.setPosTag(PosTag.forValue(".")); + word8.setPosTag(POSTag.forValue(".")); List words2 = new ArrayList<>(List.of(word5, word6, word7, word8)); @@ -203,9 +207,9 @@ public static Text generateTextWithMultipleSentences() { List sentences = new ArrayList<>(); List words = new ArrayList<>(List.of(new WordImpl(text, 0, 0, "This", POSTag.DETERMINER, "this", new ArrayList<>(), new ArrayList<>()), - new WordImpl(text, 1, 0, "is", POSTag.VERB_SINGULAR_PRESENT_THIRD_PERSON, "be", new ArrayList<>(), new ArrayList<>()), new WordImpl(text, 2, 0, - "me", POSTag.PRONOUN_PERSONAL, "I", new ArrayList<>(), new ArrayList<>()), new WordImpl(text, 3, 0, ".", POSTag.CLOSER, ".", - new ArrayList<>(), new ArrayList<>()))); + new WordImpl(text, 1, 0, "is", POSTag.VERB_SINGULAR_PRESENT_THIRD_PERSON, "be", new ArrayList<>(), new ArrayList<>()), + new WordImpl(text, 2, 0, "me", POSTag.PRONOUN_PERSONAL, "I", new ArrayList<>(), new ArrayList<>()), + new WordImpl(text, 3, 0, ".", POSTag.CLOSER, ".", new ArrayList<>(), new ArrayList<>()))); SentenceImpl sentence1 = new SentenceImpl(0, "This is me.", Lists.immutable.ofAll(words)); @@ -222,9 +226,9 @@ public static Text generateTextWithMultipleSentences() { sentences.add(sentence1); List words2 = new ArrayList<>(List.of(new WordImpl(text, 4, 1, "This", POSTag.DETERMINER, "this", new ArrayList<>(), new ArrayList<>()), - new WordImpl(text, 5, 1, "is", POSTag.VERB_SINGULAR_PRESENT_THIRD_PERSON, "be", new ArrayList<>(), new ArrayList<>()), new WordImpl(text, 6, 1, - "you", POSTag.PRONOUN_PERSONAL, "you", new ArrayList<>(), new ArrayList<>()), new WordImpl(text, 7, 1, ".", POSTag.CLOSER, ".", - new ArrayList<>(), new ArrayList<>()))); + new WordImpl(text, 5, 1, "is", POSTag.VERB_SINGULAR_PRESENT_THIRD_PERSON, "be", new ArrayList<>(), new ArrayList<>()), + new WordImpl(text, 6, 1, "you", POSTag.PRONOUN_PERSONAL, "you", new ArrayList<>(), new ArrayList<>()), + new WordImpl(text, 7, 1, ".", POSTag.CLOSER, ".", new ArrayList<>(), new ArrayList<>()))); SentenceImpl sentence2 = new SentenceImpl(1, "This is you.", Lists.immutable.ofAll(words2)); Phrase subsubphrase2 = new PhraseImpl(Lists.immutable.of(words2.get(2)), PhraseType.NP, new ArrayList<>()); @@ -268,7 +272,7 @@ public static TextDto generateTextDtoWithDependencies() throws IOException { word1.setSentenceNo(1); word1.setLemma("hello"); word1.setText("Hello"); - word1.setPosTag(PosTag.forValue("UH")); + word1.setPosTag(POSTag.forValue("UH")); OutgoingDependencyDto outgoingDependency = new OutgoingDependencyDto(); outgoingDependency.setTargetWordId(2); outgoingDependency.setDependencyTag(DependencyTag.PUNCT); @@ -279,7 +283,7 @@ public static TextDto generateTextDtoWithDependencies() throws IOException { word2.setSentenceNo(1); word2.setLemma("."); word2.setText("."); - word2.setPosTag(PosTag.forValue(".")); + word2.setPosTag(POSTag.forValue(".")); IncomingDependencyDto incomingDependency = new IncomingDependencyDto(); incomingDependency.setSourceWordId(1); incomingDependency.setDependencyTag(DependencyTag.PUNCT); diff --git a/framework/text-provider-json/src/test/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/converter/JsonConverterTest.java b/framework/text-provider-json/src/test/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/converter/JsonConverterTest.java index 591438e1e..5e38ab8b8 100644 --- a/framework/text-provider-json/src/test/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/converter/JsonConverterTest.java +++ b/framework/text-provider-json/src/test/java/edu/kit/kastel/mcse/ardoco/core/textproviderjson/converter/JsonConverterTest.java @@ -11,9 +11,9 @@ import org.junit.jupiter.api.Test; import edu.kit.kastel.mcse.ardoco.core.api.text.DependencyTag; +import edu.kit.kastel.mcse.ardoco.core.api.text.POSTag; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.IncomingDependencyDto; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.OutgoingDependencyDto; -import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.PosTag; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.SentenceDto; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.TextDto; import edu.kit.kastel.mcse.ardoco.core.textproviderjson.dto.WordDto; @@ -65,7 +65,7 @@ private TextDto getValidTextDtoExample() throws IOException { expectedWord.setSentenceNo(1); expectedWord.setLemma("hello"); expectedWord.setText("Hello"); - expectedWord.setPosTag(PosTag.forValue("UH")); + expectedWord.setPosTag(POSTag.forValue("UH")); OutgoingDependencyDto expectedOutDep = new OutgoingDependencyDto(); expectedOutDep.setTargetWordId(1);