|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 Contributors to the openHAB project |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | +package marytts.modules; |
| 14 | + |
| 15 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 16 | + |
| 17 | +import java.io.BufferedReader; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.io.InputStreamReader; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.StringTokenizer; |
| 25 | + |
| 26 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 27 | +import org.eclipse.jdt.annotation.Nullable; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | +import org.w3c.dom.Document; |
| 31 | +import org.w3c.dom.Element; |
| 32 | +import org.w3c.dom.traversal.NodeIterator; |
| 33 | +import org.w3c.dom.traversal.TreeWalker; |
| 34 | + |
| 35 | +import marytts.datatypes.MaryData; |
| 36 | +import marytts.datatypes.MaryDataType; |
| 37 | +import marytts.server.MaryProperties; |
| 38 | +import marytts.util.MaryUtils; |
| 39 | +import marytts.util.dom.MaryDomUtils; |
| 40 | +import opennlp.tools.postag.POSModel; |
| 41 | +import opennlp.tools.postag.POSTagFormat; |
| 42 | +import opennlp.tools.postag.POSTaggerME; |
| 43 | + |
| 44 | +/** |
| 45 | + * OpenNLP POS tagger compatible with the model and tag set expected by MaryTTS 5.2.1. |
| 46 | + * |
| 47 | + * <p> |
| 48 | + * MaryTTS 5.2.1 uses OpenNLP's removed {@code tag(List)} API and the legacy one-argument constructor. This replacement |
| 49 | + * uses the current array API and explicitly retains Penn Treebank output, on which MaryTTS's English prosody rules |
| 50 | + * depend. |
| 51 | + * |
| 52 | + * @author Leo Siepel - Initial contribution |
| 53 | + */ |
| 54 | +@NonNullByDefault |
| 55 | +public class OpenNLPPosTagger extends InternalModule { |
| 56 | + |
| 57 | + private static final Logger LOGGER = LoggerFactory.getLogger(OpenNLPPosTagger.class); |
| 58 | + |
| 59 | + private final String propertyPrefix; |
| 60 | + |
| 61 | + private @Nullable POSTaggerME tagger; |
| 62 | + private @Nullable Map<String, String> posMapper; |
| 63 | + |
| 64 | + public OpenNLPPosTagger(String locale, String propertyPrefix) throws Exception { |
| 65 | + super("OpenNLPPosTagger", MaryDataType.WORDS, MaryDataType.PARTSOFSPEECH, MaryUtils.string2locale(locale)); |
| 66 | + this.propertyPrefix = propertyPrefix.endsWith(".") ? propertyPrefix : propertyPrefix + "."; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void startup() throws Exception { |
| 71 | + super.startup(); |
| 72 | + |
| 73 | + try (InputStream modelStream = MaryProperties.needStream(propertyPrefix + "model")) { |
| 74 | + tagger = new POSTaggerME(new POSModel(modelStream), POSTagFormat.PENN); |
| 75 | + } |
| 76 | + |
| 77 | + InputStream posMapStream = MaryProperties.getStream(propertyPrefix + "posMap"); |
| 78 | + if (posMapStream != null) { |
| 79 | + posMapper = readPosMapper(posMapStream); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private Map<String, String> readPosMapper(InputStream posMapStream) throws Exception { |
| 84 | + Map<String, String> mapper = new HashMap<>(); |
| 85 | + try (posMapStream; BufferedReader reader = new BufferedReader(new InputStreamReader(posMapStream, UTF_8))) { |
| 86 | + String line; |
| 87 | + while ((line = reader.readLine()) != null) { |
| 88 | + if (!line.startsWith("#") && !line.isBlank()) { |
| 89 | + StringTokenizer tokenizer = new StringTokenizer(line); |
| 90 | + mapper.put(tokenizer.nextToken(), tokenizer.nextToken()); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return mapper; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public MaryData process(@NonNullByDefault({}) MaryData input) throws Exception { |
| 99 | + Document document = input.getDocument(); |
| 100 | + NodeIterator sentenceIterator = MaryDomUtils.createNodeIterator(document, document, "s"); |
| 101 | + |
| 102 | + Element sentence; |
| 103 | + while ((sentence = (Element) sentenceIterator.nextNode()) != null) { |
| 104 | + tagSentence(sentence); |
| 105 | + } |
| 106 | + |
| 107 | + MaryData output = new MaryData(getOutputType(), input.getLocale()); |
| 108 | + output.setDocument(document); |
| 109 | + return output; |
| 110 | + } |
| 111 | + |
| 112 | + private void tagSentence(Element sentence) { |
| 113 | + TreeWalker tokenWalker = MaryDomUtils.createTreeWalker(sentence, "t"); |
| 114 | + List<Element> tokens = new ArrayList<>(); |
| 115 | + List<String> words = new ArrayList<>(); |
| 116 | + |
| 117 | + Element token; |
| 118 | + while ((token = (Element) tokenWalker.nextNode()) != null) { |
| 119 | + tokens.add(token); |
| 120 | + words.add(MaryDomUtils.tokenText(token)); |
| 121 | + } |
| 122 | + |
| 123 | + // OpenNLP cannot determine useful context from a one-token sentence. |
| 124 | + if (words.size() == 1) { |
| 125 | + words.add("."); |
| 126 | + } |
| 127 | + |
| 128 | + String[] tags; |
| 129 | + synchronized (this) { |
| 130 | + POSTaggerME tagger = this.tagger; |
| 131 | + if (tagger != null) { |
| 132 | + tags = tagger.tag(words.toArray(String[]::new)); |
| 133 | + } else { |
| 134 | + tags = new String[words.size()]; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + for (int i = 0; i < tokens.size(); i++) { |
| 139 | + Element currentToken = tokens.get(i); |
| 140 | + if (!currentToken.hasAttribute("pos")) { |
| 141 | + currentToken.setAttribute("pos", mapPosTag(tags[i])); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private String mapPosTag(String posTag) { |
| 147 | + Map<String, String> posMapper = this.posMapper; |
| 148 | + if (posMapper == null) { |
| 149 | + return posTag; |
| 150 | + } |
| 151 | + |
| 152 | + String mappedTag = posMapper.get(posTag); |
| 153 | + if (mappedTag == null) { |
| 154 | + LOGGER.warn("POS map file incomplete: do not know how to map '{}'", posTag); |
| 155 | + return posTag; |
| 156 | + } |
| 157 | + return mappedTag; |
| 158 | + } |
| 159 | +} |
0 commit comments