PHP script to determine if letter Y is a consonant or vowel

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi All

If a name or word contains the letter y, I need to determine if it is a vowel or a consonant.

Here is what I grammatically know, assuming I am correct.
It may sound a bit winded so sorry about that.

The letter y is a consonant when it is the first letter of a syllable that has more than one letter. If y is anywhere else in the syllable, it is a vowel.

Y as a consonant:
yes
yam
yell
yellow
yogurt
yale

Y as a vowel
gym
my
cycle
baby
hairy
sky

The important thing in linking is the sound, not the letter. Often the letter and the sound are the same, but not always.
For example, the word "pay" ends with:
the consonant letter "y"
the vowel sound "a"

So I guess we can summarize then as follows:

- When the letter Y serves as a vowel, and in fact sounds like one, it is a vowel. The same is true when the Y serves as the only vowel in the syllable.
Examples of both of these cases are such names as Lynn, Yvonne, Mary, Betty, Elly, and Bryan.

- However, if the Y does not provide a separate vowel sound, as when it is coupled with another vowel, it is considered a consonant.
In names such as Maloney or Murray, the Y is a consonant, because the vowel sound depends upon the long E in Maloney and the long A in Murray.

-In general, the Y is a consonant when the syllable already has a vowel. Also, the Y is considered a consonant when it is used in place of the soft J sound, such as in the name Yolanda or Yoda.

- In the names Bryan and Wyatt, the Y is a vowel, because it provides the only vowel sound for the first syllable of both names. For both of these names, the letter A is part of the second syllable, and therefore does not influence the nature of the Y.

Other examples:
-In Sydney, the first Y is a vowel, the second Y is a consonant.
-In Billy, Sylvia, Missy, Kyle, Blythe, Sylvester, and Katy, the Y is a vowel
-In Kay, Yeltsin, May, and Kuykendahl, the Y is a consonant.

I don't know if this can even be done, but I would like to create a PHP script to determine if the Letter Y is a vowel or a consonant.

Any one have any suggestions.

Thanks
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Here is what I have been able to do so far:

Code: [ Select ]
<?php

echo "<br />";

$firstname = "BARBARA";
$middlename = "ANN";
$lastname = "KELLY";

echo "Name : ".$firstname." ".$middlename." ".$lastname;
echo "<br />";
$lengthfirstname = strlen($firstname);
$lengthmiddlename = strlen($middlename);
$lengthlastname = strlen($lastname);

echo "Letters in First Name : ".$lengthfirstname."<br />";
echo "Letters in Middle Name : ".$lengthmiddlename."<br />";
echo "Letters in Last Name : ".$lengthlastname."<br />";
echo "<br />";

$vowels = array('A', 'E', 'I', 'O', 'U');

$firstnamevoweltotal=0;
$firstnameconsonanttotal = 0;
echo "For first name $firstname we have :"."<br />";
for ($i = 0; $i < $lengthfirstname; ++$i)
{
  if (in_array($firstname[$i], $vowels))
  {
   echo "vowel ".$firstname[$i]." at position ".($i+1)."<br />";
   $firstnamevoweltotal = $firstnamevoweltotal + 1;
  }
  else
  {
   echo "consonant ".$firstname[$i]." at position ".($i+1)."<br />";
   $firstnameconsonanttotal = $firstnameconsonanttotal +1;
  }
}

echo "<br />";
echo "Total Consonants in First Name : ".$firstnameconsonanttotal."<br />";
echo "Total vowels in First Name : ".$firstnamevoweltotal;

echo "<br /><br />";

$middlenamevoweltotal=0;
$middlenameconsonanttotal = 0;
echo "For middle name $middlename we have"."<br />";
for ($i = 0; $i < $lengthmiddlename; ++$i)
{
  if (in_array($middlename[$i], $vowels))
  {
   echo "vowel ".$middlename[$i]." at position ".($i+1)."<br />";
   $middlenamevoweltotal = $middlenamevoweltotal + 1;
  }
  else
  {
   echo "consonant ".$middlename[$i]." at position ".($i+1)."<br />";
   $middlenameconsonanttotal = $middlenameconsonanttotal +1;
  }
}

echo "<br />";

echo "Total Consonants in Middle Name : ".$middlenameconsonanttotal."<br />";
echo "Total vowels in Middle Name : ".$middlenamevoweltotal;

echo "<br /><br />";

$lastnamevoweltotal=0;
$lastnameconsonanttotal = 0;
echo "For last name $lastname we have"."<br />";
for ($i = 0; $i < $lengthlastname; ++$i)
{
  if (in_array($lastname[$i], $vowels))
  {
   echo "vowel ".$lastname[$i]." at position ".($i+1)."<br />";
   $lastnamevoweltotal = $lastnamevoweltotal + 1;
  }
  else
  {
   echo "consonant ".$lastname[$i]." at position ".($i+1)."<br />";
   $lastnameconsonanttotal = $lastnameconsonanttotal +1;
  }
}

echo "<br />";

echo "Total Consonants in Last Name : ".$lastnameconsonanttotal."<br />";
echo "Total vowels in Middle Name : ".$lastnamevoweltotal;

?>
  1. <?php
  2. echo "<br />";
  3. $firstname = "BARBARA";
  4. $middlename = "ANN";
  5. $lastname = "KELLY";
  6. echo "Name : ".$firstname." ".$middlename." ".$lastname;
  7. echo "<br />";
  8. $lengthfirstname = strlen($firstname);
  9. $lengthmiddlename = strlen($middlename);
  10. $lengthlastname = strlen($lastname);
  11. echo "Letters in First Name : ".$lengthfirstname."<br />";
  12. echo "Letters in Middle Name : ".$lengthmiddlename."<br />";
  13. echo "Letters in Last Name : ".$lengthlastname."<br />";
  14. echo "<br />";
  15. $vowels = array('A', 'E', 'I', 'O', 'U');
  16. $firstnamevoweltotal=0;
  17. $firstnameconsonanttotal = 0;
  18. echo "For first name $firstname we have :"."<br />";
  19. for ($i = 0; $i < $lengthfirstname; ++$i)
  20. {
  21.   if (in_array($firstname[$i], $vowels))
  22.   {
  23.    echo "vowel ".$firstname[$i]." at position ".($i+1)."<br />";
  24.    $firstnamevoweltotal = $firstnamevoweltotal + 1;
  25.   }
  26.   else
  27.   {
  28.    echo "consonant ".$firstname[$i]." at position ".($i+1)."<br />";
  29.    $firstnameconsonanttotal = $firstnameconsonanttotal +1;
  30.   }
  31. }
  32. echo "<br />";
  33. echo "Total Consonants in First Name : ".$firstnameconsonanttotal."<br />";
  34. echo "Total vowels in First Name : ".$firstnamevoweltotal;
  35. echo "<br /><br />";
  36. $middlenamevoweltotal=0;
  37. $middlenameconsonanttotal = 0;
  38. echo "For middle name $middlename we have"."<br />";
  39. for ($i = 0; $i < $lengthmiddlename; ++$i)
  40. {
  41.   if (in_array($middlename[$i], $vowels))
  42.   {
  43.    echo "vowel ".$middlename[$i]." at position ".($i+1)."<br />";
  44.    $middlenamevoweltotal = $middlenamevoweltotal + 1;
  45.   }
  46.   else
  47.   {
  48.    echo "consonant ".$middlename[$i]." at position ".($i+1)."<br />";
  49.    $middlenameconsonanttotal = $middlenameconsonanttotal +1;
  50.   }
  51. }
  52. echo "<br />";
  53. echo "Total Consonants in Middle Name : ".$middlenameconsonanttotal."<br />";
  54. echo "Total vowels in Middle Name : ".$middlenamevoweltotal;
  55. echo "<br /><br />";
  56. $lastnamevoweltotal=0;
  57. $lastnameconsonanttotal = 0;
  58. echo "For last name $lastname we have"."<br />";
  59. for ($i = 0; $i < $lengthlastname; ++$i)
  60. {
  61.   if (in_array($lastname[$i], $vowels))
  62.   {
  63.    echo "vowel ".$lastname[$i]." at position ".($i+1)."<br />";
  64.    $lastnamevoweltotal = $lastnamevoweltotal + 1;
  65.   }
  66.   else
  67.   {
  68.    echo "consonant ".$lastname[$i]." at position ".($i+1)."<br />";
  69.    $lastnameconsonanttotal = $lastnameconsonanttotal +1;
  70.   }
  71. }
  72. echo "<br />";
  73. echo "Total Consonants in Last Name : ".$lastnameconsonanttotal."<br />";
  74. echo "Total vowels in Middle Name : ".$lastnamevoweltotal;
  75. ?>


The script gives the following output:

Name : BARBARA ANN KELLY
Letters in First Name : 7
Letters in Middle Name : 3
Letters in Last Name : 5

For first name BARBARA we have :
consonant B at position 1
vowel A at position 2
consonant R at position 3
consonant B at position 4
vowel A at position 5
consonant R at position 6
vowel A at position 7

Total Consonants in First Name : 4
Total vowels in First Name : 3

For middle name ANN we have
vowel A at position 1
consonant N at position 2
consonant N at position 3

Total Consonants in Middle Name : 2
Total vowels in Middle Name : 1

For last name KELLY we have
consonant K at position 1
vowel E at position 2
consonant L at position 3
consonant L at position 4
consonant Y at position 5

Total Consonants in Last Name : 4
Total vowels in Middle Name : 1

If we look at the Y in the last name, it should really be a vowel, not a consonant based on the grammatical rules from before.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Im on a phone so looking up manual pages is tough right now.. Does php have soundex functions? I remember mySQL had a sort of "soundz like" function, it seems liks php did too. Im wondering if doing a comparison between the word and vowels could work in some way.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Try experimenting with metaphone. If Y is a constant, it should show up in the resulting metaphone string.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

..... In reading the rules for the Y it seems like you would need to split each word into syllables first before analyzing to check to see if the y is a vowel or a consonant. Luckily Frank Liang's algorithm can help get the syllables and this class can help even more https://github.com/vanderlee/phpSyllable. using this I am currently working on a possible solution
  • alemao_brazil
  • Newbie
  • Newbie
  • alemao_brazil
  • Posts: 8

Post 3+ Months Ago

PHP Code: [ Select ]
      <?php
             # Credits: Sergio Abreu
            # Matches y followed by vowel in any place...
 
             if( preg_match( "/y[aeiou]/i", $word)){
                 /* consonant code */
             } elseif( preg_match( "/[b-z]y/i", $word) && ! preg_match( "/[aeiou]y/i", $word)){
                 /* vowel code */
             }
       
   # To restrict y being the first letter, use ^ :
 
             if( preg_match( "/^y[aeiou]/i", $word)){
                 /* consonant code */
             } elseif( preg_match( "/[b-z]y/i", $word) && ! preg_match( "/[aeiou]y/i", $word)){
                 /* vowel code */
             }
 
       ?>
 
  1.       <?php
  2.              # Credits: Sergio Abreu
  3.             # Matches y followed by vowel in any place...
  4.  
  5.              if( preg_match( "/y[aeiou]/i", $word)){
  6.                  /* consonant code */
  7.              } elseif( preg_match( "/[b-z]y/i", $word) && ! preg_match( "/[aeiou]y/i", $word)){
  8.                  /* vowel code */
  9.              }
  10.        
  11.    # To restrict y being the first letter, use ^ :
  12.  
  13.              if( preg_match( "/^y[aeiou]/i", $word)){
  14.                  /* consonant code */
  15.              } elseif( preg_match( "/[b-z]y/i", $word) && ! preg_match( "/[aeiou]y/i", $word)){
  16.                  /* vowel code */
  17.              }
  18.  
  19.        ?>
  20.  
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

OK Well I for the most part have gotten the Vowel detector script working ... Ish there is a bit of strangeness going on with words like Yvonne

Download the the zip to test for yourself .... I can't think any more tonight so I'll look at it again tomorrow
  • alemao_brazil
  • Newbie
  • Newbie
  • alemao_brazil
  • Posts: 8

Post 3+ Months Ago

PHP Code: [ Select ]
<?php
      #If y is followed by consonant...
 
        if( preg_match("/^y[b-z]/i", $word) && preg_match("/^y[^aeiou]/i", $word)){
         /*Code for words like Yvone */
}
?>
 
  1. <?php
  2.       #If y is followed by consonant...
  3.  
  4.         if( preg_match("/^y[b-z]/i", $word) && preg_match("/^y[^aeiou]/i", $word)){
  5.          /*Code for words like Yvone */
  6. }
  7. ?>
  8.  
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

ScottG

Downloaded test.zip but get error when try to open
Error: central directory not found
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

That's strange ... Try this
Attachments:
test.zip

(214.53 KiB) Downloaded 203 times

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

If that doesn't download the class from https://github.com/vanderlee/phpSyllable

Here is my project file
PHP Code: [ Select ]
start
<?php
 
// Include the class
require_once(dirname(__FILE__) . '/Syllable.php');
 
function vowel_check($word) {
   //echo $word .'<br>';
   // Setup an array of vowels
   $vowels = array('a', 'e', 'i', 'o', 'u');
   
   // Make an return array
   $return_array = array("found" => false);
   
   // Make a counter
   $ii = 0;
   
   // Loop the letters
   for($i=0; $i<strlen($word); $i++) {
     
      // Check for vowels
      if(in_array($word[$i], $vowels)) {
         
         // Set found to true
         $return_array['found'] = true;
         
         // Build the info
         $return_array['vowels'][$ii]['letter'] = $word[$i];
         $return_array['vowels'][$ii]['pos'] = strpos($word, $word[$i]);
         
         // Add to counter
         $ii++;
      }
     
   }
   
   // Return $return_array
   return $return_array;
     
}
 
 
function check_y($word) {
   
   // Start the class
   $syllable = new Syllable('en-us');
   
   // Make th word lower case
   $word = strtolower($word);
 
   // Check for Y
   if(strpos($word, 'y') === false) {
     
      // No Y found
      return 'No Y Found';
   
   }
   
   // Make an array to return info
   $return_array = array();
   
   // Breakword into syllables
   $syllable->setTreshold(Syllable::TRESHOLD_MOST);
   $word_array = explode('&shy;', utf8_encode($syllable->hyphenateText($word)));
   
   // Loop the word array
   for($i=0; $i<count($word_array); $i++) {
     
      // Check for vowels
      $vowels_check = vowel_check($word_array[$i]);
   
      // Check for no vowels
      if(!$vowels_check['found']) {
         // No Vowels found so Y is a vowel
         $return_array[$word][$i] = 'vowel';
         
      } else {
 
         //echo $word_array[$i] . ' ';
         // Loop Vowels found
         for($j=0; $j<count($vowels_check['vowels']); $j++) {
           
            // Check for Y
            if(strpos($word_array[$i], 'y') !== false) {
 
               // Check position of vowel
               if($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y')) {
               echo $word_array[$i] . ' in If';
                  // Y is after vowel so it is a constant
                  $return_array[$word][$i] = 'consonant<br>';
                 
               } else if(strpos($word_array[$i], 'y') + 1 == $vowels_check['vowels'][$j]['pos']) {
                  echo $word_array[$i] . ' in else If<br>';
                  // Y is before vowel so it is a Vowel
                  $return_array[$word][$i] = 'vowel';
                 
               } else {
                  echo $word_array[$i] . ' in else<br><br>';
                  // Y is before vowel so it is a Vowel
                  $return_array[$word][$i] = 'vowel';
                 
               }
           
            }
           
         }
         
      }
     
   }
     
   // Return the return array
   return $return_array;
   
}
 
echo '<pre>';
var_dump(check_text('Sydney Billy Sylvia Missy Kyle Blythe Sylvester Katy Kay Yeltsin May Kuykendahl yes yam yell yellow yogurt yale gym my cycle baby hairy sky Lynn Yvonne Mary Betty Elly Bryan Maloney Murray Wyatt'));
//check_text('Sylvia');
echo '</pre>';
 
function check_text($text) {
   
   // Make sure we have text
   if(empty($text)) {
     
      // Kick back
      return false;
     
   }
   
   // Break text into words
   $text_array = explode(' ', $text);
   
   // Make an array to return info
   $return_array = array();
   
   // Loop Text
   for($i=0; $i<count($text_array); $i++) {
     
      // Add to the return array
      $return_array[] = check_y($text_array[$i]);
     
   }
   
   // Return the array
   return $return_array;
   
}
 
?>
done
 
  1. start
  2. <?php
  3.  
  4. // Include the class
  5. require_once(dirname(__FILE__) . '/Syllable.php');
  6.  
  7. function vowel_check($word) {
  8.    //echo $word .'<br>';
  9.    // Setup an array of vowels
  10.    $vowels = array('a', 'e', 'i', 'o', 'u');
  11.    
  12.    // Make an return array
  13.    $return_array = array("found" => false);
  14.    
  15.    // Make a counter
  16.    $ii = 0;
  17.    
  18.    // Loop the letters
  19.    for($i=0; $i<strlen($word); $i++) {
  20.      
  21.       // Check for vowels
  22.       if(in_array($word[$i], $vowels)) {
  23.          
  24.          // Set found to true
  25.          $return_array['found'] = true;
  26.          
  27.          // Build the info
  28.          $return_array['vowels'][$ii]['letter'] = $word[$i];
  29.          $return_array['vowels'][$ii]['pos'] = strpos($word, $word[$i]);
  30.          
  31.          // Add to counter
  32.          $ii++;
  33.       }
  34.      
  35.    }
  36.    
  37.    // Return $return_array
  38.    return $return_array;
  39.      
  40. }
  41.  
  42.  
  43. function check_y($word) {
  44.    
  45.    // Start the class
  46.    $syllable = new Syllable('en-us');
  47.    
  48.    // Make th word lower case
  49.    $word = strtolower($word);
  50.  
  51.    // Check for Y
  52.    if(strpos($word, 'y') === false) {
  53.      
  54.       // No Y found
  55.       return 'No Y Found';
  56.    
  57.    }
  58.    
  59.    // Make an array to return info
  60.    $return_array = array();
  61.    
  62.    // Breakword into syllables
  63.    $syllable->setTreshold(Syllable::TRESHOLD_MOST);
  64.    $word_array = explode('&shy;', utf8_encode($syllable->hyphenateText($word)));
  65.    
  66.    // Loop the word array
  67.    for($i=0; $i<count($word_array); $i++) {
  68.      
  69.       // Check for vowels
  70.       $vowels_check = vowel_check($word_array[$i]);
  71.    
  72.       // Check for no vowels
  73.       if(!$vowels_check['found']) {
  74.          // No Vowels found so Y is a vowel
  75.          $return_array[$word][$i] = 'vowel';
  76.          
  77.       } else {
  78.  
  79.          //echo $word_array[$i] . ' ';
  80.          // Loop Vowels found
  81.          for($j=0; $j<count($vowels_check['vowels']); $j++) {
  82.            
  83.             // Check for Y
  84.             if(strpos($word_array[$i], 'y') !== false) {
  85.  
  86.                // Check position of vowel
  87.                if($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y')) {
  88.                echo $word_array[$i] . ' in If';
  89.                   // Y is after vowel so it is a constant
  90.                   $return_array[$word][$i] = 'consonant<br>';
  91.                  
  92.                } else if(strpos($word_array[$i], 'y') + 1 == $vowels_check['vowels'][$j]['pos']) {
  93.                   echo $word_array[$i] . ' in else If<br>';
  94.                   // Y is before vowel so it is a Vowel
  95.                   $return_array[$word][$i] = 'vowel';
  96.                  
  97.                } else {
  98.                   echo $word_array[$i] . ' in else<br><br>';
  99.                   // Y is before vowel so it is a Vowel
  100.                   $return_array[$word][$i] = 'vowel';
  101.                  
  102.                }
  103.            
  104.             }
  105.            
  106.          }
  107.          
  108.       }
  109.      
  110.    }
  111.      
  112.    // Return the return array
  113.    return $return_array;
  114.    
  115. }
  116.  
  117. echo '<pre>';
  118. var_dump(check_text('Sydney Billy Sylvia Missy Kyle Blythe Sylvester Katy Kay Yeltsin May Kuykendahl yes yam yell yellow yogurt yale gym my cycle baby hairy sky Lynn Yvonne Mary Betty Elly Bryan Maloney Murray Wyatt'));
  119. //check_text('Sylvia');
  120. echo '</pre>';
  121.  
  122. function check_text($text) {
  123.    
  124.    // Make sure we have text
  125.    if(empty($text)) {
  126.      
  127.       // Kick back
  128.       return false;
  129.      
  130.    }
  131.    
  132.    // Break text into words
  133.    $text_array = explode(' ', $text);
  134.    
  135.    // Make an array to return info
  136.    $return_array = array();
  137.    
  138.    // Loop Text
  139.    for($i=0; $i<count($text_array); $i++) {
  140.      
  141.       // Add to the return array
  142.       $return_array[] = check_y($text_array[$i]);
  143.      
  144.    }
  145.    
  146.    // Return the array
  147.    return $return_array;
  148.    
  149. }
  150.  
  151. ?>
  152. done
  153.  
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

Some of the comments will not help as they were copied from other parts of this script I was trying to flush out any issues I was having
Moderator Remark: That new zip file works for me, for some reason the other one is corrupt. I removed that attachment on your older post.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi ScottG

Got a response from Germany that snuck into our post
Moderator should have a look at this.

Anyways, I downloaded you file and was able to open it and test it out.

It gave the letter y in some cases as a vowel when it should have been a consonant.

I used the following to test for y being a consonat starting with the letter y and put in some other words as control words:

check_text('yacht yesterday yellow york you maybe yam yet yes yell yogurt yale day obey yoland yoda yeltsin player sydney')

According to what I found, Y is also considered a consonant when it appears at the start of a syllable where there is another vowel (yam, yet).

The following are the results:

yacht - vowel
yesterday - first vowel and last consonant
yellow - vowel
york - vowel
you - vowel
maybe - vowel
yam - vowel
yet - vowel
yes - vowel
yell - vowel
yogurt - vowel
yale - vowel
day - consonant
obey - consonant
yoland - vowel
yoda - vowel
yeltsin - vowel
player - consonant
sydney - first vowel last consonant

When y was used as firts letter, it should have been a consonant, not a vowel.

For yesterday it was half right with y being consonant at the end

For sydney, it was competely correct, first y vowel and last y consonant.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

Its not completely working correct the one you downloaded was the newest one i was working on I'm still working on the first position I had it working in one test but it caused the word Yeltsin to be a vowel instead on a consonant.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I tried the word boy as another check which should have been a vowel but it was stated as a consonant.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

If i change the vowel loop to
PHP Code: [ Select ]
// Loop Vowels found
         for($j=0; $j<count($vowels_check['vowels']); $j++) {
           
            // Check for Y
            if(strpos($word_array[$i], 'y') !== false) {
 
               // Check position of vowel
               if($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') || strpos($word_array[$i], 'y') == 0) {
               echo $word_array[$i] . ' in If';
                  // Y is after vowel so it is a constant
                  $return_array[$word][$i] = 'consonant<br>';
                 
               } /*else if(strpos($word_array[$i], 'y') + 1 == $vowels_check['vowels'][$j]['pos']) {
                  echo $word_array[$i] . ' in else If<br>';
                  // Y is before vowel so it is a Vowel
                  $return_array[$word][$i] = 'vowel';
                 
               }*/ else {
                  echo $word_array[$i] . ' in else<br><br>';
                  // Y is before vowel so it is a Vowel
                  $return_array[$word][$i] = 'vowel';
                 
               }
           
            }
           
         }
 
  1. // Loop Vowels found
  2.          for($j=0; $j<count($vowels_check['vowels']); $j++) {
  3.            
  4.             // Check for Y
  5.             if(strpos($word_array[$i], 'y') !== false) {
  6.  
  7.                // Check position of vowel
  8.                if($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') || strpos($word_array[$i], 'y') == 0) {
  9.                echo $word_array[$i] . ' in If';
  10.                   // Y is after vowel so it is a constant
  11.                   $return_array[$word][$i] = 'consonant<br>';
  12.                  
  13.                } /*else if(strpos($word_array[$i], 'y') + 1 == $vowels_check['vowels'][$j]['pos']) {
  14.                   echo $word_array[$i] . ' in else If<br>';
  15.                   // Y is before vowel so it is a Vowel
  16.                   $return_array[$word][$i] = 'vowel';
  17.                  
  18.                }*/ else {
  19.                   echo $word_array[$i] . ' in else<br><br>';
  20.                   // Y is before vowel so it is a Vowel
  21.                   $return_array[$word][$i] = 'vowel';
  22.                  
  23.                }
  24.            
  25.             }
  26.            
  27.          }
  28.  


Almost all cases work except Yvonne gets messed up again
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

This issue could be caused by the syllable class not splitting Yvonne up
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

Updated check y function
PHP Code: [ Select ]
function check_y($word) {
   
   // Start the class
   $syllable = new Syllable('en-us');
   
   // Make th word lower case
   $word = strtolower($word);
 
   // Check for Y
   if(strpos($word, 'y') === false) {
     
      // No Y found
      return 'No Y Found';
   
   }
   
   // Make an array to return info
   $return_array = array();
   
   // Breakword into syllables
   $syllable->setTreshold(Syllable::TRESHOLD_MOST);
   $word_array = explode('&shy;', utf8_encode($syllable->hyphenateText($word)));
   
   // Loop the word array
   for($i=0; $i<count($word_array); $i++) {
     
      // Check for vowels
      $vowels_check = vowel_check($word_array[$i]);
   
      // Check for no vowels
      if(!$vowels_check['found']) {
         // Check for Y
         if(strpos($word_array[$i], 'y') !== false) {
           
            // No Vowels found so Y is a vowel
            $return_array[$word][$i] = 'vowel';
         
         }
         
      } else {
 
         //echo $word_array[$i] . ' ';
         // Loop Vowels found
         for($j=0; $j<count($vowels_check['vowels']); $j++) {
           
            // Check for Y
            if(strpos($word_array[$i], 'y') !== false) {
 
               // Check position of vowel
               if($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') || strpos($word_array[$i], 'y') == 0) {
               
                  // Y is after vowel so it is a constant
                  $return_array[$word][$i] = 'consonant<br>';
                 
               } else {
                 
                  // Y is before vowel so it is a Vowel
                  $return_array[$word][$i] = 'vowel';
                 
               }
           
            }
           
         }
         
      }
     
   }
     
   // Return the return array
   return $return_array;
   
}
  1. function check_y($word) {
  2.    
  3.    // Start the class
  4.    $syllable = new Syllable('en-us');
  5.    
  6.    // Make th word lower case
  7.    $word = strtolower($word);
  8.  
  9.    // Check for Y
  10.    if(strpos($word, 'y') === false) {
  11.      
  12.       // No Y found
  13.       return 'No Y Found';
  14.    
  15.    }
  16.    
  17.    // Make an array to return info
  18.    $return_array = array();
  19.    
  20.    // Breakword into syllables
  21.    $syllable->setTreshold(Syllable::TRESHOLD_MOST);
  22.    $word_array = explode('&shy;', utf8_encode($syllable->hyphenateText($word)));
  23.    
  24.    // Loop the word array
  25.    for($i=0; $i<count($word_array); $i++) {
  26.      
  27.       // Check for vowels
  28.       $vowels_check = vowel_check($word_array[$i]);
  29.    
  30.       // Check for no vowels
  31.       if(!$vowels_check['found']) {
  32.          // Check for Y
  33.          if(strpos($word_array[$i], 'y') !== false) {
  34.            
  35.             // No Vowels found so Y is a vowel
  36.             $return_array[$word][$i] = 'vowel';
  37.          
  38.          }
  39.          
  40.       } else {
  41.  
  42.          //echo $word_array[$i] . ' ';
  43.          // Loop Vowels found
  44.          for($j=0; $j<count($vowels_check['vowels']); $j++) {
  45.            
  46.             // Check for Y
  47.             if(strpos($word_array[$i], 'y') !== false) {
  48.  
  49.                // Check position of vowel
  50.                if($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') || strpos($word_array[$i], 'y') == 0) {
  51.                
  52.                   // Y is after vowel so it is a constant
  53.                   $return_array[$word][$i] = 'consonant<br>';
  54.                  
  55.                } else {
  56.                  
  57.                   // Y is before vowel so it is a Vowel
  58.                   $return_array[$word][$i] = 'vowel';
  59.                  
  60.                }
  61.            
  62.             }
  63.            
  64.          }
  65.          
  66.       }
  67.      
  68.    }
  69.      
  70.    // Return the return array
  71.    return $return_array;
  72.    
  73. }
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

The more I dig into this, the more differing opinions I get, even why I contacted some english teaches.

To try an narrow this done I have come up with 2 things that I was given that I will use as a basis for testing.

Rule 1
If you replace the letter y with the sound /j/ and the word sounds almost the same then it is a consonant, even though you would appear to have an accent.
e.g., yell would almost sound the same with /j/ so y is consonant
day would not sound the same with /j/, so y is a vowel.

Rule 2
Y is a consonant when it appears at the start of a syllable where there is another vowel (yam, yet).

Rule 3
If there are no vowels in the word then y is a vowel such as gym.
If it takes the place of a vowel, then y is a vowel such as system.

I don't know how correct I am, but this is where I would like to start.

Based on the above I created the following for testing:

For Vowels:

check_test('anyone baby betty boy bryan by bycycle candy cry cycle day dry dynamic elly fly flying goodbye gym happy hairy hymn joy lynn many mary my mystery myth play pony pry psycho quickly rye rhyme rhythm say shy sky sly spy soy symbol syringe tiny trendy try very vinyl why wyatt yvonne')

For consonants:

check_test('hey key kyle lloyd oyster player yacht yahoo yale yam yarn yield yell yellow yeltsin yes yesterday yet yoda yogurt yoke yolanda you young your yvette royce')

For both:

check_test('sydney')

If anyone has any further suggestions I would be very happy to hear from you.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

hey scottG

In my rush to set down some rules, I may have mixed things up a bit.

When you have a working script, please advise and then I will test carefully what you have.

Thanks
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

Rule 1 is the rule that is going to be the most difficult to get correct.

is there a correlation to the vowel to the y that would constantly be the same

Example
oy ay and uy being vowels ... boy day buy
and ey being consonants ... hey

If this is the case this should make things a bit easier
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

also shouldn't yvette be considered a vowel since y makes an e sound?
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

Also these words

These have the oy which in your example "soy" was a vowel
royce
lloyd
oyster

And player has an a sound like day
player

If all of my post are correct i think i may have a modified script that meets all the rules that can be tested
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

Here is updated code to test with

PHP Code: [ Select ]
<?php
 
// Include the class
require_once(dirname(__FILE__) . '/Syllable.php');
 
function vowel_check($word) {
   
   // Setup an array of vowels
   $vowels = array('a', 'e', 'i', 'o', 'u');
   
   // Make an return array
   $return_array = array("found" => false);
   
   // Make a counter
   $ii = 0;
   
   // Loop the letters
   for($i=0; $i<strlen($word); $i++) {
     
      // Check for vowels
      if(in_array($word[$i], $vowels)) {
         
         // Set found to true
         $return_array['found'] = true;
         
         // Build the info
         $return_array['vowels'][$ii]['letter'] = $word[$i];
         $return_array['vowels'][$ii]['pos'] = strpos($word, $word[$i]);
         
         // Add to counter
         $ii++;
      }
     
   }
   
   // Return $return_array
   return $return_array;
     
}
 
 
function check_y($word) {
   
   // Start the class
   $syllable = new Syllable('en-us');
   
   // Make th word lower case
   $word = strtolower($word);
 
   // Check for Y
   if(strpos($word, 'y') === false) {
     
      // No Y found
      return 'No Y Found';
   
   }
   
   // Make an array to return info
   $return_array = array();
   
   // Breakword into syllables
   $syllable->setTreshold(Syllable::TRESHOLD_MOST);
   $word_array = explode('&shy;', utf8_encode($syllable->hyphenateText($word)));
   
   // Loop the word array
   for($i=0; $i<count($word_array); $i++) {
     
      // Check for vowels
      $vowels_check = vowel_check($word_array[$i]);
   
      // Check for no vowels
      if(!$vowels_check['found']) {
         // Check for Y
         if(strpos($word_array[$i], 'y') !== false) {
           
            if(strpos($word_array[$i], 'y') == 0) {
           
               // No Vowels and y is the first letter of the word
               $return_array[$word][$i] = 'consonant';
           
            } else {
           
               // No Vowels found so Y is a vowel
               $return_array[$word][$i] = 'vowel';
           
            }
         
         }
         
      } else {
 
         
         // Loop Vowels found
         for($j=0; $j<count($vowels_check['vowels']); $j++) {
           
            // Check for Y
            if(strpos($word_array[$i], 'y') !== false) {
               
               // Check position of vowel
               switch(true) {
                 
                  // Vowel before and vowel being o a or u
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'a'):
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'o'):
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'u'):
                     
                     // Y is s vowel
                     $return_array[$word][$i] = 'vowel';
                 
                     // Kickout
                     break;
                     
                  // Vowel before and vowel being e or i
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'e'):
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'i'):
                     
                     // Y is s consonant
                     $return_array[$word][$i] = 'consonant';
                 
                     // Kickout
                     break;
                 
                  // YV fix the syllable class does not break the yv apart into the correct syllable grouping so this is a fix to
                  // counter act this issue
                  case (strtolower($word_array[$i][strpos($word_array[$i], 'y')+1]) == 'v'):
                     
                     // Y is s vowel
                     $return_array[$word][$i] = 'vowel';
                 
                     // Kickout
                     break;
                     
                  // Y is first letter
                  case (strpos($word_array[$i], 'y') == 0):
                     
                     // Y is s consonant
                     $return_array[$word][$i] = 'consonant';
                 
                     // Kickout
                     break;
                 
               }
           
            }
           
         }
         
      }
     
   }
     
   // Return the return array
   return $return_array;
   
}
 
echo '<pre>';
var_dump(check_text('boy Sydney Billy Sylvia Missy Kyle Blythe Sylvester Katy Kay Yeltsin May Kuykendahl yes yam yell yellow yogurt yale gym my cycle baby hairy sky Lynn Yvonne Mary Betty Elly Bryan Maloney Murray Wyatt'));
 
//var_dump(check_text('anyone baby betty boy bryan by bycycle candy cry cycle day dry dynamic elly fly flying goodbye gym happy hairy hymn joy lynn many mary my mystery myth play pony pry psycho quickly rye rhyme rhythm say shy sky sly spy soy symbol syringe tiny trendy try very vinyl why wyatt yvonne yvette'));
 
//var_dump(check_text('hey key kyle lloyd oyster player yacht yahoo yale yam yarn yield yell yellow yeltsin yes yesterday yet yoda yogurt yoke yolanda you young your royce'));
 
//check_text('Sylvia');
echo '</pre>';
 
function check_text($text) {
   
   // Make sure we have text
   if(empty($text)) {
     
      // Kick back
      return false;
     
   }
   
   // Break text into words
   $text_array = explode(' ', $text);
   
   // Make an array to return info
   $return_array = array();
   
   // Loop Text
   for($i=0; $i<count($text_array); $i++) {
     
      // Check the word
      $temp_array = check_y($text_array[$i]);
     
      if(!empty($temp_array)) {
         
         // Add to the return array
         $return_array[] = $temp_array;
         
      }
     
   }
   
   // Return the array
   return $return_array;
   
}
 
?>
 
  1. <?php
  2.  
  3. // Include the class
  4. require_once(dirname(__FILE__) . '/Syllable.php');
  5.  
  6. function vowel_check($word) {
  7.    
  8.    // Setup an array of vowels
  9.    $vowels = array('a', 'e', 'i', 'o', 'u');
  10.    
  11.    // Make an return array
  12.    $return_array = array("found" => false);
  13.    
  14.    // Make a counter
  15.    $ii = 0;
  16.    
  17.    // Loop the letters
  18.    for($i=0; $i<strlen($word); $i++) {
  19.      
  20.       // Check for vowels
  21.       if(in_array($word[$i], $vowels)) {
  22.          
  23.          // Set found to true
  24.          $return_array['found'] = true;
  25.          
  26.          // Build the info
  27.          $return_array['vowels'][$ii]['letter'] = $word[$i];
  28.          $return_array['vowels'][$ii]['pos'] = strpos($word, $word[$i]);
  29.          
  30.          // Add to counter
  31.          $ii++;
  32.       }
  33.      
  34.    }
  35.    
  36.    // Return $return_array
  37.    return $return_array;
  38.      
  39. }
  40.  
  41.  
  42. function check_y($word) {
  43.    
  44.    // Start the class
  45.    $syllable = new Syllable('en-us');
  46.    
  47.    // Make th word lower case
  48.    $word = strtolower($word);
  49.  
  50.    // Check for Y
  51.    if(strpos($word, 'y') === false) {
  52.      
  53.       // No Y found
  54.       return 'No Y Found';
  55.    
  56.    }
  57.    
  58.    // Make an array to return info
  59.    $return_array = array();
  60.    
  61.    // Breakword into syllables
  62.    $syllable->setTreshold(Syllable::TRESHOLD_MOST);
  63.    $word_array = explode('&shy;', utf8_encode($syllable->hyphenateText($word)));
  64.    
  65.    // Loop the word array
  66.    for($i=0; $i<count($word_array); $i++) {
  67.      
  68.       // Check for vowels
  69.       $vowels_check = vowel_check($word_array[$i]);
  70.    
  71.       // Check for no vowels
  72.       if(!$vowels_check['found']) {
  73.          // Check for Y
  74.          if(strpos($word_array[$i], 'y') !== false) {
  75.            
  76.             if(strpos($word_array[$i], 'y') == 0) {
  77.            
  78.                // No Vowels and y is the first letter of the word
  79.                $return_array[$word][$i] = 'consonant';
  80.            
  81.             } else {
  82.            
  83.                // No Vowels found so Y is a vowel
  84.                $return_array[$word][$i] = 'vowel';
  85.            
  86.             }
  87.          
  88.          }
  89.          
  90.       } else {
  91.  
  92.          
  93.          // Loop Vowels found
  94.          for($j=0; $j<count($vowels_check['vowels']); $j++) {
  95.            
  96.             // Check for Y
  97.             if(strpos($word_array[$i], 'y') !== false) {
  98.                
  99.                // Check position of vowel
  100.                switch(true) {
  101.                  
  102.                   // Vowel before and vowel being o a or u
  103.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'a'):
  104.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'o'):
  105.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'u'):
  106.                      
  107.                      // Y is s vowel
  108.                      $return_array[$word][$i] = 'vowel';
  109.                  
  110.                      // Kickout
  111.                      break;
  112.                      
  113.                   // Vowel before and vowel being e or i
  114.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'e'):
  115.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'i'):
  116.                      
  117.                      // Y is s consonant
  118.                      $return_array[$word][$i] = 'consonant';
  119.                  
  120.                      // Kickout
  121.                      break;
  122.                  
  123.                   // YV fix the syllable class does not break the yv apart into the correct syllable grouping so this is a fix to
  124.                   // counter act this issue
  125.                   case (strtolower($word_array[$i][strpos($word_array[$i], 'y')+1]) == 'v'):
  126.                      
  127.                      // Y is s vowel
  128.                      $return_array[$word][$i] = 'vowel';
  129.                  
  130.                      // Kickout
  131.                      break;
  132.                      
  133.                   // Y is first letter
  134.                   case (strpos($word_array[$i], 'y') == 0):
  135.                      
  136.                      // Y is s consonant
  137.                      $return_array[$word][$i] = 'consonant';
  138.                  
  139.                      // Kickout
  140.                      break;
  141.                  
  142.                }
  143.            
  144.             }
  145.            
  146.          }
  147.          
  148.       }
  149.      
  150.    }
  151.      
  152.    // Return the return array
  153.    return $return_array;
  154.    
  155. }
  156.  
  157. echo '<pre>';
  158. var_dump(check_text('boy Sydney Billy Sylvia Missy Kyle Blythe Sylvester Katy Kay Yeltsin May Kuykendahl yes yam yell yellow yogurt yale gym my cycle baby hairy sky Lynn Yvonne Mary Betty Elly Bryan Maloney Murray Wyatt'));
  159.  
  160. //var_dump(check_text('anyone baby betty boy bryan by bycycle candy cry cycle day dry dynamic elly fly flying goodbye gym happy hairy hymn joy lynn many mary my mystery myth play pony pry psycho quickly rye rhyme rhythm say shy sky sly spy soy symbol syringe tiny trendy try very vinyl why wyatt yvonne yvette'));
  161.  
  162. //var_dump(check_text('hey key kyle lloyd oyster player yacht yahoo yale yam yarn yield yell yellow yeltsin yes yesterday yet yoda yogurt yoke yolanda you young your royce'));
  163.  
  164. //check_text('Sylvia');
  165. echo '</pre>';
  166.  
  167. function check_text($text) {
  168.    
  169.    // Make sure we have text
  170.    if(empty($text)) {
  171.      
  172.       // Kick back
  173.       return false;
  174.      
  175.    }
  176.    
  177.    // Break text into words
  178.    $text_array = explode(' ', $text);
  179.    
  180.    // Make an array to return info
  181.    $return_array = array();
  182.    
  183.    // Loop Text
  184.    for($i=0; $i<count($text_array); $i++) {
  185.      
  186.       // Check the word
  187.       $temp_array = check_y($text_array[$i]);
  188.      
  189.       if(!empty($temp_array)) {
  190.          
  191.          // Add to the return array
  192.          $return_array[] = $temp_array;
  193.          
  194.       }
  195.      
  196.    }
  197.    
  198.    // Return the array
  199.    return $return_array;
  200.    
  201. }
  202.  
  203. ?>
  204.  
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hey ScottG

After going over everything carefully, I had been wrong in a few areas. After hours of investigating, I believe the following is probably the most correct for the letter y.

1. words with "oy" the letter y is a vowel
e.g. boy, joy, toy, roy

2. words with ay, ey and uy the letter y is a consonant
e.g., pay, say, day, key, buy, mayor, murray, maloney

3. Y is a consonant when it appears at the start of a syllable where there is another vowel (yam, yet, yolanda, yoda), creating the "yuh" sound. In the word player, the "yuh" sound is heard, so y is a consonant.

4. If there are no vowels in the word then y is a vowel such as gym, Lynn, Yvonne, Yvette, Mary, Betty, Elly, and Bryan.
If it takes the place of a vowel, then y is a vowel such as system, Bryan and Wyatt.

5. Other examples

-In Billy, Sylvia, Missy, Kyle, Blythe, Sylvester, and Katy, the Y is a vowel
-In gym, my, cycle, baby, hairy, sky the y is a vowel

-In Kay, Yeltsin, May, and Kuykendahl, the Y is a consonant.
-In Sydney, the first Y is a vowel, the second Y is a consonant.
-In yes, yam, yell, yellow, yogurt, yale the y is a consonant.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I ran your new script and based on new rules in my previous post the following needs changing:

kay: y stated as vowel but should be consonant
may: y stated as vowel but should be consonant
kuykendahl: y stated as vowel but should be consonant
murray: y stated as vowel but should be consonant

I investigated these words further and they were indeed all specified as y being a consonant.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

ok So here is the altered check y function based on the new rules set

Passed all examples you provided
PHP Code: [ Select ]
function check_y($word) {
   
   // Start the class
   $syllable = new Syllable('en-us');
   
   // Make th word lower case
   $word = strtolower($word);
 
   // Check for Y
   if(strpos($word, 'y') === false) {
     
      // No Y found
      return 'No Y Found';
   
   }
   
   // Make an array to return info
   $return_array = array();
   
   // Breakword into syllables
   $syllable->setTreshold(Syllable::TRESHOLD_MOST);
   $word_array = explode('&shy;', utf8_encode($syllable->hyphenateText($word)));
   
   // Loop the word array
   for($i=0; $i<count($word_array); $i++) {
     
      // Check for vowels
      $vowels_check = vowel_check($word_array[$i]);
   
      // Check for no vowels
      if(!$vowels_check['found']) {
         // Check for Y
         if(strpos($word_array[$i], 'y') !== false) {
           
            if(strpos($word_array[$i], 'y') == 0) {
           
               // No Vowels and y is the first letter of the word
               $return_array[$word][$i] = 'consonant';
           
            } else {
           
               // No Vowels found so Y is a vowel
               $return_array[$word][$i] = 'vowel';
           
            }
         
         }
         
      } else {
 
         // Loop Vowels found
         for($j=0; $j<count($vowels_check['vowels']); $j++) {
           
            // Check for Y
            if(strpos($word_array[$i], 'y') !== false) {
               
               // Check position of vowel
               switch(true) {
                 
                  // Vowel before and vowel being o
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'o'):
                     
                     // Y is s vowel
                     $return_array[$word][$i] = 'vowel';
                 
                     // Kickout
                     break;
                     
                  // Vowel before and vowel being a, e, i or u
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'a'):
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'e'):
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'i'):
                  case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'u'):
                     
                     // Y is s consonant
                     $return_array[$word][$i] = 'consonant';
                 
                     // Kickout
                     break;
                 
                  // YV fix the sylible class does not break the yv apart into the correct sylible grouping so this is a fix to
                  // counter act this issue
                  case (strtolower($word_array[$i][strpos($word_array[$i], 'y')+1]) == 'v'):
                     
                     // Y is s vowel
                     $return_array[$word][$i] = 'vowel';
                 
                     // Kickout
                     break;
                     
                  // Y is first letter
                  case (strpos($word_array[$i], 'y') == 0):
                     
                     // Y is s consonant
                     $return_array[$word][$i] = 'consonant';
                 
                     // Kickout
                     break;
                 
                  // Y not next two vowels
                  default:
                     
                     // Y is s vowel
                     $return_array[$word][$i] = 'vowel';
                 
                     // Kickout
                     break;
                 
               }
           
            }
           
         }
         
      }
     
   }
     
   // Return the return array
   return $return_array;
   
}
 
  1. function check_y($word) {
  2.    
  3.    // Start the class
  4.    $syllable = new Syllable('en-us');
  5.    
  6.    // Make th word lower case
  7.    $word = strtolower($word);
  8.  
  9.    // Check for Y
  10.    if(strpos($word, 'y') === false) {
  11.      
  12.       // No Y found
  13.       return 'No Y Found';
  14.    
  15.    }
  16.    
  17.    // Make an array to return info
  18.    $return_array = array();
  19.    
  20.    // Breakword into syllables
  21.    $syllable->setTreshold(Syllable::TRESHOLD_MOST);
  22.    $word_array = explode('&shy;', utf8_encode($syllable->hyphenateText($word)));
  23.    
  24.    // Loop the word array
  25.    for($i=0; $i<count($word_array); $i++) {
  26.      
  27.       // Check for vowels
  28.       $vowels_check = vowel_check($word_array[$i]);
  29.    
  30.       // Check for no vowels
  31.       if(!$vowels_check['found']) {
  32.          // Check for Y
  33.          if(strpos($word_array[$i], 'y') !== false) {
  34.            
  35.             if(strpos($word_array[$i], 'y') == 0) {
  36.            
  37.                // No Vowels and y is the first letter of the word
  38.                $return_array[$word][$i] = 'consonant';
  39.            
  40.             } else {
  41.            
  42.                // No Vowels found so Y is a vowel
  43.                $return_array[$word][$i] = 'vowel';
  44.            
  45.             }
  46.          
  47.          }
  48.          
  49.       } else {
  50.  
  51.          // Loop Vowels found
  52.          for($j=0; $j<count($vowels_check['vowels']); $j++) {
  53.            
  54.             // Check for Y
  55.             if(strpos($word_array[$i], 'y') !== false) {
  56.                
  57.                // Check position of vowel
  58.                switch(true) {
  59.                  
  60.                   // Vowel before and vowel being o
  61.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'o'):
  62.                      
  63.                      // Y is s vowel
  64.                      $return_array[$word][$i] = 'vowel';
  65.                  
  66.                      // Kickout
  67.                      break;
  68.                      
  69.                   // Vowel before and vowel being a, e, i or u
  70.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'a'):
  71.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'e'):
  72.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'i'):
  73.                   case ($vowels_check['vowels'][$j]['pos'] + 1 == strpos($word_array[$i], 'y') && $vowels_check['vowels'][$j]['letter'] == 'u'):
  74.                      
  75.                      // Y is s consonant
  76.                      $return_array[$word][$i] = 'consonant';
  77.                  
  78.                      // Kickout
  79.                      break;
  80.                  
  81.                   // YV fix the sylible class does not break the yv apart into the correct sylible grouping so this is a fix to
  82.                   // counter act this issue
  83.                   case (strtolower($word_array[$i][strpos($word_array[$i], 'y')+1]) == 'v'):
  84.                      
  85.                      // Y is s vowel
  86.                      $return_array[$word][$i] = 'vowel';
  87.                  
  88.                      // Kickout
  89.                      break;
  90.                      
  91.                   // Y is first letter
  92.                   case (strpos($word_array[$i], 'y') == 0):
  93.                      
  94.                      // Y is s consonant
  95.                      $return_array[$word][$i] = 'consonant';
  96.                  
  97.                      // Kickout
  98.                      break;
  99.                  
  100.                   // Y not next two vowels
  101.                   default:
  102.                      
  103.                      // Y is s vowel
  104.                      $return_array[$word][$i] = 'vowel';
  105.                  
  106.                      // Kickout
  107.                      break;
  108.                  
  109.                }
  110.            
  111.             }
  112.            
  113.          }
  114.          
  115.       }
  116.      
  117.    }
  118.      
  119.    // Return the return array
  120.    return $return_array;
  121.    
  122. }
  123.  
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I figured if I dug enough, I would eventually find a general consensus on how to determine when the letter Y was a vowel or a consonant. Fortunately for me, it agreed with the rules I plan to use.

The following is what I found:

If a syllable begins with Y, and the next letter represents a vowel, then Y almost certainly represents a consonant. In “yo” for example, Y represent a consonant.

If a syllable begins with Y and the next letter represents a consonant, then the Y represents a vowel such as, Yvonne, Yvette, Yves, yttrium.

In words like “rhythm,” Y represents a vowel, specifically the short I sound. However, in a word such as the name “Reynold,” it doesn’t make sense to say that Y represents a vowel. It’s the two-letter combination E-Y that represents a vowel so y here is a consonant.

In one-syllable words such as “by” and “fly,” Y represents the vowel commonly known as long I.
In longer words such as “sorry” and “friendly,” it represents the vowel of long E (or maybe short I again—speakers vary).

In words like “hey” or “day, the letter combinations of E-Y and A-Y together represent the long A sound in these words so y here is a consonant.

For the Y at the end of a word like “boy”, “oy” is a diphthong, consisting of an O-like vowel followed by long E or short I.
There is debate among phoneticians on wether y is a vowel or consonant. They don’t all agree on this.
In my case I have chosen to make it a vowel.

So then, Y gets to represent a vowel in many words. It represents short I in words like “gym,” and either short I or long E in words like “happy.” It represents a diphthong in words like “by.”
  • alemao_brazil
  • Newbie
  • Newbie
  • alemao_brazil
  • Posts: 8

Post 3+ Months Ago

Has anybody taken a look on my suggestions (preg_match?)
It seems I posted in vain, I didn't see any reply or comment...
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

I did do a test with your suggestion on using preg_match and using words like Sydney where y is both a vowel and a consonant It doesn't return anything. Sorry I didn't respond to your post earlier I've been a bit busy building a Christmas tree and before I went to bed or when i woke up and couldn't sleep I was working on my solution. It may have worked if tweaked a bit I never really got around to tweaking it though. Below is the code i used to test your suggestion

PHP Code: [ Select ]
start
<?php
 
function check_y($word) {
   
   // Make an array to return info
   $return_array = array();
   
   # Credits: Sergio Abreu
   # Matches y followed by vowel in any place...
   
   if( preg_match( "/y[aeiou]/i", $word)){
      $return_array[$word] = 'consonant';
   } elseif( preg_match( "/[b-z]y/i", $word) && ! preg_match( "/[aeiou]y/i", $word)){
      $return_array[$word] = 'vowel';
   }
   
   # To restrict y being the first letter, use ^ :
   
   if( preg_match( "/^y[aeiou]/i", $word)){
      $return_array[$word] = 'consonant';
   } elseif( preg_match( "/[b-z]y/i", $word) && ! preg_match( "/[aeiou]y/i", $word)){
     
      $return_array[$word] = 'vowel';
   }
     
   // Return the return array
   return $return_array;
   
}
 
echo '<pre>';
var_dump(check_text('Sydney Billy Sylvia Missy Kyle Blythe Sylvester Katy Kay Yeltsin May Kuykendahl yes yam yell yellow yogurt yale gym my cycle baby hairy sky Lynn Yvonne Mary Betty Elly Bryan Maloney Murray Wyatt'));
//check_text('Sylvia');
echo '</pre>';
 
function check_text($text) {
   
   // Make sure we have text
   if(empty($text)) {
     
      // Kick back
      return false;
     
   }
   
   // Break text into words
   $text_array = explode(' ', $text);
   
   // Make an array to return info
   $return_array = array();
   
   // Loop Text
   for($i=0; $i<count($text_array); $i++) {
     
      // Add to the return array
      $return_array[] = check_y($text_array[$i]);
     
   }
   
   // Return the array
   return $return_array;
   
}
 
?>
done
 
  1. start
  2. <?php
  3.  
  4. function check_y($word) {
  5.    
  6.    // Make an array to return info
  7.    $return_array = array();
  8.    
  9.    # Credits: Sergio Abreu
  10.    # Matches y followed by vowel in any place...
  11.    
  12.    if( preg_match( "/y[aeiou]/i", $word)){
  13.       $return_array[$word] = 'consonant';
  14.    } elseif( preg_match( "/[b-z]y/i", $word) && ! preg_match( "/[aeiou]y/i", $word)){
  15.       $return_array[$word] = 'vowel';
  16.    }
  17.    
  18.    # To restrict y being the first letter, use ^ :
  19.    
  20.    if( preg_match( "/^y[aeiou]/i", $word)){
  21.       $return_array[$word] = 'consonant';
  22.    } elseif( preg_match( "/[b-z]y/i", $word) && ! preg_match( "/[aeiou]y/i", $word)){
  23.      
  24.       $return_array[$word] = 'vowel';
  25.    }
  26.      
  27.    // Return the return array
  28.    return $return_array;
  29.    
  30. }
  31.  
  32. echo '<pre>';
  33. var_dump(check_text('Sydney Billy Sylvia Missy Kyle Blythe Sylvester Katy Kay Yeltsin May Kuykendahl yes yam yell yellow yogurt yale gym my cycle baby hairy sky Lynn Yvonne Mary Betty Elly Bryan Maloney Murray Wyatt'));
  34. //check_text('Sylvia');
  35. echo '</pre>';
  36.  
  37. function check_text($text) {
  38.    
  39.    // Make sure we have text
  40.    if(empty($text)) {
  41.      
  42.       // Kick back
  43.       return false;
  44.      
  45.    }
  46.    
  47.    // Break text into words
  48.    $text_array = explode(' ', $text);
  49.    
  50.    // Make an array to return info
  51.    $return_array = array();
  52.    
  53.    // Loop Text
  54.    for($i=0; $i<count($text_array); $i++) {
  55.      
  56.       // Add to the return array
  57.       $return_array[] = check_y($text_array[$i]);
  58.      
  59.    }
  60.    
  61.    // Return the array
  62.    return $return_array;
  63.    
  64. }
  65.  
  66. ?>
  67. done
  68.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 33 posts
  • Users browsing this forum: No registered users and 88 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.