Exemplo n.º 1
0
 public void setDefaultVoice(Voice voice) {
   if (voice == null) {
     return;
   }
   // check that voice locale fits before accepting the voice:
   Locale voiceLocale = null;
   voiceLocale = voice.getLocale();
   Locale docLocale = getLocale();
   if (docLocale == null
       && getType().isXMLType()
       && getDocument() != null
       && getDocument().getDocumentElement().hasAttribute("xml:lang")) {
     docLocale =
         MaryUtils.string2locale(getDocument().getDocumentElement().getAttribute("xml:lang"));
   }
   if (docLocale != null
       && voiceLocale != null
       && !(MaryUtils.subsumes(docLocale, voiceLocale)
           || MaryUtils.subsumes(voiceLocale, docLocale))) {
     logger.warn(
         "Voice `"
             + voice.getName()
             + "' does not match document locale `"
             + docLocale
             + "' -- ignoring!");
   }
   this.defaultVoice = voice;
 }
Exemplo n.º 2
0
 /**
  * For an element in a MaryXML document, do what you can to determine the appropriate
  * AllophoneSet. First search for the suitable voice, then if that fails, go by locale.
  *
  * @param e
  * @return an allophone set if there is any way of determining it, or null.
  * @throws MaryConfigurationException if a suitable allophone set exists in principle, but there
  *     were problems loading it.
  */
 public static AllophoneSet determineAllophoneSet(Element e) throws MaryConfigurationException {
   AllophoneSet allophoneSet = null;
   Element voice = (Element) MaryDomUtils.getAncestor(e, MaryXML.VOICE);
   Voice maryVoice = Voice.getVoice(voice);
   if (maryVoice == null) {
     // Determine Locale in order to use default voice
     Locale locale =
         MaryUtils.string2locale(
             e.getOwnerDocument().getDocumentElement().getAttribute("xml:lang"));
     maryVoice = Voice.getDefaultVoice(locale);
   }
   if (maryVoice != null) {
     allophoneSet = maryVoice.getAllophoneSet();
   } else {
     Locale locale =
         MaryUtils.string2locale(
             e.getOwnerDocument().getDocumentElement().getAttribute("xml:lang"));
     allophoneSet = determineAllophoneSet(locale);
   }
   return allophoneSet;
 }
  public void computeFeaturesFor(String basename) throws IOException, Exception {
    String text;
    Locale localVoice;
    localVoice = MaryUtils.string2locale(locale);

    // First, test if there is a corresponding .rawmaryxml file in textdir:
    File rawmaryxmlFile =
        new File(db.getProp(db.MARYXMLDIR) + basename + db.getProp(db.MARYXMLEXT));
    if (rawmaryxmlFile.exists()) {
      text = FileUtils.getFileAsString(rawmaryxmlFile, "UTF-8");
    } else {
      text =
          getMaryXMLHeaderWithInitialBoundary(locale)
              + FileUtils.getFileAsString(
                  new File(db.getProp(db.TEXTDIR) + basename + db.getProp(db.TEXTEXT)), "UTF-8")
              + "</maryxml>";
    }
    File pfeatFile = new File(unitfeatureDir, basename + featsExt);
    OutputStream os = new BufferedOutputStream(new FileOutputStream(pfeatFile));
    MaryClient maryClient = getMaryClient();
    /*Vector voices = maryClient.getVoices(localVoice);
    MaryClient.Voice defaultVoice = (MaryClient.Voice) voices.firstElement();
    String voiceName = defaultVoice.name();*/
    // maryClient.process(text, maryInputType, maryOutputType, null, null, os);

    maryClient.process(text, maryInputType, maryOutputType, locale, null, "slt-arctic", os);
    // maryClient.process(text, maryInputType, maryOutputType, null, "slt-arctic", os, timeout);
    // maryClient.getOutputDataTypes().size()
    // MaryData result = new MaryData(os);

    os.flush();
    os.close();

    // System.out.println(" TO STRING: "+new FileReader(pfeatFile).toString());
    // BufferedReader bfr = new BufferedReader(new FileReader(pfeatFile));
    String line;
    MaryData d = new MaryData(MaryDataType.get("PHONEMISED_EN"), Locale.US);
    // d.readFrom(new ByteArrayInputStream(os.toByteArray()));
    d.readFrom(new FileReader(pfeatFile));

    // MaryData d = new MaryData(pfeatFile);
    Document doc = d.getDocument();
    // Document acoustparams = d.getDocument();

    // NodeIterator it = ((DocumentTraversal)acoustparams).createNodeIterator(acoustparams,
    // NodeFilter.SHOW_ELEMENT,new NameNodeFilter(new String[]{MaryXML.TOKEN,
    // MaryXML.BOUNDARY}),false);
    NodeIterator it =
        ((DocumentTraversal) doc)
            .createNodeIterator(
                doc, NodeFilter.SHOW_ELEMENT, new NameNodeFilter(MaryXML.TOKEN), false);

    Element t = null;
    while ((t = (Element) it.nextNode()) != null) {
      if (t.hasAttribute("g2p_method")) {
        String g2p = t.getAttribute("g2p_method");
        String nodeText = t.getTextContent().trim();
        if (g2p.equals("rules")) { // && nodeText.equals("!")){
          System.out.print(basename + " ----> " + nodeText);
          if (bnl.contains(basename)) bnl.remove(basename);
          System.out.println(" SO removing basename: " + basename);
        }

        // System.out.println("G2P:"+t.getAttribute("g2p_method"));
        // System.out.println("Text:"+t.getTextContent());
      }
    }

    /*while((line =bfr.readLine()) != null){
        //boolean b = m.matches();
        if(Pattern.matches("rules", line))
                System.out.println(basename + " LINE ---> " + line);

    }*/
    // System.out.println(" TO STRING: "+line);

  }