REM
PRINT "IF YOU JUST GOT AN ERROR, IT'S OK."
PRINT "SOME KERMITS INSIST ON INSERTING A"
PRINT "LEADING LINEFEED IN THE FIRST FILE"
PRINT "RECEIVED."
NEW
 1000 REM MAKE ANAGRAM INTO WORD
 1010 DIM FT(31): REM FREQUENCY TABLE FOR 10000
 1020 PRINT 
 1030 PRINT "       ANAGRAM DICTIONARY"
 1040 PRINT "(PRESS RETURN AT PROMPT TO EXIT)"
 1050 PRINT 
 1090 D$ = CHR$ (4)
 1100 REM ASK FOR NEXT WORD
 1110 PRINT D$;"CLOSE"
 1120 PRINT 
 1130 INPUT "Enter anagram: ";AX$: IF AX$ = "" THEN END 
 1140 TX$ = AX$: GOSUB 10000:AC$ = AN$
 1150 AX$ = LEFT$ (AX$, LEN (AC$))
 1200 REM OPEN THE DICTIONARY
 1210 FI$ = "00" + STR$ ( LEN (AC$))
 1220 FI$ = "ANA" + RIGHT$ (FI$,2) + ".DIC"
 1230 RL = LEN (AC$) + 1
 1240 ONERR  GOTO 9000
 1250 PRINT D$;"OPEN ";FI$;",L";RL
 1300 REM INITIALIZE BINARY SEARCH
 1310 RH = 65536
 1390 RL = - RH
 1400 REM DO BINARY SEARCH
 1410 RT = INT ((RH + RL) / 2): IF RL = RT THEN GOTO 9000
 1415 RT$ = "": IF RT > 0 THEN RT$ = ",R" + STR$ (RT)
 1420 ONERR  GOTO 1480
 1430 PRINT D$;"READ ";FI$;RT$
 1440 INPUT TX$: GOSUB 10000
 1450 IF AN$ = AC$ THEN GOTO 1500
 1460 IF AN$ < AC$ THEN RL = RT: GOTO 1400
 1480 RH = RT: GOTO 1400
 1500 REM WE HAVE FOUND AN ANAGRAM. THERE MAY BE MORE.
 1510 IF RT < 2 THEN GOTO 1580
 1520 RT = RT - 1
 1530 PRINT D$;"READ ";FI$;",R";RT
 1540 INPUT TX$: GOSUB 10000: IF AN$ = AC$ THEN GOTO 1500
 1550 RT = RT + 1
 1580 REM NOW WE HAVE THE FIRST ONE.
 1600 REM PRINT ALL ANAGRAMS.
 1610 PRINT : PRINT "The known anagrams of ";AX$;" are:"
 1620 ONERR  GOTO 1100
 1630 PRINT D$;"READ ";FI$;",R";RT
 1640 INPUT TX$: GOSUB 10000: IF AN$ = AC$ THEN PRINT "        ";TX$:RT = RT + 1: GOTO 1620
 1650 GOTO 1100
 9000 REM NO ANAGRAM FOUND
 9010 PRINT : PRINT "Error - ";AX$;" has no known anagram."
 9020 GOTO 1100
 10000 REM CONVERT TEXT TO ANAGRAM
 10010 REM TEXT COMES IN IN TX$
 10020 REM ANAGRAM FORMED IN AN$
 10110 REM DIM FT(31) IN MAINLINE
 10170 FOR IT = 0 TO 31:FT(IT) = 0: NEXT IT
 10180 IF TX$ = "" THEN AN$ = "~~~~~~~~~~~~~~~~~~~~": RETURN 
 10200 FOR IT = 1 TO  LEN (TX$):CH$ = MID$ (TX$,IT,1):CH = - 1
 10210 IF CH$ >= "A" AND CH$ <= "Z" THEN CH = ASC (CH$) - ASC ("A"):FT(CH) = FT(CH) + 1
 10220 IF CH$ >= "a" AND CH$ <= "z" THEN CH = ASC (CH$) - ASC ("a"):FT(CH) = FT(CH) + 1
 10230 IF CH >= 0 THEN NEXT IT
 10300 AN$ = ""
 10310 FOR IT = 0 TO 31: IF FT(IT) = 0 THEN NEXT IT: RETURN 
 10320 CH$ = CHR$ (IT + ASC ("a")): FOR JT = 1 TO FT(IT):AN$ = AN$ + CH$: NEXT JT: NEXT IT: RETURN 
 20000 REM
 20010 REM PROGRAM ANAGRM
 20020 REM AUTHOR:	T. R. WYANT
 20030 REM DATE:	20-JUL-1990
 20040 REM REMARKS:
 20050 REM  ANAGRM IS A PROGRAM THAT TRANSLATES ANAGRAMS INTO NORMAL
 20060 REM  ENGLISH WORDS. IT IS DRIVEN BY A DICTIONARY OF ABOUT
 20070 REM  88000 WORDS, GROUPED BY LENGTH AND ARRANGED IN
 20080 REM  ALPHABETICAL ORDER BY THEIR 'CANNONICAL' ANAGRAMS (THE
 20090 REM  'CANNONICAL' ANAGRAM OF A WORD IS FORMED BY ARRANGING
 20100 REM  THE LETTERS OF THAT WORD IN ALPHABETICAL ORDER). TO FIND
 20110 REM  THE WORDS CORRESPONDING TO A GIVEN ANAGRAM, IT IS
 20120 REM  CONVERTED TO CANONICAL FORM, AND A BINARY SEARCH IS
 20130 REM  PERFORMED ON THE DESIRED LIST. 
SAVE ANAGRM
