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

  • ScottG
  • Proficient
  • Proficient
  • No Avatar
  • Joined: Jul 06, 2010
  • Posts: 266
  • Status: Offline

Post December 21st, 2012, 8:20 pm

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
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post December 21st, 2012, 8:20 pm

  • ScottG
  • Proficient
  • Proficient
  • No Avatar
  • Joined: Jul 06, 2010
  • Posts: 266
  • Status: Offline

Post December 21st, 2012, 8:30 pm

This issue could be caused by the syllable class not splitting Yvonne up
  • ScottG
  • Proficient
  • Proficient
  • No Avatar
  • Joined: Jul 06, 2010
  • Posts: 266
  • Status: Offline

Post December 21st, 2012, 8:32 pm

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
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post December 21st, 2012, 11:12 pm

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.
http://www.schembrionics.com
The Ultimate Solutions Center
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post December 22nd, 2012, 12:33 am

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
http://www.schembrionics.com
The Ultimate Solutions Center
  • ScottG
  • Proficient
  • Proficient
  • No Avatar
  • Joined: Jul 06, 2010
  • Posts: 266
  • Status: Offline

Post December 22nd, 2012, 7:50 am

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
  • No Avatar
  • Joined: Jul 06, 2010
  • Posts: 266
  • Status: Offline

Post December 22nd, 2012, 8:04 am

also shouldn't yvette be considered a vowel since y makes an e sound?
  • ScottG
  • Proficient
  • Proficient
  • No Avatar
  • Joined: Jul 06, 2010
  • Posts: 266
  • Status: Offline

Post December 22nd, 2012, 8:32 am

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
  • No Avatar
  • Joined: Jul 06, 2010
  • Posts: 266
  • Status: Offline

Post December 22nd, 2012, 12:44 pm

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
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post December 22nd, 2012, 2:34 pm

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.
http://www.schembrionics.com
The Ultimate Solutions Center
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post December 22nd, 2012, 2:44 pm

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.
http://www.schembrionics.com
The Ultimate Solutions Center
  • ScottG
  • Proficient
  • Proficient
  • No Avatar
  • Joined: Jul 06, 2010
  • Posts: 266
  • Status: Offline

Post December 22nd, 2012, 3:08 pm

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
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post December 22nd, 2012, 10:21 pm

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.”
http://www.schembrionics.com
The Ultimate Solutions Center
  • alemao_brazil
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Feb 20, 2012
  • Posts: 8
  • Status: Offline

Post December 25th, 2012, 1:20 pm

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
  • No Avatar
  • Joined: Jul 06, 2010
  • Posts: 266
  • Status: Offline

Post December 25th, 2012, 2:25 pm

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post December 25th, 2012, 2:25 pm

Post Information

  • Total Posts in this topic: 33 posts
  • Users browsing this forum: No registered users and 124 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.