472,354 Members | 2,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

[STRING] extract a word and text around it

teo
hallo,

I need to extract a word and few text that
precedes and follows it (about 30 + 30 chars)
from a long textual document.

Like the description that Google returns when
it has found a given word.

In example from:

"Sylvia Brunner, a marine mammals researcher at the museum in Fairbanks,
identified the decomposing carcass and oversaw its recovery on Wednesday.
The "bloated, black thing on the beach" was about 12 feet from the river's
edge, she said."

I have to find the 'carcass' word
and finally return with:
"identified the decomposing carcass and oversaw its recovery on"

---

Which is the *fast* method in VbNet?

In VB6 I would have used
InStr (with Binary option because faster)
to find the position of the word,
then Mid to extract the preceding text,
then Mid to extract the following text,
then build up my phrase in this way: text1 & word & text2 .

Any suggestion in VBNet ?
New methods, StringBuilder, Regular Expression... or what else?

--------
Thanks
(examples are obviously very appreciated ;-) )

Jun 17 '06 #1
7 2775
teo,
| Which is the *fast* method in VbNet?
It sounds like you have the methods identified, you simply want someone else
to test them for you. Why not test them yourself, as you probably already
have the situation (program) and data to test them with.

I would probably use a regular expression, as regex feels like the "correct"
solution (not necessarily the fastest method). The trick is going to be
ensuring that it is an efficient expression and not a poorly performing
one... For example using a lazy compare instead of a greedy compare on the
30 before & after...

If I have time later I will see what RegEx I can come up with...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"teo" <te*@inwind.it> wrote in message
news:ue********************************@4ax.com...
| hallo,
|
| I need to extract a word and few text that
| precedes and follows it (about 30 + 30 chars)
| from a long textual document.
|
| Like the description that Google returns when
| it has found a given word.
|
| In example from:
|
| "Sylvia Brunner, a marine mammals researcher at the museum in Fairbanks,
| identified the decomposing carcass and oversaw its recovery on Wednesday.
| The "bloated, black thing on the beach" was about 12 feet from the river's
| edge, she said."
|
| I have to find the 'carcass' word
| and finally return with:
| "identified the decomposing carcass and oversaw its recovery on"
|
| ---
|
| Which is the *fast* method in VbNet?
|
| In VB6 I would have used
| InStr (with Binary option because faster)
| to find the position of the word,
| then Mid to extract the preceding text,
| then Mid to extract the following text,
| then build up my phrase in this way: text1 & word & text2 .
|
| Any suggestion in VBNet ?
| New methods, StringBuilder, Regular Expression... or what else?
|
| --------
|
|
| Thanks
| (examples are obviously very appreciated ;-) )
|
Jun 17 '06 #2
Use the same method as you would in VB6. Use the IndexOf method to find
the string and the Substring method to extract the part of the text.

However, I don't see the reason for getting the preceding text and
following text, just to put them together, when the string that you want
already exists in the text.

teo wrote:
hallo,

I need to extract a word and few text that
precedes and follows it (about 30 + 30 chars)
from a long textual document.

Like the description that Google returns when
it has found a given word.

In example from:

"Sylvia Brunner, a marine mammals researcher at the museum in Fairbanks,
identified the decomposing carcass and oversaw its recovery on Wednesday.
The "bloated, black thing on the beach" was about 12 feet from the river's
edge, she said."

I have to find the 'carcass' word
and finally return with:
"identified the decomposing carcass and oversaw its recovery on"

---

Which is the *fast* method in VbNet?

In VB6 I would have used
InStr (with Binary option because faster)
to find the position of the word,
then Mid to extract the preceding text,
then Mid to extract the following text,
then build up my phrase in this way: text1 & word & text2 .

Any suggestion in VBNet ?
New methods, StringBuilder, Regular Expression... or what else?

--------
Thanks
(examples are obviously very appreciated ;-) )

Jun 17 '06 #3
Teo,
Here is a regex:

Dim input As String = "Sylvia Brunner, a marine mammals researcher
at the museum in Fairbanks, identified the decomposing carcass and oversaw
its recovery on Wednesday. The ""bloated, black thing on the beach"" was
about 12 feet from the river's edge, she said."
Dim pattern As String = ".{1,30}?carcass.{1,30}"

Dim match As Match = Regex.Match(input, pattern,
RegexOptions.Multiline)

If match.Success Then
Debug.WriteLine(match.Value)
End If
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"teo" <te*@inwind.it> wrote in message
news:ue********************************@4ax.com...
| hallo,
|
| I need to extract a word and few text that
| precedes and follows it (about 30 + 30 chars)
| from a long textual document.
|
| Like the description that Google returns when
| it has found a given word.
|
| In example from:
|
| "Sylvia Brunner, a marine mammals researcher at the museum in Fairbanks,
| identified the decomposing carcass and oversaw its recovery on Wednesday.
| The "bloated, black thing on the beach" was about 12 feet from the river's
| edge, she said."
|
| I have to find the 'carcass' word
| and finally return with:
| "identified the decomposing carcass and oversaw its recovery on"
|
| ---
|
| Which is the *fast* method in VbNet?
|
| In VB6 I would have used
| InStr (with Binary option because faster)
| to find the position of the word,
| then Mid to extract the preceding text,
| then Mid to extract the following text,
| then build up my phrase in this way: text1 & word & text2 .
|
| Any suggestion in VBNet ?
| New methods, StringBuilder, Regular Expression... or what else?
|
| --------
|
|
| Thanks
| (examples are obviously very appreciated ;-) )
|
Jun 17 '06 #4
teo
On Sat, 17 Jun 2006 23:12:22 +0200, Göran Andersson <gu***@guffa.com>
wrote:
Use the same method as you would in VB6. Use the IndexOf method to find
the string and the Substring method to extract the part of the text.

However, I don't see the reason for getting the preceding text and
following text, just to put them together, when the string that you want
already exists in the text.


The fact is that I'm building a searching engine, and I need to
format the searched word as Bold,
so I'm compelled to have two chunk of text ,
so I can format my final string as this:
plain Text1 + Bold word + plain Text2.

Because I have to extract the integral text from a column of a DB
(then extract only a part of it, as described above),
do you know if SQL syntax is able to perform such extraction?

Or I'm compelled to extract the string using the VB methods
after having stored the integral text in a DataReader?

Jun 17 '06 #5
teo
Thanks;
I didn't want you to do the work for me indeed,
I only liked to know the name of the functions
it is advisable to use for this case...

Teo,
Here is a regex:

Dim input As String = "Sylvia Brunner, a marine mammals researcher
at the museum in Fairbanks, identified the decomposing carcass and oversaw
its recovery on Wednesday. The ""bloated, black thing on the beach"" was
about 12 feet from the river's edge, she said."
Dim pattern As String = ".{1,30}?carcass.{1,30}"

Dim match As Match = Regex.Match(input, pattern,
RegexOptions.Multiline)

If match.Success Then
Debug.WriteLine(match.Value)
End If


Jun 18 '06 #6
| The fact is that I'm building a searching engine, and I need to
| format the searched word as Bold,
Rather then search for the text each time, have you considered, "indexing"
each document.

Then when you need to do a search, you simply check the index, the index
would return where in the text the word was found.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"teo" <te*@inwind.it> wrote in message
news:su********************************@4ax.com...
| On Sat, 17 Jun 2006 23:12:22 +0200, Göran Andersson <gu***@guffa.com>
| wrote:
|
| >Use the same method as you would in VB6. Use the IndexOf method to find
| >the string and the Substring method to extract the part of the text.
| >
| >However, I don't see the reason for getting the preceding text and
| >following text, just to put them together, when the string that you want
| >already exists in the text.
|
| The fact is that I'm building a searching engine, and I need to
| format the searched word as Bold,
| so I'm compelled to have two chunk of text ,
| so I can format my final string as this:
| plain Text1 + Bold word + plain Text2.
|
| Because I have to extract the integral text from a column of a DB
| (then extract only a part of it, as described above),
| do you know if SQL syntax is able to perform such extraction?
|
| Or I'm compelled to extract the string using the VB methods
| after having stored the integral text in a DataReader?
|
Jun 18 '06 #7
teo
On Sat, 17 Jun 2006 22:42:07 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************@tsbradley.net> wrote:
| The fact is that I'm building a searching engine, and I need to
| format the searched word as Bold,
Rather then search for the text each time, have you considered, "indexing"
each document.

Then when you need to do a search, you simply check the index, the index
would return where in the text the word was found.


I know that there is such an option,
but I didn't think about it about a solution
because it seemed to me that it would have required
a lot of job to do firstly ;
also going to search a given word among the resulting huge list of
indexed words,
I think it would require a lot of time, maybe the same time than
it would require searching for the given every time.
I'm only guessing about, I've no benchmark....

Maybe I'm going to implement such a solution
when I've finished this method I've started to develop now.

----------

Another question:

the RegExp sample we discuss above
returns 30 + 30 , regardless how the 30 on the left start.
I'll try to explain what I mean.

I'd like to have the chunk of text on the left
starting where the sentence containing the given word starts,
(so to have the very first letter capitalized),
like the way Google displays the results,
that is, if you search 'Lewinsky'
Goggle returns with:

To maintain the *Lewinsky* Story's original feel, we will leave much
of this ... These were gifts the president had originally given
to Ms. Lewinsky himself. ...

In this way,
the 'T' letter is at # -16 position,
I renounce to the preceding 14 chars
and decide to start straight at # - 16
and decide to increase the chunk of text on the
right to 44 ( = 30 + 14).

If the "T" isn't within the 30 chars on left,
no problem, I accept the old 30+30 solution.

Is it possible?

-----

Basically,
we need to trace of the . (= dot) char
that signals to us that a sentence (within to 30 left) is going to start.

If a single RegExp doesn't workt,
we could maybe go with doubling the first RegExp (60 +60)
and then
with a second RegExp find the dot char
and then simply extract the following 60 chars chunk of
text on the right.

What about this?



Jun 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nel | last post by:
Hi all, Before I re-invent the wheel here, has anyone willing to share a basic script to extract META keywords from a string. I have a string,...
8
by: John van Terheijden | last post by:
Hi. I'm trying to make a conversion algorithm that colors even and odd words in a HTML string with <div> tags. // input text with all sorts of...
1
by: Sheela | last post by:
Hi all gurus in tha club, I scripted a prog that extract a string from an html page excluding all the tags. The problem is that it works quite...
1
by: cassandra.flowers | last post by:
Hi, I am using VB6 and want to extract text from a string. But ONLY take out words that begin with 't' or 'd'. The mainstring is input by the...
3
by: Richard L Rosenheim | last post by:
I have some text where I need to extract some pieces from. The text will be in a format like this: a string description color="red"...
8
by: yerk5 | last post by:
Does js have some way of doing what I want here: I want to paste a multiline block of text into a TEXTAREA form field, and there is data in the...
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based //...
1
by: Edwin.Madari | last post by:
from each line separate out url and request parts. split the request into key-value pairs, use urllib to unquote key-value pairs......as show...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.