I had a quick look at the code out of interest. The problem occurs in the str_normalize function. The code is buggy and it does not check boundary conditions properly.
Below is the line of the code that caused the problem.
if(isalpha(*(s-1)) && strchr(chrsep, *s) && isalpha(*(s+1))) {
if(space_words(s, *s)) {
char c = *s;
while(*s) {
if(*s == c) s++;
else if(!isalpha(*s)) break;
*p = *s;
s++;
p++;
}
}
}
The code detects a possible space character, and tries to validate it. But the single character 'I' and the spaces surrounding it caused logical malfunction in the space_words function. Thus <space> is not recognized as separator for the words.
My advise is to ditch this module, and use simple Perl implementation. The single line below does pretty much what the ExtractWords.xs code does in hundred lines of C.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @strings = (
q|one two 'three' one|,
q|yes i am 'sme'|,
q|x21 y z|,
);
for my $str (@strings){
my %hash;
for ( map { length >= 3 ? $_ : () } $str =~ /(\w+)/g ) {
$hash{$_}++;
}
print Dumper(\%hash);
}
And the output is
$VAR1 = {
'three' => 1,
'one' => 2,
'two' => 1
};
$VAR1 = {
'yes' => 1,
'sme' => 1
};
$VAR1 = {
'x21' => 1
};
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.