Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Use Dictionary.app' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use Dictionary.app
Authored by: kholburn on Mar 02, '08 02:28:50PM
a faster version:

perl -n -e 'while  (/<o:ent [^>"]+"[^>"]+"[^>"]+"([^>"]+)"/g) { $a=$1; if ($a =~ /\b'$text'\b/) { print $a,"\n";} }' /Library/Dictionaries/New\ Oxford\ American\ Dictionary.dict/Contents/dict_body


[ Reply to This | # ]
Perl version
Authored by: hamarkus on Mar 04, '08 10:17:35AM
While I think I understand the Perl code, it does not find anything (as opposed to the original version of the hint).
Though I cannot find the -n and -e flags in man perl, I assume the command simply calls Perl with the supplied file as the default input argument and executes the actual Perl code. I guess the while condition is some fancy regular expressions code that simply chops up the dictionary file into individual words, stores each word in $a and compares $a with $text and prints it if it finds a match.
However, I think somewhere the reading of library file does not work, as the following simplified code should just read the file 'Untitled.txt' and print its contents:
perl -n -e 'while(/"]+"[^>"]+"[^>"]+"([^>"]+)"/g) { print $1,"n"; }' ~/Desktop/Untitled.txt

[ Reply to This | # ]
Perl version
Authored by: kholburn on Mar 04, '08 01:38:51PM
I should mention that this works for 10.4. I haven't upgraded yet, just about backed up and ready to go. So the 10.5 dictionary may be in a different place and may be a different format.

The command switches are documented in "man perlrun".

-n makes perl read the input file in a while loop until the file is read completely.

-e indicates the next argument is a script.

The regex looks for a pattern like
<o:ent ..."...."..."..."...."> 
which indicates an entry in the dictionary. The actual entry word is between the last 2 double quotes which is caught by the brackets and referenced by the $1 in the while loop. The /g modifier at the end of the regex allows the while loop to loop through the file. The $a is needed to retain the "word" through another regex. The backslash-b's makes the regex the equivalent of -w.

Is that clear? Perl is a strange language.

[ Reply to This | # ]
Perl version
Authored by: hamarkus on Mar 04, '08 04:27:07PM

The dictionary indeed has a slightly different name in 10.5 but I corrected that already.
I had checked 'man perl' not 'man perlrun', thanks for pointing me to that.

Perl is still ok, regular expressions are what often throws me off.



[ Reply to This | # ]