Solved

Retrive words from a string and compare

Posted on 2003-11-28
7
217 Views
Last Modified: 2012-05-07
FILENAME       KEYWORD
xxx.doc      abc,caa,funny
xyz.txt      abc,cba,funnys
sdfetll.txt      ball,basketball,players,court,
d.doc      bryan,name,hobbies,male,boy

above is a datasets, fields are filename,keyword. store in a ms access table. both filename and keyword fields are string.   i.e.   'xxx.doc'  is a string,  'abc,caa,funny' is a string

if a user input "abc", the program should return filename associated with it , i.e.  xxx.doc and xyz.txt

since the datasets may be very large, the algorithim to retrive the keywords from keyword string and then compare in order to get the associated filename should be efficient.

anyone has the script or can provide some idea? like what data types., method i should use....etc

thank you for enlightening.

0
Comment
Question by:plovel
  • 4
  • 2
7 Comments
 
LVL 9

Expert Comment

by:mocarts
ID: 9837867
hi, plovel :)
just use SQL:
SELECT FILENAME FROM TABLE1 WHERE ','+KEYWORD+',' LIKE '%,abc,%'

delphi example:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ADOQuery1.SQL.Text := 'SELECT FILENAME FROM TABLE2 WHERE '',''+KEYWORD+'','' LIKE ''%,'
    + Edit1.Text + ',%''';
  ADOQuery1.Open;
  if ADOQuery1.RecordCount = 0 then
    ShowMessage('No result found!')
  else
    ShowMessage('FileName: '+ ADOQuery1.FieldByName('FILENAME').AsString);
  ADOQuery1.Close;
end;

wbr, mo.
0
 
LVL 9

Expert Comment

by:mocarts
ID: 9837893
caution - you must check for empty search string, otherwise you will get all records where KEYWORD field ends with comma.
0
 
LVL 3

Expert Comment

by:ahll
ID: 9838061
I agree with mocarts, but you have to include in the KEYWORD field a comma at the beginind and at the end. If not, you will lose the first and the last word.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Accepted Solution

by:
mocarts earned 150 total points
ID: 9838129
not actually - that can be overcome in SQL by "quoting" keyword field in commas - what is actually done in my example.
SELECT FILENAME FROM TABLE1 WHERE ','+KEYWORD+',' LIKE '%,abc,%'

also comment to my example - plovel - you should additionally implement loop (while not eof) for retrieving all selected records (filenames):
var
  sFiles: string;
begin
...
 ADOQuery1.Open;
  if ADOQuery1.RecordCount = 0 then
    ShowMessage('No result found!')
  else begin
    while not ADOQuery1.eof do begin
      sFIles := sFiles + ',' + ADOQuery1.FieldByName('FILENAME').AsString;
      ADOQuery1.Next;
    end;
    if sFiles <> '' then Delete(sFiles, 1);
    ShowMessage(sFiles);
  end;
...
0
 
LVL 1

Expert Comment

by:cerdal
ID: 9887682
You'd have much faster access if you split the list up and inserted one record for each keyword. That way you could just do a straight "select filename from Table1 where keyword = :MyKeyWord".

If the table has other fields in it, which would mean too many duplicates, add an AutoIncrement field to Table1 and fill a second table with KeyWord + Tbl1AutoIncValue in Table1's AfterPost event. You could then do:

Select TABLENAME from TABLE1, TABLE2
Where TABLE2.KEYWORD = :MyKeyWord
And TABLE1.AutoIncValue = TABLE2.Tbl1AutoIncValue

If you need more detail, just ask.

Chris.
0
 
LVL 9

Expert Comment

by:mocarts
ID: 9965421
thanks, plovel :)
small comment on Chris comment..
that will be only a little bit faster, but will take more room to store all data. and if to be truly formal then there must be three tables - one for keywords, one for filenames and last one to link previous tables i.e.
Kyeword table (KT): IDKeyword, Keyword
Filename table (FT): IDFile, FileName
Link table (LT): IDKeyword, IDFile
and select would be SELECT FT.FileName FROM FT, KT, LT WHERE FT.IDFile=LT.IDFile AND KT.IDKeyword=LT.IDKeyword AND KT.Keyword = 'KeywordToSearch'

wbr, mo.
0
 
LVL 1

Expert Comment

by:cerdal
ID: 9967993
reply to mo:

pragmatism is always best :-) If it's good enough, do it that way.

it's another case of horses for courses - I've never worked with Access, but some databases can be orders of magnitude slower matching parts of a field (even if it's indexed) in every record from start to finish rather than going straight to the only ones whose keyfield matches what's needed exactly.

It might be an idea for plovel to compare the 2 methods and let us know how much faster it actually is in his situation...

As for the extent of normalising that's required, I agree it's pretty rare to find a case where 100% is optimum.

Chris.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now