|
| 1 | +/** |
| 2 | + * Copyright 2007 DFKI GmbH. |
| 3 | + * All Rights Reserved. Use is subject to license terms. |
| 4 | + * |
| 5 | + * This file is part of MARY TTS. |
| 6 | + * |
| 7 | + * MARY TTS is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as published by |
| 9 | + * the Free Software Foundation, version 3 of the License. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + */ |
| 20 | +package marytts.modules; |
| 21 | + |
| 22 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 23 | + |
| 24 | +import java.io.BufferedReader; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.InputStreamReader; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.StringTokenizer; |
| 32 | + |
| 33 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 34 | +import org.eclipse.jdt.annotation.Nullable; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
| 37 | +import org.w3c.dom.Document; |
| 38 | +import org.w3c.dom.Element; |
| 39 | +import org.w3c.dom.traversal.NodeIterator; |
| 40 | +import org.w3c.dom.traversal.TreeWalker; |
| 41 | + |
| 42 | +import marytts.datatypes.MaryData; |
| 43 | +import marytts.datatypes.MaryDataType; |
| 44 | +import marytts.server.MaryProperties; |
| 45 | +import marytts.util.MaryUtils; |
| 46 | +import marytts.util.dom.MaryDomUtils; |
| 47 | +import opennlp.tools.postag.POSModel; |
| 48 | +import opennlp.tools.postag.POSTagFormat; |
| 49 | +import opennlp.tools.postag.POSTaggerME; |
| 50 | + |
| 51 | +/** |
| 52 | + * OpenNLP POS tagger compatible with the model and tag set expected by MaryTTS 5.2.1. |
| 53 | + * |
| 54 | + * <p> |
| 55 | + * MaryTTS 5.2.1 uses OpenNLP's removed {@code tag(List)} API and the legacy one-argument constructor. This replacement |
| 56 | + * uses the current array API and explicitly retains Penn Treebank output, on which MaryTTS's English prosody rules |
| 57 | + * depend. |
| 58 | + * |
| 59 | + * @author Marc Schröder - Initial contribution |
| 60 | + * @author Leo Siepel - Refactored for use with openHAB's MaryTTS runtime |
| 61 | + */ |
| 62 | +@NonNullByDefault |
| 63 | +public class OpenNLPPosTagger extends InternalModule { |
| 64 | + |
| 65 | + private static final Logger LOGGER = LoggerFactory.getLogger(OpenNLPPosTagger.class); |
| 66 | + |
| 67 | + private final String propertyPrefix; |
| 68 | + |
| 69 | + private @Nullable POSTaggerME tagger; |
| 70 | + private @Nullable Map<String, String> posMapper; |
| 71 | + |
| 72 | + public OpenNLPPosTagger(String locale, String propertyPrefix) throws Exception { |
| 73 | + super("OpenNLPPosTagger", MaryDataType.WORDS, MaryDataType.PARTSOFSPEECH, MaryUtils.string2locale(locale)); |
| 74 | + this.propertyPrefix = propertyPrefix.endsWith(".") ? propertyPrefix : propertyPrefix + "."; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void startup() throws Exception { |
| 79 | + super.startup(); |
| 80 | + |
| 81 | + try (InputStream modelStream = MaryProperties.needStream(propertyPrefix + "model")) { |
| 82 | + tagger = new POSTaggerME(new POSModel(modelStream), POSTagFormat.PENN); |
| 83 | + } |
| 84 | + |
| 85 | + InputStream posMapStream = MaryProperties.getStream(propertyPrefix + "posMap"); |
| 86 | + if (posMapStream != null) { |
| 87 | + posMapper = readPosMapper(posMapStream); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private Map<String, String> readPosMapper(InputStream posMapStream) throws Exception { |
| 92 | + Map<String, String> mapper = new HashMap<>(); |
| 93 | + try (posMapStream; BufferedReader reader = new BufferedReader(new InputStreamReader(posMapStream, UTF_8))) { |
| 94 | + String line; |
| 95 | + while ((line = reader.readLine()) != null) { |
| 96 | + if (!line.startsWith("#") && !line.isBlank()) { |
| 97 | + StringTokenizer tokenizer = new StringTokenizer(line); |
| 98 | + if (tokenizer.countTokens() >= 2) { |
| 99 | + mapper.put(tokenizer.nextToken(), tokenizer.nextToken()); |
| 100 | + } else { |
| 101 | + LOGGER.warn("Invalid POS map line (expected 2 tokens): '{}'", line); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return mapper; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public MaryData process(@NonNullByDefault({}) MaryData input) throws Exception { |
| 111 | + Document document = input.getDocument(); |
| 112 | + NodeIterator sentenceIterator = MaryDomUtils.createNodeIterator(document, document, "s"); |
| 113 | + |
| 114 | + Element sentence; |
| 115 | + while ((sentence = (Element) sentenceIterator.nextNode()) != null) { |
| 116 | + tagSentence(sentence); |
| 117 | + } |
| 118 | + |
| 119 | + MaryData output = new MaryData(getOutputType(), input.getLocale()); |
| 120 | + output.setDocument(document); |
| 121 | + return output; |
| 122 | + } |
| 123 | + |
| 124 | + private void tagSentence(Element sentence) { |
| 125 | + TreeWalker tokenWalker = MaryDomUtils.createTreeWalker(sentence, "t"); |
| 126 | + List<Element> tokens = new ArrayList<>(); |
| 127 | + List<String> words = new ArrayList<>(); |
| 128 | + |
| 129 | + Element token; |
| 130 | + while ((token = (Element) tokenWalker.nextNode()) != null) { |
| 131 | + tokens.add(token); |
| 132 | + words.add(MaryDomUtils.tokenText(token)); |
| 133 | + } |
| 134 | + |
| 135 | + // OpenNLP cannot determine useful context from a one-token sentence. |
| 136 | + if (words.size() == 1) { |
| 137 | + words.add("."); |
| 138 | + } |
| 139 | + |
| 140 | + String[] tags; |
| 141 | + synchronized (this) { |
| 142 | + POSTaggerME tagger = this.tagger; |
| 143 | + if (tagger != null) { |
| 144 | + tags = tagger.tag(words.toArray(String[]::new)); |
| 145 | + } else { |
| 146 | + tags = new String[words.size()]; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + for (int i = 0; i < tokens.size(); i++) { |
| 151 | + Element currentToken = tokens.get(i); |
| 152 | + if (!currentToken.hasAttribute("pos")) { |
| 153 | + currentToken.setAttribute("pos", mapPosTag(tags[i])); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private String mapPosTag(String posTag) { |
| 159 | + Map<String, String> posMapper = this.posMapper; |
| 160 | + if (posMapper == null) { |
| 161 | + return posTag; |
| 162 | + } |
| 163 | + |
| 164 | + String mappedTag = posMapper.get(posTag); |
| 165 | + if (mappedTag == null) { |
| 166 | + LOGGER.warn("POS map file incomplete: do not know how to map '{}'", posTag); |
| 167 | + return posTag; |
| 168 | + } |
| 169 | + return mappedTag; |
| 170 | + } |
| 171 | +} |
0 commit comments