2

I am looking for a way to find the number of sentence occurrences in another sentence

For example (I have):

Do you have a different language or operating system? JavaScript is currently disabled in your browser and is required to

and I am searching for:

a

This should present me the result of:

result = 1

because if you count a as a word and not as a char you will get 1 as result:

"Do you have a different language or operating system? JavaScript is currently disabled in your browser and is required to" Din.

Charles
  • 50,943
  • 13
  • 104
  • 142
dinbrca
  • 1,101
  • 1
  • 14
  • 30
  • 1
    i thin you are looking for [substr_count()](http://php.net/manual/en/function.substr-count.php) –  Mar 03 '13 at 21:31

3 Answers3

1

Indeed use substr_count.

To make sure you only match words: add a space before and after the word before using substr_count and explicitly check for the word at the start or end of the string using substr.

Coert Metz
  • 894
  • 6
  • 21
  • Or add a space to the start and end of a word, like `" " . $string . " "`, which would then allow you to count `" a "` with one call to `substr_count()`. – Cat Mar 03 '13 at 21:36
  • That is exactly what I meant Eric, but you then miss the 'a ' at the start and ' a' at the end of the string, so therefore do two additional `substr` checks. – Coert Metz Mar 03 '13 at 21:38
  • You can explicitly check that case as well. With regular expressions you can match an optional dot. – Coert Metz Mar 03 '13 at 21:42
  • @dinbrca That's exactly what Coert and I are talking about: Putting a space on either side of the string to search will allow that. See: http://codepad.org/fORXYur3 – Cat Mar 03 '13 at 21:42
  • @Eric but what if you have "a potato is a.." how would u count the last a? ("a..") – dinbrca Mar 04 '13 at 14:45
  • `str_replace(array('.', '?', '"', /* etc */), ' ', $string);` first. – Cat Mar 04 '13 at 14:46
1

As far as counting the number of times the WORD "a" is used in a string quickly/simply:

$sent = "Do you have a different language or operating system? JavaScript is currently disabled in your browser and is required to";

if( preg_match( '/ a /', $sent, $matches ) ) { # a space before and after makes it a word not a letter.
    echo count( $matches );            
}

But that still won't tell you how many sentences there are for sure in all cases; to do that would require a pretty complicated regex.

--> EDIT:

To get the word "a" at the beginning of a sentence and anywhere else, you could do this:

$sent = "A different language or operating system? JavaScript is currently disabled in your browser and is required to eat a walrus";

$patterns = array( '/ a /', '/A /' );
$ctr = 0;

foreach( $patterns as $p ) {

    if( preg_match( $p, $sent, $matches ) ) {
        $ctr += count( $matches );             
    }

}

echo $ctr;  
BIT CHEETAH
  • 1,200
  • 1
  • 7
  • 10
  • The OP asked for it without regex, if possible (and it *is* possible). – Cat Mar 03 '13 at 21:42
  • He also is "looking for a way to find the number of sentence occurrences in another sentence" which _may not_ be possible without regex. @Eric – BIT CHEETAH Mar 03 '13 at 21:52
  • That is possible without regex as well. – Cat Mar 03 '13 at 21:55
  • How would you do it without regex with a sentence like this: 'Mr. Ree gasped, "My God! Where is my cheese!?" and then watched TV.' @Eric – BIT CHEETAH Mar 03 '13 at 22:00
  • If he doesn't care what the word boundary is, you can do it with a simple `substr_count()`, again. Even if this is the case, though, the OP never specified that he would need a word boundary other than " ". – Cat Mar 03 '13 at 22:05
0

Not the most efficient solution but seems to give you what you need.

$search = 'a';
$str = "Do you have a different language or operating system? JavaScript is currently disabled in your browser and is required to";
$count = array_sum(array_map(function($val) use ($search) {if ($val == $search) { return 1; } else { return 0; }}, str_word_count($str, 1)));
GeoffK
  • 70
  • 6
  • See docs for [str_word_count](http://php.net/manual/en/function.str-word-count.php). Note: this could get pretty gnarly with large chunks of text – GeoffK Mar 03 '13 at 21:45
  • seems like Coert Metz solution can be better, am I wrong? – dinbrca Mar 04 '13 at 14:46
  • It's an altogether better solution probably if speed of execution is a concern. Still - they both do the trick :) You could replace the array_map in my solution with a call to [array_count_values](http://php.net/manual/en/function.array-count-values.php) which would probably make it LOOK a little less complicated but ultimately amounts to the same thing. – GeoffK Mar 04 '13 at 17:47