AlgorithmNeeded

  • Thread starter Thread starter Aamir Mahmood
  • Start date Start date
A

Aamir Mahmood

Hi,
I am looking for a routine (if someone has already written it), which can
convert the following string

"ILoveMyPCButIDon'tLoveWindows2000"

To

"I Love My PC But I Don't Love Windows 2000"

Didn't want to spend time if someone has already implemented it and wants to
share it.

If I don't find it I will write it.

Thanks.
 
So you want to determine where every word starts and pre-pend each word with
a space?

In your sample there is no clear way to separate words from each other.
You've got one acronym (PC), so the task is already quite complex. I regret
to inform you that in order to achieve this, you'll have to come up with
some artificially intelligent piece of software.
 
* Aamir Mahmood wrote, On 2-6-2007 0:23:
Hi,
I am looking for a routine (if someone has already written it), which can
convert the following string

"ILoveMyPCButIDon'tLoveWindows2000"

To

"I Love My PC But I Don't Love Windows 2000"

Didn't want to spend time if someone has already implemented it and wants to
share it.

If I don't find it I will write it.

Thanks.


A regex should do the trick. But it's quite hard as ILove should be
split into I Love, but PCBut shouldn't be split into P C But. The
following expression comes pretty close:

"(?:(?<tag>[a-z])(?=[A-Z0-9])|(?<tag>[A-Z]*[A-Z])(?![a-z]))"

Just use Regex.Replace with the following replacement pattern:

"${tag} "

There might be a few examples which won't work, but most should.

Regex.Replace(
input,
"(?:(?<tag>[a-z])(?=[A-Z0-9])|(?<tag>[A-Z]*[A-Z])(?![a-z]))",
"${tag} ",
RegexOptions.None
);

Jesse
 
There is of course a dumb way to determine where an acronym is.

If you loop through the characters and then find a capital letter => A new
word starts.
While ( letter is capital && next_letter is capital too ) { add letter to
acronym }

There's a risk of having two adjacent acronyms - in this case, tough luck...



using System;
using System.Collections.Generic;
using System.Text;

namespace parsing
{
class Program
{
static void Main( string[] args )
{
string strOriginal = "ILoveMyPCButIDon'tLoveWindows2000";
string strResult = "";
bool bReadingNumber = false;
bool bReadingAcronym = false;

for( int i = 0; i < strOriginal.Length; i++ )
{
if( strOriginal >= 'A' && strOriginal <= 'Z' )
{
if( i > 0 )
{
if( i + 1 < strOriginal.Length &&
( strOriginal[i + 1] >= 'A' && strOriginal[i +
1] <= 'Z' ) )
{
if( !bReadingAcronym )
{
bReadingAcronym = true;
strResult += ' ';
}
}
else
{
bReadingAcronym = false;
strResult += ' ';
}
}

bReadingNumber = false;
}

else if( strOriginal >= '0' && strOriginal <= '9' )
{
if( !bReadingNumber )
{
if( i > 0 )
strResult += ' ';

bReadingNumber = true;
}
}

else
bReadingNumber = false;

strResult += strOriginal;
}
}
}
}
 
If you noticed, every word is starting with a capital letter.
So even if multiple caps come along the last one should be the beginning of
the next word. All the previous ones should either make up an acronym or a
word itself (as in "ILove").
 
Nice, I haven't thought of regex's...

Can you come up with an internationalized one? :)

Jesse Houwing said:
* Aamir Mahmood wrote, On 2-6-2007 0:23:
Hi,
I am looking for a routine (if someone has already written it), which can
convert the following string

"ILoveMyPCButIDon'tLoveWindows2000"

To

"I Love My PC But I Don't Love Windows 2000"

Didn't want to spend time if someone has already implemented it and wants
to share it.

If I don't find it I will write it.

Thanks.


A regex should do the trick. But it's quite hard as ILove should be split
into I Love, but PCBut shouldn't be split into P C But. The following
expression comes pretty close:

"(?:(?<tag>[a-z])(?=[A-Z0-9])|(?<tag>[A-Z]*[A-Z])(?![a-z]))"

Just use Regex.Replace with the following replacement pattern:

"${tag} "

There might be a few examples which won't work, but most should.

Regex.Replace(
input,
"(?:(?<tag>[a-z])(?=[A-Z0-9])|(?<tag>[A-Z]*[A-Z])(?![a-z]))",
"${tag} ",
RegexOptions.None
);

Jesse
 
* Ashot Geodakov wrote, On 2-6-2007 1:36:
Nice, I haven't thought of regex's...

Can you come up with an internationalized one? :)

Replace [A-Z] with \p{Lu}
Replace [a-z] with \p{Ll}
Replace [0-9] with \p{N}

And you should be well under way. This sure as hell isn't going to cover
all possibilities (like McDonalds and O'Niell in names) but the previous
example didn't work well on those either.

Jesse

Jesse Houwing said:
* Aamir Mahmood wrote, On 2-6-2007 0:23:
Hi,
I am looking for a routine (if someone has already written it), which can
convert the following string

"ILoveMyPCButIDon'tLoveWindows2000"

To

"I Love My PC But I Don't Love Windows 2000"

Didn't want to spend time if someone has already implemented it and wants
to share it.

If I don't find it I will write it.

Thanks.

A regex should do the trick. But it's quite hard as ILove should be split
into I Love, but PCBut shouldn't be split into P C But. The following
expression comes pretty close:

"(?:(?<tag>[a-z])(?=[A-Z0-9])|(?<tag>[A-Z]*[A-Z])(?![a-z]))"

Just use Regex.Replace with the following replacement pattern:

"${tag} "

There might be a few examples which won't work, but most should.

Regex.Replace(
input,
"(?:(?<tag>[a-z])(?=[A-Z0-9])|(?<tag>[A-Z]*[A-Z])(?![a-z]))",
"${tag} ",
RegexOptions.None
);

Jesse
 
Ashot Geodakov said:
So you want to determine where every word starts and pre-pend each word
with a space?

In your sample there is no clear way to separate words from each other.
You've got one acronym (PC), so the task is already quite complex. I
regret to inform you that in order to achieve this, you'll have to come up
with some artificially intelligent piece of software.

I'm curious. Why do you have a string like this? Are you creating this
string for some reason? I'm sure if we get a good understanding of why your
data is like this, we may be able to help you further in getting around it,
otherwise, the previous replies are similar if not exactly what you'll have
to do...

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top