<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--    This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 3 of the License, or
        (at your option) any later version.
        
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
        
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
        MA 02110-1301, USA. -->
<!-- 20091104 latin -->

<!-- (4*) will replace 4 -->
<xsl:template name="grapheme-outside">
 <xsl:variable name="current-grapheme"><xsl:text>
  </xsl:text>
  <xsl:element name="grapheme">
   <xsl:value-of select="normalize-space(grapheme)"/>
  </xsl:element>
 </xsl:variable>
 <xsl:sequence select="$current-grapheme"/>
</xsl:template>

<!-- use (1) create-phoneme-fast # OR (2) create-phoneme-espeaktoipa OR (3) create-phoneme -->
<xsl:template name="create-phoneme-fast">
 <xsl:for-each select="phoneme"><xsl:text>
   </xsl:text><xsl:element name="phoneme">
  <xsl:variable name="sierra"><xsl:value-of select="."/></xsl:variable>
  <xsl:sequence select="$sierra"/></xsl:element>
 </xsl:for-each>
</xsl:template>

<!-- (1*) create-phoneme-variable-outside -->
<xsl:template name="create-phoneme-variable-outside">
 <xsl:variable name="sierra">
  <xsl:for-each select="phoneme"><xsl:text> <!-- distinct-values()/phoneme -->
1-outside   </xsl:text>
   <xsl:element name="phoneme">
    <xsl:value-of select="."/>
   </xsl:element>
  </xsl:for-each>
 </xsl:variable>
 <xsl:sequence select="$sierra"/>
</xsl:template>

<!-- 20091029 -->
<xsl:template name="phoneme2ipa">

    <xsl:variable name="sierra">
     <xsl:value-of select="."/> <!-- should select the current phoneme -->
    </xsl:variable>
<!-- 20091018 convert eSpeak to SAMPA -->
<xsl:variable name="sierra" select="replace($sierra, '3', 'ɐ')"/>
<xsl:variable name="sierra" select="replace($sierra, 'A', 'a')"/>
<xsl:variable name="sierra" select="replace($sierra, '_!', 'Q')"/>
<xsl:variable name="sierra" select="replace($sierra, '_\|', 'Q')"/>
<xsl:variable name="sierra" select="replace($sierra, 'pF', 'pf')"/>
<xsl:variable name="sierra" select="replace($sierra, 'W', 'œ')"/>
<xsl:variable name="sierra" select="replace($sierra, '_\^_', '')"/>
<xsl:variable name="sierra" select="replace($sierra, '@-', '')"/>
<xsl:variable name="sierra" select="replace($sierra, ',', '')"/>
<xsl:variable name="sierra" select="replace($sierra, ';', '')"/>
<xsl:variable name="sierra" select="replace($sierra, 'Y:', 'øː')"/>
<xsl:variable name="sierra" select="replace($sierra, 'E2', 'ɛ')"/>
<xsl:variable name="sierra" select="replace($sierra, '_:_:', '')"/>
<xsl:variable name="sierra" select="replace($sierra, '\*', 'ʀ')"/>
<!-- convert SAMPA to IPA -->
<xsl:variable name="sierra" select="replace($sierra, 'aU_', 'aʊ̯')"/> 
<xsl:variable name="sierra" select="replace($sierra, 'aI_', 'aɪ̯')"/>
<xsl:variable name="sierra" select="replace($sierra, 'EI_', 'ɛɪ̯')"/>
<xsl:variable name="sierra" select="replace($sierra, 'aɪ̯_', 'aɪ̯')"/>
<xsl:variable name="sierra" select="replace($sierra, 'ɛɪ_', 'ɛɪ̯')"/>
<xsl:variable name="sierra" select="replace($sierra, 'ɔɪ_', 'ɔɪ̯')"/>
<xsl:variable name="sierra" select="replace($sierra, 'aʊ̯_', 'aʊ̯')"/>
<xsl:variable name="sierra" select="replace($sierra, 'au̯', 'aʊ̯')"/> 
<xsl:variable name="sierra" select="replace($sierra, 'aU', 'aʊ̯')"/> 
<xsl:variable name="sierra" select="replace($sierra, 'aI', 'aɪ̯')"/>
<xsl:variable name="sierra" select="replace($sierra, 'ɔY', 'ɔɪ̯')"/>
<xsl:variable name="sierra" select="replace($sierra, 'ai̯', 'aɪ̯')"/>
<xsl:variable name="sierra" select="replace($sierra, 'OI_', 'ɔɪ̯')"/>

<xsl:variable name="sierra" select="replace($sierra, 'n=', 'n̩')"/>
<xsl:variable name="sierra" select="replace($sierra, 'm=', 'm̩')"/>
<xsl:variable name="sierra" select="replace($sierra, 'N=', 'ŋ̩')"/>

<xsl:variable name="sierra" select="replace($sierra, 'S', 'ʃ')"/>
<xsl:variable name="sierra" select="replace($sierra, '@', 'ə')"/>
<xsl:variable name="sierra" select="replace($sierra, 'N', 'ŋ')"/>
<xsl:variable name="sierra" select="replace($sierra, 'Q', 'ʔ')"/>
<xsl:variable name="sierra" select="replace($sierra, 'E', 'ɛ')"/>
<xsl:variable name="sierra" select="replace($sierra, 'O', 'ɔ')"/>
<xsl:variable name="sierra" select="replace($sierra, 'C', 'ç')"/>
<xsl:variable name="sierra" select="replace($sierra, 'I', 'ɪ')"/>
<xsl:variable name="sierra" select="replace($sierra, '9', 'œ')"/>
<xsl:variable name="sierra" select="replace($sierra, 'U', 'ʊ')"/>
<xsl:variable name="sierra" select="replace($sierra, '6', 'ɐ')"/>
<xsl:variable name="sierra" select="replace($sierra, 'r', 'ʀ')"/>
<xsl:variable name="sierra" select="replace($sierra, 'Z', 'ʒ')"/>
<!-- normalize space -->
<xsl:variable name="sierra" select="normalize-space($sierra)"/>
<xsl:variable name="sierra" select="replace($sierra, ' ', '')"/>
<!-- convert more SAMPA characters to IPA -->
<xsl:variable name="sierra" select="replace($sierra, 'Y','ʏ')"/> 
<xsl:variable name="sierra" select="replace($sierra, 'l̩', 'l')"/> <!-- this is a workaround to be simon compatible -->
<xsl:variable name="sierra" select="replace($sierra, 'a:', 'aː')"/>
<xsl:variable name="sierra" select="replace($sierra, 'e:', 'eː')"/>
<xsl:variable name="sierra" select="replace($sierra, 'i:', 'iː')"/>
<xsl:variable name="sierra" select="replace($sierra, 'o:', 'oː')"/>
<xsl:variable name="sierra" select="replace($sierra, 'u:', 'uː')"/>
<xsl:variable name="sierra" select="replace($sierra, 'ɛ:', 'ɛː')"/>
<xsl:variable name="sierra" select="replace($sierra, 'y:', 'yː')"/>
<xsl:variable name="sierra" select="replace($sierra, '2:', 'øː')"/>
<!-- replace several phonemes  

<xsl:variable name="sierra" select="replace($sierra, 'foːtoːxeːmɪʃ', 'foːtoːCeːmɪʃ')"/>
-->
 <xsl:sequence select="$sierra"/>

</xsl:template>

<!-- (2*) 20091023 currently in use; seems to be OK -->
<xsl:template name="espeaktoipa-outside">
 <xsl:variable name="sierra-outside">
  <xsl:for-each select="phoneme"><xsl:text>
   </xsl:text>
   
<xsl:element name="phoneme">
<xsl:call-template name="phoneme2ipa"/>
 <!--<xsl:with-param name="currentphone" select=".">
</xsl:call-template>-->
</xsl:element>

   
  </xsl:for-each>
 </xsl:variable>
 <xsl:sequence select="$sierra-outside"/>
</xsl:template>

<!-- (3)  -->
<xsl:template name="create-phoneme">

<xsl:for-each select="phoneme"><xsl:text>
   </xsl:text><phoneme>
<xsl:variable name="sierra"><xsl:value-of select="."/></xsl:variable>




<xsl:sequence select="$sierra"/></phoneme>
</xsl:for-each>
</xsl:template>

<!-- (5) create lexeme .. -->
<xsl:template name="create-lexeme"><xsl:text>
 </xsl:text>
 <xsl:element name="lexeme">
  <xsl:call-template name="test-grapheme"/><xsl:text>
 </xsl:text></xsl:element>
</xsl:template>

<!-- (6) .. with role -->
<xsl:template name="create-lexemewithrole">
 <xsl:param name="rolevalue"></xsl:param><xsl:text>
 </xsl:text>
 <xsl:element name="lexeme">
  <xsl:attribute name="role" select="$rolevalue"/>
  <xsl:call-template name="test-grapheme"/><xsl:text>
 </xsl:text>
 </xsl:element>
</xsl:template>

<!-- (7) -->
<xsl:template name="create-different-phonemes">
     <xsl:param name="iieren"></xsl:param>
     <xsl:variable name="different">
      <xsl:for-each select="phoneme">
       <xsl:value-of select="string-join((., $iieren), ' ')"/><xsl:text> </xsl:text>
      </xsl:for-each>
     </xsl:variable> 
     <xsl:variable name="different" select="tokenize(($different), ' ')"/>
     <xsl:for-each select="distinct-values($different)">
<xsl:if test="not(.='')">
<xsl:text>
   </xsl:text>
       <xsl:element name="phoneme">
        <xsl:value-of select="."/>
       </xsl:element>
</xsl:if>
     </xsl:for-each>
</xsl:template>

<!-- (8) will continue to use this version-->
<xsl:template name="test-grapheme">
 <xsl:call-template name="grapheme-outside"/>
<!-- <xsl:call-template name="espeaktoipa-outside"/>-->

  <xsl:choose>
  <xsl:when test="ends-with(grapheme, 'verkehr')"> <!-- modify phoneme if grapheme ends-with .. -->
    <xsl:for-each select="phoneme"><xsl:text>
   </xsl:text><phoneme>
    <xsl:variable name="sierra"><xsl:value-of select="."/></xsl:variable>
    <xsl:variable name="sierra" select="replace($sierra, 'vɐkeːʀ', 'fɐkeːɐ̯')"/>
    <xsl:variable name="sierra" select="replace($sierra, 'fɐkeːʀ', 'fɐkeːɐ̯')"/>
    <xsl:sequence select="$sierra"/></phoneme>
    </xsl:for-each>
  </xsl:when>


  <xsl:otherwise>
    <xsl:call-template name="espeaktoipa-outside"/> <!-- call 2 -->
  </xsl:otherwise>
  </xsl:choose>

</xsl:template>

<!-- (8*) dont use -->
<xsl:template name="test-grapheme-outside">
 <xsl:call-template name="grapheme-outside"/>
 <xsl:for-each select="phoneme">
 <xsl:call-template name="changephonemes"> <!-- call 10 -->
  <xsl:with-param name="testsierra" select="."/>
 </xsl:call-template>
 </xsl:for-each>  
</xsl:template>

<!-- (10) dont use-->
<xsl:template name="changephonemes">
 <xsl:param name="testsierra"></xsl:param>
<xsl:value-of select="grapheme"/>grapheme
<xsl:value-of select="parent::grapheme"/>parentgrapheme
   <xsl:variable name="sierra">
     <xsl:choose>
     <xsl:when test="ends-with(grapheme, 'verkehr')">
      <xsl:element name="phoneme">
       <xsl:variable name="sierra"><xsl:sequence select="$testsierra"/></xsl:variable>
       <xsl:variable name="sierra" select="replace($sierra, 'vɐkeːʀ', 'fɐkeːɐ̯')"/>
       <xsl:variable name="sierra" select="replace($sierra, 'fɐkeːʀ', 'fɐkeːɐ̯')"/>
       <xsl:sequence select="$sierra"/>
      </xsl:element>
     </xsl:when>
     <xsl:otherwise>
      <xsl:sequence select="$testsierra"/>10-otherwise
     </xsl:otherwise>
     </xsl:choose>
  </xsl:variable>
 <xsl:sequence select="$sierra"/>10-outside
</xsl:template>

<!-- (9) -->
<xsl:template match="lexicon"><xsl:text>
</xsl:text><!-- 20091024 -->
<xsl:variable name="phoneme-wortende"/><xsl:comment> preparing   Ralf's Latin dictionary - German pronunciation (version 0.1.1; November 4, 2009)
         About 1.7 million words, and of course errors. 
         You can import this dictionary into 'simon'.</xsl:comment><xsl:text>
</xsl:text>
<xsl:comment>    This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 3 of the License, or
        (at your option) any later version.
        
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
        
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
        MA 02110-1301, USA. </xsl:comment><xsl:text>
</xsl:text>
<lexicon version="1.0" alphabet="ipa" xml:lang="la">

<xsl:for-each select="lexeme">
<!-- <xsl:sort select="grapheme" order="ascending"/> -->


<xsl:variable name="rolecontent" select="@role"></xsl:variable> 
  <xsl:choose>
  <xsl:when test="compare($rolecontent, '')">
   <xsl:call-template name="create-lexemewithrole"><xsl:with-param name="rolevalue" select="$rolecontent"/>
   </xsl:call-template>
  </xsl:when>

  <xsl:otherwise>
    <xsl:choose>
    <xsl:when test="starts-with(grapheme, 'aufzu')"> <!--auf...enden-->
     <xsl:choose>
     <xsl:when test="ends-with(grapheme, 'nehmenden')">
      <xsl:call-template name="create-lexemewithrole"><xsl:with-param name="rolevalue">Adjektiv</xsl:with-param>
      </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
      <xsl:call-template name="create-lexeme"/>
     </xsl:otherwise>
     </xsl:choose>
    </xsl:when>

    <xsl:when test="ends-with(grapheme, 'fähig')">
      <xsl:call-template name="create-lexemewithrole"><xsl:with-param name="rolevalue">Adjektiv</xsl:with-param>
      </xsl:call-template>
    </xsl:when>

    <xsl:otherwise>
     <xsl:call-template name="create-lexeme"/>
    </xsl:otherwise>
    </xsl:choose>

  </xsl:otherwise>
  </xsl:choose>

</xsl:for-each><xsl:text>
</xsl:text>
</lexicon>
</xsl:template>

</xsl:stylesheet>

