Hi,
Ok, let me see if I can explain myself :)
Basically, if we have a string like;
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
...we them split this up into sections, like:
Lorem Ipsum is simply dummy text of the printing and typesetting indus
+try. Lorem Ipsum
has been the industry's standard dummy text ever since
the 1500s, when an unknown printer took a galley of type and scrambled
+ it to make a type
specimen book. It has survived not only five centuries, but also the l
+eap into electronic
typesetting, remaining essentially unchanged. It was popularised in th
+e 1960s with the
release of Letraset sheets containing Lorem Ipsum passages, and more r
+ecently with desktop
publishing software like Aldus PageMaker including versions of Lorem I
+psum.
Now, for some of those phrases, we wanna remove anything before any of these charachters:
! , . : and )
For example, this:
typesetting, remaining essentially unchanged. It was popularised in the 1960s with the
..would become:
remaining essentially unchanged. It was popularised in the 1960s with the
Now, I've got a function that does this:
sub cleanup_start_words {
my $text = $_[0];
my @split = split //, $text;
my @keywords = split //, $_[1];
return $text;
# see if we have any charachters we wanna skip in the first 10 cha
+rachters
my $remove_at;
my $do_remove = 0;
for (my $x = 0; $x < 40; $x++) {
if ($split[$x] =~ /[\.\!\?,\)\:\:]/) {
$do_remove = 1;
$remove_at = $x;
}
}
if ($do_remove) {
my $i = 0;
foreach (@split) {
$i++;
if ($i > $remove_at) { last; }
if (m/[\.\!\?,]\)\:/) {
# print "skipping [last] $_ \n";
last;
} else {
# print "skipping $_ \n";
}
}
# didn't seem to work right when doing it in the foreach above, s
+o get rid of the
# charachters we dont want here
for (my $ii = 0; $ii < $i; $ii++) {
shift @split;
}
my $tmp = join("",@split);
$tmp =~ s/^[\.\!\?,\)\:]//;
$tmp =~ s/^\s+//;
return $tmp;
} else {
return $text;
}
}
..but I'm wondering if there is maybe a regex we could use, which would be more effecient?
I'm trying to knock of crutial miliseconds from this, cos the new feature has added about .1 of a second to each request (it doesn't only consist of the above code - there is a lot else going on =))
Anyone got any suggestions? Please note, this is a non-english site, so will need to work with accented charachters etc.
TIA!
Andy
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.