/* Filename: trans2.pro Programmer: Br. David Carlson Date of creation: April 4, 1991 Revised: December 16, 1999 to run under yap. Based on the parser in grammar.pro and ideas in Neil Rowe's Artificial Intelligence Through Prolog, pp 89 - 90. Description: This program will read sentences from the file trans.in, translate them to a more readable form, and store the results in the file trans.out. Start the program with the goal: go. */ go :- see('trans.in'), tell('trans.out'), doSentences, told, seen. doSentences :- at_end_of_stream, !. doSentences :- get0(StartChar), StartChar \== 32, StartChar \== 10, !, readSentence(StartChar, WordList), process(WordList), doSentences. doSentences :- doSentences. readSentence(StartChar, [Word1| RestWords]) :- readWord(StartChar, Word1, NextChar), restOfSentence(NextChar, RestWords). restOfSentence(NextChar, [NextChar]) :- endOfSentence(NextChar), !. restOfSentence(NextChar, [Word| RemainingWords]) :- readWord(NextChar, Word, NextNextChar), restOfSentence(NextNextChar, RemainingWords). /* endOfSentence true for . ! ? */ endOfSentence(46). endOfSentence(33). endOfSentence(63). readWord(StartChar, [LCString | RestCharList], NextChar) :- wordChar(StartChar, LCString), !, get0(NextNextChar), restOfWord(NextNextChar, RestCharList, NextChar). readWord(_, Word, NextChar) :- /* skip blanks, etc at start */ get0(AnotherChar), readWord(AnotherChar, Word, NextChar). wordChar(Ch, Ch) :- Ch =< 122, Ch >= 97. % already a lower case character wordChar(Ch, LowerCh) :- Ch =< 90, Ch >= 65, LowerCh is Ch + 32. wordChar(45, 45). /* The - char */ wordChar(39, 39). /* the ' char */ restOfWord(Char, [LCString| RestCharList], NextChar) :- wordChar(Char, LCString), !, get0(AnotherChar), restOfWord(AnotherChar, RestCharList, NextChar). restOfWord(Char, [], Char). /* char cannot occur in a word */ process(L) :- translate(L, S), !, printlist(S). process(_) :- nl, write('ERROR: sentence not translatable'), nl. translate([], []) :- !. translate(List, NewList) :- substitute(Item, NewItem), append(Item, Rest, List), translate(Rest, NewRest), append(NewItem, NewRest, NewList). translate([Head | Tail], [Head | NewTail]) :- translate(Tail, NewTail). append([], L, L) :- !. append([X | L1], L2, [X | L3]) :- append(L1, L2, L3). substitute(["adversely", "impact"], ["hurt"]). substitute(["negatively", "impact"], ["hurt"]). substitute(["impact"], ["affect"]). substitute(["will", "transition"], ["will", "change"]). substitute(["must", "transition"], ["must", "change"]). substitute(["to", "transition"], ["to", "change"]). substitute(["consider", "options"], ["study"]). substitute(["evaluate", "options"], ["study"]). substitute(["under", "advisement"], ["being", "studied"]). substitute(["under", "consideration"], ["being", "studied"]). substitute(["expedite"], ["move", "along"]). substitute(["expeditiously"], ["quickly"]). substitute(["prioritize"], ["rank"]). substitute(["key", "to"], ["important", "to"]). substitute(["director", "of", "janitorial", "services"], ["head", "janitor"]). printlist([]). printlist([Head | Tail]) :- firstWord(Head), normalPrint(Tail). firstWord([]). firstWord([Head | Tail]) :- put(Head-32), printRestWord(Tail). printWord(46) :- put(46), nl. % period printWord(33) :- put(33), nl. % exclamation printWord(63) :- put(63), nl. % questionmark printWord([]). printWord([Head | Tail]) :- put(32), put(Head), printRestWord(Tail). printRestWord([]). printRestWord([Head | Tail]) :- put(Head), printRestWord(Tail). normalPrint([]). normalPrint([Head | Tail]) :- printWord(Head), normalPrint(Tail).