OK, here’s one that seems strange to me. I’ve read the discussion of the differences between properties and global and local variables and how they are scoped. But I have a list declared as a property that IS visible to a handler in the script (as it should be) but when I update the list (add an item) within the handler, the added item is visible within the handler (it’s been added to the list) but when I come out of the handler and re-enter it the added list item is gone (or ignored by contains).
I’m working on what will eventually be a chatbot (or chatterbox), a program that “chats” in an intelligent fashion. So far I’m still breaking down the input sentence for meaning, but I intend to allow the program to “learn” by adding items to the appropriate lists (noun, pronoun, etc.). But if the added item doesn’t “stick” then I’m sunk. Here’s the script:
property question : {"who", "what", "when", "where", "how", "why"}
property article : {"a", "an", "the"}
property conjunction : {"and", "but", "or"}
property negation : {"not"}
property noun : {"computer", "boy", "girl", "man", "woman", "answer", "name"}
property adverb : {"quickly", "slowly", "happily", "quietly"}
property subjectpronoun : {"I", "you", "they", "we", "he", "she", "it"}
property verb : {"am", "is", "are", "like", "want", "give", "take", "go", "goes", "do", "does", "have", "has"}
property objectpronoun : {"me", "you", "them", "us", "him", "her", "it"}
property possessive : {"my", "mine", "your", "yours", "his", "hers", "its", "their", "theirs", "our", "ours"}
property adjective : {"big", "small", "black", "white", "fat", "thin", "happy", "sad"}
property mypreposition : {"to", "on", "with", "around", "near", "without"}
property parts : {"noun", "verb", "article", "conjunction", "negation", "adverb", "subjectpronoun", "objectpronoun", "possessive", "adjective", "preposition"}
property partList : {noun, verb, article, conjunction, negation, adverb, subjectpronoun, objectpronoun, possessive, adjective, mypreposition}
copy AppleScript's text item delimiters to astid
set text item delimiters to space
set myCmd to ""
repeat while myCmd is not "quit"
set myCmd to text returned of (display dialog "Talk to me" default answer "")
display dialog analyzeSentence(myCmd) as string
end repeat
copy astid to text item delimiters
-- handlers for dialog
to analyzeSentence(theSentence)
set thePattern to {}
repeat with myWord in (words of theSentence)
ignoring case
if (myWord is in subjectpronoun) and (myWord is in objectpronoun) then
if thePattern ends with "verb" then
set thePattern to thePattern & "objectpronoun"
else
set thePattern to thePattern & "subjectpronoun"
end if
else
if question contains myWord then
set thePattern to thePattern & "?"
else if (article contains myWord) then
set thePattern to thePattern & "article"
else if (noun contains myWord) then
set thePattern to thePattern & "noun"
else if (conjunction contains myWord) then
set thePattern to thePattern & "conjunction"
else if (possessive contains myWord) then
set thePattern to thePattern & "possessive"
else if (subjectpronoun contains myWord) then
set thePattern to thePattern & "subjectpronoun"
else if (verb contains myWord) then
set thePattern to thePattern & "verb"
else if (adverb contains myWord) then
set thePattern to thePattern & "adverb"
else if (objectpronoun contains myWord) then
set thePattern to thePattern & "objectpronoun"
else if (adjective contains myWord) then
set thePattern to thePattern & "adjective"
else if (mypreposition contains myWord) then
set thePattern to thePattern & "preposition"
else
set myPrompt to " What kind of word is " & myWord & "?"
set thePart to choose from list parts with prompt (myPrompt as string)
set myItem to 1
set found to false
repeat while not found
if thePart as string = item myItem of parts then
set (item myItem of partList) to (item myItem of partList & myWord)
set found to true
set thePattern to thePattern & thePart
--display the list with the added word
display dialog (item myItem of partList) as string
end if
set myItem to myItem + 1
end repeat
end if
end if
end ignoring
end repeat
return thePattern
end analyzeSentence
Try running the program and using a new word (“dog” or “brown” or both) and it will ask you to pick the part of speech the word belongs to. The next time through the parser, though, it doesn’t recognize the new word, even though it’s been added to the list.
Is this aberrant behavior on the part of AppleScript? Or did I miss something in the discussion of scope?
Model: iMac DV+ (450mhz), 384mb, 40gb HD
AppleScript: 1.9.1
Browser: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.1) Gecko/20060214 Camino/1.0
Operating System: Mac OS X (10.2.x)