📄 featureprocessors.java
字号:
/** * Portions Copyright 2001 Sun Microsystems, Inc. * Portions Copyright 1999-2001 Language Technologies Institute, * Carnegie Mellon University. * All Rights Reserved. Use is subject to license terms. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. */package com.sun.speech.freetts.en.us;import com.sun.speech.freetts.en.us.USEnglish;import com.sun.speech.freetts.util.Utilities;import com.sun.speech.freetts.FeatureProcessor;import com.sun.speech.freetts.Item;import com.sun.speech.freetts.Relation;import com.sun.speech.freetts.PartOfSpeech;import com.sun.speech.freetts.PathExtractor;import com.sun.speech.freetts.PathExtractorImpl;import com.sun.speech.freetts.ProcessException;import com.sun.speech.freetts.Voice;import java.io.PrintWriter;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.regex.Pattern;import java.util.Set;/** * Provides the set of feature processors that are used by this * language as part of the CART processing. */public class FeatureProcessors { private final static PathExtractor FIRST_SYLLABLE_PATH = new PathExtractorImpl( "R:SylStructure.parent.R:Phrase.parent.daughter.R:SylStructure.daughter", false); private final static PathExtractor LAST_SYLLABLE_PATH = new PathExtractorImpl( "R:SylStructure.parent.R:Phrase.parent.daughtern.R:SylStructure.daughter", false); private final static PathExtractor LAST_LAST_SYLLABLE_PATH = new PathExtractorImpl( "R:SylStructure.parent.R:Phrase.parent.daughtern.R:SylStructure.daughtern", false); private final static PathExtractor SUB_PHRASE_PATH = new PathExtractorImpl("R:SylStructure.parent.R:Phrase.parent.p", false); private final static Pattern DOUBLE_PATTERN = Pattern.compile(USEnglish.RX_DOUBLE); private final static Pattern DIGITS_PATTERN = Pattern.compile(USEnglish.RX_DIGITS); private static Set months; private static Set days; // the set of month names static { months = new HashSet(); months.add("jan"); months.add("january"); months.add("feb"); months.add("february"); months.add("mar"); months.add("march"); months.add("apr"); months.add("april"); months.add("may"); months.add("jun"); months.add("june"); months.add("jul"); months.add("july"); months.add("aug"); months.add("august"); months.add("sep"); months.add("september"); months.add("oct"); months.add("october"); months.add("nov"); months.add("november"); months.add("dec"); months.add("december"); } // the set of week neames static { days = new HashSet(); days.add("sun"); days.add("sunday"); days.add("mon"); days.add("monday"); days.add("tue"); days.add("tuesday"); days.add("wed"); days.add("wednesday"); days.add("thu"); days.add("thursday"); days.add("fri"); days.add("friday"); days.add("sat"); days.add("saturday"); } // no instances private FeatureProcessors() {} /** * Returns a guess of the part-of-speech. * * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class Gpos implements FeatureProcessor { PartOfSpeech pos; /** * Creates a GPOS with the given part-of-speech table * * @param pos part of speech determiner */ public Gpos(PartOfSpeech pos) { this.pos = pos; } /** * Performs some processing on the given item. * * @param item the item to process * * @return a guess at the part-of-speech for the item * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { return pos.getPartOfSpeech(item.toString()); } } /** * Returns as an Integer the number of syllables in the given * word. This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class WordNumSyls implements FeatureProcessor { /** * Performs some processing on the given item. * * @param item the item to process * * @return the number of syllables in the given word * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { int count = 0; Item daughter = item.getItemAs( Relation.SYLLABLE_STRUCTURE).getDaughter(); while (daughter != null) { count++; daughter = daughter.getNext(); } return Integer.toString(rail(count)); } } /** * Counts the number of accented syllables since the last major break. * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class AccentedSylIn implements FeatureProcessor { /** * Performs some processing on the given item. * * @param item the item to process * * @return the number of accented syllables since the last * major break * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { int count = 0; Item ss = item.getItemAs(Relation.SYLLABLE); Item firstSyllable = FIRST_SYLLABLE_PATH.findItem(item); for (Item p = ss; p != null; p = p.getPrevious() ) { if (isAccented(p)) { count++; } if (p.equalsShared(firstSyllable)) { break; } } return Integer.toString(rail(count)); } } /** * Counts the number of stressed syllables since the last major break. * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class StressedSylIn implements FeatureProcessor { /** * Performs some processing on the given item. * * @param item the item to process * * @return the number of stresses syllables since the last * major break * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { int count = 0; Item ss = item.getItemAs(Relation.SYLLABLE); Item firstSyllable = FIRST_SYLLABLE_PATH.findItem(item); // this should include the first syllable, but // flite 1.1 and festival don't. for (Item p = ss.getPrevious(); p != null && !p.equalsShared(firstSyllable); p = p.getPrevious() ) { if ("1".equals(p.getFeatures().getString("stress"))) { count++; } } return Integer.toString(rail(count)); } } /** * Counts the number of stressed syllables until the next major break. * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class StressedSylOut implements FeatureProcessor { /** * Performs some processing on the given item. * * @param item the item to process * * @return the number of stressed syllables until the next major break * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { int count = 0; Item ss = item.getItemAs(Relation.SYLLABLE); Item lastSyllable = LAST_SYLLABLE_PATH.findItem(item); for (Item p = ss.getNext(); p != null; p = p.getNext() ) { if ("1".equals(p.getFeatures().getString("stress"))) { count++; } if (p.equalsShared(lastSyllable)) { break; } } return Integer.toString(rail(count)); } } /** * Returns the length of the string. (generally this is a digit * string) * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class NumDigits implements FeatureProcessor { /** * Performs some processing on the given item. * * @param item the item to process * * @return the length of the string * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { String name = item.getFeatures().getString("name"); return Integer.toString(rail(name.length())); } } /** * Returns true ("1") if the given item is a number between 0 and * 32 exclusive, otherwise, returns "0". * string) * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class MonthRange implements FeatureProcessor { /** * Performs some processing on the given item. * * @param item the item to process * * @return returns "1" if the given item is a number between 0 * and 32 (exclusive) otherwise returns "0" * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { int v = Integer.parseInt(item.getFeatures().getString("name")); if ((v > 0) && (v < 32)) { return "1"; } else { return "0"; } } } /** * Attempts to guess the part of speech. * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class TokenPosGuess implements FeatureProcessor { /** * Performs some processing on the given item. * * @param item the item to process * * @return a guess at the part of speech * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { String name = item.getFeatures().getString("name"); String dc = name.toLowerCase(); if (DIGITS_PATTERN.matcher(dc).matches()) { return "numeric"; } else if (DOUBLE_PATTERN.matcher(dc).matches()) { return "number"; } else if (months.contains(dc)) { return "month"; } else if (days.contains(dc)) { return "day"; } else if (dc.equals("a")) { return "a"; } else if (dc.equals("flight")) { return "flight"; } else if (dc.equals("to")) { return "to"; } else { return "_other_"; } } } /** * Checks to see if the given syllable is accented. * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class Accented implements FeatureProcessor { /** * Performs some processing on the given item. * @param item the item to process * * @return "1" if the syllable is accented; otherwise "0" * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { if (isAccented(item)) { return "1"; } else { return "0"; } } } /** * Find the last accented syllable * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class LastAccent implements FeatureProcessor { /** * Performs some processing on the given item. * * @param item the item to process * * @return the count of the last accented syllable * * @throws ProcessException if an exception occurred during the * processing */ public String process(Item item) throws ProcessException { int count = 0; for (Item p = item.getItemAs(Relation.SYLLABLE); p != null; p = p.getPrevious(), count++) { if (isAccented(p)) { break; } } return Integer.toString(rail(count)); } } /** * Finds the position of the phoneme in the syllable * This is a feature processor. A feature processor takes an item, * performs some sort of processing on the item and returns an object. */ public static class PosInSyl implements FeatureProcessor { /** * Performs some processing on the given item. * * @param item the item to process
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -