Link to home
Start Free TrialLog in
Avatar of eadam-uk
eadam-ukFlag for United States of America

asked on

How can I search a list of phrases for specific terms?

Hi,

I have written a small database program using access and VB to store all the movies I have along with their location, format, director, cast etc. I also have a text file full of names of movies and I want to be able to search through this list for a specific set of words or a word. Basically, I have set my program to automatically enter the file name of a movie when I select it, but the name often contains lots of other words and characters that aren't relevant, so i want the program to compare each word in the file name with the names of the movies in my text file and suggest possibilities.

For example, If the file name is Pulp_Fiction xvd hjkhj stero.avi, it should suggest Pulp Fiction

I hope I have explained well enough. Please ask if I haven't been clear.

Thanks.
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Not quite sure what you're trying to do. But you could try something like:

Option Explicit

Private Sub Command1_Click()

    Dim fso As New Scripting.FileSystemObject
    Dim fol As Folder
    Dim fil As File
   
    Set fol = fso.GetFolder("C:\My Documents\My Pictures")
   
    For Each fil In fol.Files
        If CheckFile(fil.Name) Then
            lstResults.AddItem fil.Name
        End If
    Next fil

End Sub

Private Function CheckFile(ByVal fileName As String) As Boolean

    CheckFile = True   '// Default
   
    Dim arrWords() As String
    Dim i As Integer
   
    If Len(txtSearch.Text) > 0 Then
       
        arrWords = Split(txtSearch.Text, " ")
       
        If UBound(arrWords) = 0 Then
            ReDim arrWords(1)
            arrWords(0) = txtSearch.Text
        End If
   
        For i = 0 To UBound(arrWords) - 1
            If arrWords(i) <> "" Then
                If InStr(fileName, arrWords(i)) < 1 Then
                    CheckFile = False
                    Exit For
                End If
            End If
        Next i
       
    End If
   
End Function
Avatar of eadam-uk

ASKER

sorry, I guess I didn't explain that well.

What i mean is say I have a file name held in a string "pulp fiction xvid ac3.avi", I want the program to suggest "pulp fiction" to the user. I have a list of movie titles in a text file that I read in, but I need to compare them.

At the moment my code looks like this:

Public Function guessName(source As String) As String
Dim cnt As Long
Dim tmp As String
Dim tmpArray() As String

cnt = 0

Dim oFSO As New FileSystemObject
Dim oFSTR As Scripting.TextStream

ReDim tmpArray(2000)

     Set oFSTR = oFSO.OpenTextFile(App.Path & "\MovieList2.txt")

     Do While Not oFSTR.AtEndOfStream

            tmpArray(cnt) = oFSTR.ReadLine

            cnt = cnt + 1

            DoEvents
    Loop
     oFSTR.Close
     
   Dim i As Integer
   Dim tmpStr As String
   
   
   For i = LBound(tmpArray) To UBound(tmpArray)
    tmpStr = compareMovies(source, tmpArray(i))
    If (Not tmpStr = "") Then
        guessName = tmpStr
    End If
   Next i
     

End Function


Private Function compareMovies(source As String, target As String) As String

If (source = "" Or target = "") Then
    Exit Function
End If

Dim temp As String
Dim pos As Long

If (source = target) Then
    compareMovies = source
ElseIf (InStr(source, target)) Then
    compareMovies = target
End If


End Function

This works if the exact name that is in the text file is in the file name, but often it isn't. So I need a way of guessing what the name could be based on the filename.

For example.

If the file name is X-men.avi but my list of films contains "The X men" it won't find it.

Does that make more sense?

Thanks again,
You could perhaps have a comparison that looks for each word in the source name in the file name.
And counts the number of words that it gets a hit from.
Return those to the main function - along with the hit count, and present the user with that entire list (sorted by hit count).  OK - so you'd get a load from "The" - hence the need for sorting ;-)

Something like

Function compareMovies(source As String, target As String, intHits As Integer) As String

Dim temp As String
Dim intPos As Long
Dim intInstr As Integer

    If (source = "" Or target = "") Then
        Exit Function
    End If
   
    intPos = 1
    intInstr = InStr(intPos, source & " ", " ")
   
    Do Until intInstr = 0
        If InStr(target, Mid(source, intPos, intInstr - intPos)) > 0 Then
            intHits = intHits + 1
        End If
        intPos = intInstr + 1
        intInstr = InStr(intPos, source & " ", " ")
    Loop
   
    If intHits > 0 Then
        compareMovies = target
    End If
   
End Function
ASKER CERTIFIED SOLUTION
Avatar of TJenner2
TJenner2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TJenner2
TJenner2

Ok, I noticed I should have added the underscore to

strSeparaters = " @$%^&()!+=~`,.;_"

and failed to clarify that...

For Each Badword in List

was to presume that you know how to create a LIST and a variable BADWORDS set to that list.

There are some variations to this you could apply, but still you get the algorithm.

Regards,
TJ
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial