I need to write a script that checks if a word is in an English dictionary
Any ideas?
Thanks
gecko
Browser: Internet Explorer 6.0
Operating System: Mac OS X (10.3.9)
I need to write a script that checks if a word is in an English dictionary
Any ideas?
Thanks
gecko
Browser: Internet Explorer 6.0
Operating System: Mac OS X (10.3.9)
You might look at MacDict X. It’s scriptable, but I have not figured out how to copy the definition from the window it produces, so have never used it.
There’s no easy way to simply do a search for something in dictionary.app and have it return properties or info about the query you’ve made. This in my opinion was a major oversight/exclusion made by apple. I’m not sure why they’ve done it that way, but making dictionary scriptable seems so obviously beneficial, that I can’t fathom a good justification for leaving the feature out.
If you’re feeling ambitious, you could write your own engine to manually parse and return data from the database of words in the dictionary, but that would SUCK. If you want to get silly trying this, check out “/Library/Dictionaries/New Oxford American Dictionary.dict/”… which is a package of data files containing the dictionary database and supporting files. “…/Contents/dict_body” appears to have the actual words and definitions, but processing it in applescript appears to be a bee-ahch.
A simple solution (if it fit’s with your setup) would be to invoke the “dict” protocol via standard additions, which will open the word in the dictionary app for you.
set theSearchString to "test"
tell application "Dictionary" to activate
open location ("dict:///" & theSearchString)
There are also probably some online dictionaries that have api’s for doing remote searches, but I can’t say for sure what they might be.
j
The file web2 at the path ‘/usr/share/dict/web2’ is a text file containing a handy list of most English words.
You could use it with the Unix tool grep to find whether or not it appears in that list: (ignoring case)
on wordIsEnglish(aWord)
-- The argument '-i' ignores case
-- echo used to prevent dialog coming up when no match is found
return (do shell script "echo `grep -wci " & aWord & " /usr/share/dict/web2`") is not "0"
end wordIsEnglish
wordIsEnglish("cat")
--> true
wordIsEnglish("applescript")
--> false
BTW, this isn’t necessarily anything to do with AppleScript Studio.
Oh, and when did Internet Explorer 6.0 come out for Mac OS X?
This little script will check whether the word is in a dictionary or not:
set W to text returned of (display dialog "Please enter the word to be checked" default answer "Enter one word")
try
set Defs to word 2 of (do shell script "curl dict://dict.org/d:" & W & " | grep 150")
if Defs as number > 0 then display dialog "There is/are " & Defs & " definition(s) available"
on error
display dialog "Not a word in an English dictionary"
end try
To get the definition itself (of a word that has one):
set W to "elongate"
set def to display dialog W & return & (do shell script "curl dict://dict.org/d:" & W & " | grep " & W)
Perhaps better, getting the whole of it:
set W to text returned of (display dialog "Enter a word for definition" default answer "Word" buttons {"Cancel", "Define"} default button "Define")
try
set D to (do shell script "curl dict://dict.org/d:" & W)
set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "151"}
set D2 to last text item of D
set AppleScript's text item delimiters to "250"
set D3 to first text item of D2
set AppleScript's text item delimiters to astid
display dialog D3
on error
display dialog "Oops, word not in the online dictionary."
end try
As a final version:
to getDefinition(entry)
set D to (do shell script "curl dict://dict.org/d:" & entry)
if D contains "no match" then
return "No match found by dict.org"
else
set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "151"}
set part_1 to last text item of D
set AppleScript's text item delimiters to "250"
set def to first text item of part_1
set AppleScript's text item delimiters to astid
return def
end if
end getDefinition
display dialog getDefinition("concupiscent")