Breaking A Sentence into Words

A

Agus Budianto

Dear All,

I have a text field in Table1.
If for example, the field contain "Mary had a little lamb", I want to have
Word1 = Mary
Word2 = had
Word3 = a
Word4 = little
Word5 = lamb
I am going to put every word as a single record in another table.

Anyone can help?

Rgds,
Agus Budianto
 
D

Douglas J. Steele

Assuming you're using Access 2000 or newer,

Dim varWords As Variant

varWords = Split("Mary had a little lamb", " ")

will create an array where varWords(0) will be Mary, varWords(1) will be
had, and so on to varWords(4) being lamb.

You'll need to use VBA code to open a recordset based on Table1, parse the
text field for each row in that recordset and create the new rows in your
other table.
 
D

David Lloyd

One option would be to use the Split function and specify a space " " as the
delimiter.

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


Dear All,

I have a text field in Table1.
If for example, the field contain "Mary had a little lamb", I want to have
Word1 = Mary
Word2 = had
Word3 = a
Word4 = little
Word5 = lamb
I am going to put every word as a single record in another table.

Anyone can help?

Rgds,
Agus Budianto
 
J

John Nurick

Hi Agus,

One complication here is that it's not always easy to define a "word".
For instance, is "Mary-Ellen" one word or two?

I've pasted two VBA functions at the end of this message. One assumes
that "words" are separated by spaces; the other assumes that "words" are
substrings that contain only alphanumeric characters, underscores and
hyphens.

You can use either function in a calculated field in a query. E.g.

Word: ParseWords([MyField], 0)

will return the first word in MyField. The thing to do is to build an
append query that creates a record in your other table for the first
word of each record in Table1. After running it, modify it so it gets
the second word and run it again. And so on.

It doesn't matter if some records in Table1 contain more words than
others: the functions just return Null if you specify a number greater
than the actual number of words in the field.

Dear All,

I have a text field in Table1.
If for example, the field contain "Mary had a little lamb", I want to have
Word1 = Mary
Word2 = had
Word3 = a
Word4 = little
Word5 = lamb
I am going to put every word as a single record in another table.

Anyone can help?

Rgds,
Agus Budianto

'START OF CODE =============================================

Public Function ParseItems( _
List As Variant, _
Item As Long, _
Optional Separator As String = " " _
) As Variant

'Returns the specified item from a list of "words" separated
'by a space (or by Separator).
'Counting starts at zero.
'Returns Null if the list is too short.

Dim arWords As Variant

If IsNull(List) Then
ParseItems = Null
Exit Function
End If

arWords = Split(CStr(List), " ", Item + 2)
If UBound(arWords) < Item Then
ParseItems = Null
Else
ParseItems = arWords(Item)
End If
End Function


Public Function ParseWords( _
List As Variant, _
Item As Long) As Variant

'Returns the specified "word" from a string.
'Counting starts at 0.
'A "word" is a substring consisting only of alphanumeric characters,
'hyphens and underscores.
'By John Nurick, 2005

Dim oRE As Object 'VBScript_RegExp_55.RegExp
Dim oMatches As Object 'VBScript_RegExp_55.MatchCollection

If IsNull(List) Then
ParseWords = Null
Exit Function
End If

Set oRE = CreateObject("VBScript.Regexp")
With oRE
.Pattern = "\b[-0-9A-Z_]+\b"
.Global = True
.IgnoreCase = True
.Multiline = True
End With

Set oMatches = oRE.Execute(CStr(List))
If oMatches.Count <= Item Then
ParseWords = Null
Else
ParseWords = oMatches(Item)
End If

Set oRE = Nothing
End Function

'END OF CODE ===============================
 
A

Agus Budianto

Thanks Douglas,

The next question is: How do I know how many words I will have?

Rgds,
Agus Budianto
 
D

Douglas J. Steele

UBound(varWords) will give you the number of the last word in the array.
Since the first element in the array is 0, you'll have a total of
UBound(varWords) + 1 words.
 

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

Top