Loading ...
Sorry, an error occurred while loading the content.
 

regex to test if a string is in a list of items

Expand Messages
  • jahirju30
    I was wondering what might be the best way to test if a string exactly matches any of the elements of is list of strings in Praat script. Following is a quick
    Message 1 of 3 , Nov 11, 2017

      I was wondering what might be the best way to test if a string exactly matches any of the elements of is list of strings in Praat script. Following is a quick if-else loop I am trying to achieve; I want to test if a phone, say "P", belongs to a list of phones or not and then pass a result based on the matching. 


      phone$ = "P"

      if index_regex("(TT|TTS|DD|DDS)($)", phone$ )

      result$ = "dental"

      elsif index_regex("(P|PS|B|BS|M)($)", phone$ )

      result$ = "bilabial"


      Any help appreciated. 


      Thank you. 

      -Jahurul

    • rolando_ma
      Dear Jahurul: You could use a WordList object in order to do that. This object has the command Has word which tells you if a string match any item in string
      Message 2 of 3 , Nov 11, 2017
        Dear Jahurul:

        You could use a WordList object in order to do that.  This object has the command Has word which tells you if a string match any item in string list.

        To create it from a Strings object, follow these next steps:

        # First, select a Strings object, then:
        Genericize
        Sort
        wordList = To WordList

        # Make queries
        exist = Has word: "a"

        # Evaluate your results
        if exist
          # do something
        endif

        For more information, visit the WordList section of the Praat manual in [1]

        [1] http://www.fon.hum.uva.nl/praat/manual/WordList.html

        Best,
        - Rolando
      • Paul Boersma
        ... if you need exact matching, you may not need regular expressions. You only need a separator. If your phoneme strings do not contain spaces, you can use the
        Message 3 of 3 , Nov 12, 2017
          On 11 Nov 2017, at 18:36, jahirju30@... [praat-users] <praat-users-noreply@...> wrote:

          I was wondering what might be the best way to test if a string exactly matches any of the elements of is list of strings in Praat script. Following is a quick if-else loop I am trying to achieve; I want to test if a phone, say "P", belongs to a list of phones or not and then pass a result based on the matching. 



          if you need exact matching, you may not need regular expressions. You only need a separator. If your phoneme strings do not contain spaces, you can use the space as a separator:

          phoneBetweenSeparators$ = " " + phone$ + " "
          if index (" TT TTS DD DDS ", phoneBetweenSeparators$)
          result$ = "dental"
          elsif index (" P PS B BS M ", phoneBetweenSeparators$)
          result$ = "bilabial"

          and so on. You do have to make sure that the string in which you search starts and ends with a separator, as shown here.
          _____

          Paul Boersma
          Professor of Phonetic Sciences
          University of Amsterdam
          Spuistraat 134, room 632
          1012VB Amsterdam, The Netherlands
          http://www.fon.hum.uva.nl/paul/

        Your message has been successfully submitted and will be delivered to recipients shortly.