Rem Type=Plugin
Rem Name=Dreaming Picture Thoughts
Rem Author=cyberjedi
Rem Host=Assistant
'This sub setups the plug-ins option panel in Hal's options dialog
Sub OptionsPanel()
'------------------
' Define the Help
'------------------
lblPlugin(0).Caption = "Alice will show you pictures of it's thoughts in dreams. Say: INDEX IMAGE to index the image folder. Supports JPG, PNG, And GIF"
lblPlugin(0).Move 120, 120, 3300, 1000
lblPlugin(0).WordWrap = True
lblPlugin(0).Visible = True
cmdPlugin(0).Move 320, 650, 2100, 375
cmdPlugin(0).Caption = "Help on this plugin"
cmdPlugin(0).Visible = True
'------------------
' Define the Picture Mood listbox
'------------------
lblPlugin(1).Caption = "Alice Picture Mood level"
lblPlugin(1).Move 320, 1300, 4000, 1000
lblPlugin(1).WordWrap = True
lblPlugin(1).Visible = True
cboPlugin(0).Move 320, 1600, 1800
cboPlugin(0).AddItem "Very high"
cboPlugin(0).AddItem "High"
cboPlugin(0).AddItem "Average"
cboPlugin(0).AddItem "Low"
cboPlugin(0).AddItem "Minimal"
cboPlugin(0).Visible = True
cmdPlugin(1).Caption = "Apply Changes"
cmdPlugin(1).Move 320, 2100, 1600, 375
cmdPlugin(1).Visible = True
'------------------
' Update the listbox with the current value stored in the table
'------------------
If HalBrain.CheckTableExistence("pvrParameter") = True Then
pvrPictureMood = HalBrain.TopicSearch("PICTURE", "pvrParameter")
If pvrPictureMood = "" Then pvrPictureMood = 1
Select Case pvrPictureMood
Case 100
cboPlugin(0).Text = "Very high"
Case 75
cboPlugin(0).Text = "High"
Case 50
cboPlugin(0).Text = "Average"
Case 25
cboPlugin(0).Text = "Low"
Case 10
cboPlugin(0).Text = "Minimal"
End Select
End If
End Sub
Sub cmdPlugin_Click(Index)
Dim pvrTempQuery()
Dim pvrPicture, pvrPictureMood
Select Case Index
Case 0
HalMenu.HalCommand "<RUNPROG>pvrJpgHelp.html</RUNPROG>"
Case 1
'------------------
' Convert the listbox in a value
'------------------
Select Case cboPlugin(0).Text
Case "Very high"
pvrPictureMood = 100
Case "High"
pvrPictureMood = 75
Case "Average"
pvrPictureMood = 50
Case "Low"
pvrPictureMood = 25
Case "Minimal"
pvrPictureMood = 10
End Select
'------------------
' If the Parameter table does not exist, I create it
'------------------
HalBrain.ReadOnlyMode = False
pvrTableName = "pvrParameter"
If HalBrain.CheckTableExistence(pvrTableName) = False Then
'Create table if it doesn't exist
HalBrain.CreateTable pvrTableName, "TopicSearch", "miscData"
End If
'------------------
' Search if it contains the PictureMood parameter
'------------------
pvrPicture = "PICTURE"
If HalBrain.TopicSearch(pvrPicture, pvrTableName) = "" Then
'------------------
'Creates the record storing the new value
'------------------
HalBrain.AddToTable pvrTableName, "TopicSearch", pvrPicture, pvrPictureMood
Else
'------------------
'Updates the record storing the new value
'------------------
HalBrain.RunQuery "UPDATE " & pvrTableName & " SET topic = " & pvrPictureMood & " WHERE searchString = " & """" & pvrPicture & """", pvrTempQuery
End If
End Select
End Sub
Sub OptionsPanel()
lblPlugin(0).Caption = "Enables talking dream mode."
lblPlugin(0).Move 130, 180, 3400, 1200
lblPlugin(0).WordWrap = True
lblPlugin(0).Visible = True
End Sub
Rem PLUGIN: SCRIPT_LOAD
Script_Load = Script_Load & "<DREAMOUTLOUD>"
Rem PLUGIN: FUNCTIONS
'
'------------------
' Declare variables
'------------------
Dim PicLastIdleResponse, MyPicMediaDir, PicDefaultLoad, PicDudOut
Rem PLUGIN: PLUGINAREA7
'------------------
' Begin measuring pause time and capture userName
'------------------
PicLastIdleResponse = Now
PicDudOut = 0
Rem PLUGIN: PLUGINAREA1
If OriginalSentence <> "" Then
MyPicMediaDir = PicMediaDir()
PicDefaultLoad = MyPicMediaDir & "DefPic.jpg"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(PicDefaultLoad) Then
Set objWSHShell = CreateObject("WScript.Shell")
objWSHShell.Run """" & PicDefaultLoad & """"
End If
End If
Rem PLUGIN: PLUGINAREA5
'The comment above tells Ultra Hal Assistant to insert the following code
'on-the-fly into the main brain code in the section referenced.
'------------------
' Check if the user asked for reorganizing the Image table
'------------------
pvrJpg = False
If InStr(UserSentence, " INDEX ") <> 0 And InStr(UserSentence, " IMAGE ") <> 0 Then pvrJpg = True
If pvrJpg = True Then
'------------------
' If the Image table does not exist, I create it
'------------------
HalBrain.ReadOnlyMode = False
pvrTableName = "pvrJpg"
If HalBrain.CheckTableExistence(pvrTableName) = False Then
' Create table if it doesn't exist
' It must be of type Brain, to allow partial text search
HalBrain.CreateTable pvrTableName, "Brain", "miscData"
Else
' The 260 parameter is 256 + 4 = Yes/No with Default = No
pvrAnswer = MsgBox("Shall I delete the existing image index and recreate it from scratch?", 260, "Image Reindexing")
If pvrAnswer = 6 Then
HalBrain.RunQuery "DELETE FROM " & pvrTableName, pvrTempQuery
End If
End If
'------------------
' Ask which is the main Image folder
'------------------
pvrJpgFolder = InputBox("Which main folder to you want to reindex?", "Image Reindexing", "c:\pict")
'------------------
' Read the main Image folder
'------------------
Set pvrFso = CreateObject("Scripting.FileSystemObject")
If pvrFso.FolderExists(pvrJpgFolder) Then
GetResponse = "I have indexed all the Image files in the specified folder and subfolders. "
HalBrain.ReadOnlyMode = False
pvrCurrentFolder = pvrJpgFolder
Call pReadFolder(pvrCurrentFolder, pvrJpgFolder)
Else
GetResponse = "The folder " & pvrJpgFolder & " does not exist on your computer. "
End If
HalBrain.ReadOnlyMode = True
End If
'------------------
' From time to time, Hal will show a picture, according to the subject
' For example, you say: "I like cats"
' Hal might show a picture of a cat
'------------------
'------------------
' check the current image mood factor
'------------------
pvrPictureMood = HalBrain.TopicSearch("PICTURE", "pvrParameter")
If pvrPictureMood = "" Then pvrPictureMood = 0
pvrPictureMood = CInt(pvrPictureMood)
pvrKeywords = ""
If pvrPictureMood > 0 Then
pMediaintHighNumber = 110
pMediaintLowNumber = 1
Randomize
pMediaintNumber = Int((pMediaintHighNumber - pMediaintLowNumber + 1) * Rnd + pMediaintLowNumber)
If pMediaintNumber < pvrPictureMood Then
pvrNameToPlay = ""
If pvrNameToPlay = "" Then
pvrKeywords = WN.FindFirstNoun(UserSentence, True)
pvrKeywords = Trim(Ucase(pvrKeywords))
If pvrKeywords <> "" Then
pvrKeywords = " " & pvrKeywords & " "
pvrKeywords = Replace(pvrKeywords,", "," , ",1,-1,vbTextCompare)
pvrKeywords = Replace(pvrKeywords," ","xx",1,-1,vbTextCompare)
pvrKeywordList = Split(pvrKeywords, ",")
For i = Lbound(pvrKeywordList) To Ubound(pvrKeywordList)
pvrNameToPlay = HalBrain.QABrain(pvrKeywordList(i), "pvrJpg", 0)
Next
End If
End If
If pvrNameToPlay = "" Then
pvrKeywords = WN.FindFirstNoun(UserSentence, False)
pvrKeywords = Trim(Ucase(pvrKeywords))
If pvrKeywords <> "" Then
pvrKeywords = " " & pvrKeywords & " "
pvrKeywords = Replace(pvrKeywords,", "," , ",1,-1,vbTextCompare)
pvrKeywords = Replace(pvrKeywords," ","xx",1,-1,vbTextCompare)
pvrKeywordList = Split(pvrKeywords, ",")
For i = Lbound(pvrKeywordList) To Ubound(pvrKeywordList)
pvrNameToPlay = HalBrain.QABrain(pvrKeywordList(i), "pvrJpg", 0)
Next
End If
End If
If pvrNameToPlay = "" Then
pvrKeywords = HalBrain.RemoveExtraSpaces(HalBrain.ExtractKeywords(" " & UserSentence & " "))
pvrKeywords = Trim(Ucase(pvrKeywords))
If pvrKeywords <> "" Then
pvrKeywords = " " & pvrKeywords & " "
pvrKeywords = Replace(pvrKeywords,", "," , ",1,-1,vbTextCompare)
pvrKeywords = Replace(pvrKeywords," ","xx",1,-1,vbTextCompare)
pvrKeywordList = Split(pvrKeywords, ",")
For i = Lbound(pvrKeywordList) To Ubound(pvrKeywordList)
pvrNameToPlay = HalBrain.QABrain(pvrKeywordList(i), "pvrJpg", 0)
Next
End If
End If
If pvrNameToPlay = "" Then
pvrKeywords = WN.FindFirstNoun(UserSentence, True)
pvrKeywords = Trim(Ucase(pvrKeywords))
If pvrKeywords <> "" Then
pvrKeywords = CurrentSubject
pvrKeywords = "xx" & pvrKeywords & "xx"
pvrKeywords = Replace(pvrKeywords,", ","xx,xx",1,-1,vbTextCompare)
pvrKeywordList = Split(pvrKeywords, ",")
For i = Lbound(pvrKeywordList) To Ubound(pvrKeywordList)
pvrNameToPlay = HalBrain.QABrain(pvrKeywordList(i), "pvrJpg", 0)
Next
End If
End If
If pvrNameToPlay <> "" Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(pvrNameToPlay) Then
Set objWSHShell = CreateObject("WScript.Shell")
objWSHShell.Run """" & pvrNameToPlay & """"
End If
End If
End If
End If
Rem PLUGIN: FUNCTIONS
'The preceding comment is actually a plug-in directive for
'the Ultra Hal host application. It allows for code snippets
'to be inserted here on-the-fly based on user configuration.
Function pReadFolder(pvrCurrentFolder, pvrJpgFolder)
Set pvrFso = CreateObject("Scripting.FileSystemObject")
Set pvrFolder = pvrFso.GetFolder(pvrCurrentFolder)
'------------------
' Read all the files in the current folder
'------------------
Set pvrFiles = pvrFolder.Files
For Each pvrFile in pvrFiles
'------------------
' It's an JPG, PNG, Or GIF file; I add it to the table
'------------------
If UCase(Right(pvrFile, 3)) = "JPG" Then
pvrImage = Ucase(pvrFile)
pvrImage = Replace(pvrImage, ".JPG", "")
pvrLen = Len(pvrImage)
pvrPos = InStrRev(pvrImage, "\")
pvrRight = pvrLen - pvrPos
pvrImage = LTrim(Right(pvrImage, pvrRight))
pvrImage = " " & pvrImage & " "
pvrImage = Replace(pvrImage," ","xx",1,-1,vbTextCompare)
HalBrain.AddToTable "pvrJpg", "Brain", pvrImage, pvrFile
End If
If UCase(Right(pvrFile, 3)) = "PNG" Then
pvrImage = Ucase(pvrFile)
pvrImage = Replace(pvrImage, ".PNG", "")
pvrLen = Len(pvrImage)
pvrPos = InStrRev(pvrImage, "\")
pvrRight = pvrLen - pvrPos
pvrImage = LTrim(Right(pvrImage, pvrRight))
pvrImage = " " & pvrImage & " "
pvrImage = Replace(pvrImage," ","xx",1,-1,vbTextCompare)
HalBrain.AddToTable "pvrJpg", "Brain", pvrImage, pvrFile
End If
If UCase(Right(pvrFile, 3)) = "GIF" Then
pvrImage = Ucase(pvrFile)
pvrImage = Replace(pvrImage, ".GIF", "")
pvrLen = Len(pvrImage)
pvrPos = InStrRev(pvrImage, "\")
pvrRight = pvrLen - pvrPos
pvrImage = LTrim(Right(pvrImage, pvrRight))
pvrImage = " " & pvrImage & " "
pvrImage = Replace(pvrImage," ","xx",1,-1,vbTextCompare)
HalBrain.AddToTable "pvrJpg", "Brain", pvrImage, pvrFile
End If
Next
'------------------
' Read all the subfolders in the current folder
'------------------
Set pvrSubFolders = pvrFolder.SubFolders
For Each pvrFolderObject in pvrSubFolders
'------------------
' Pay attetion here: This is recursive! We must be sure that it will get to an end
' Since we are reading directories on a hard disk, the number of recursions
' should never be infinite. However, a very big number of subfolders and files
' might lead to memory problems or to performances bottlenecks.
' I've tested it on a folder containing about 100 subfolders and 3.000 files,
' and got acceptable performances (less than 5 minutes)
'------------------
Call pReadFolder(pvrFolderObject, pvrJpgFolder)
Next
End Function
'Directory
Function PicMediaDir()
Set fso = CreateObject("Scripting.FileSystemObject")
PicMediaDir = fso.GetAbsolutePathName(".") & "\pics\"
End Function
Rem PLUGIN: MINUTE_TIMER
If PicDudOut = 0 Then
If PicLastIdleResponse <> "" Then
If (Now - PicLastIdleResponse > 3 / 1440) And (Now - PicLastIdleResponse < 4 / 1440) Then
PicDudOut = PicDudOut + 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(PicDefaultLoad) Then
Set objWSHShell = CreateObject("WScript.Shell")
objWSHShell.Run """" & PicDefaultLoad & """"
End If
End If
End If
End If