/********************************** START OF APPLICATION **********************************/ /* ######################################################### ######################################################### ### ### ### ### ### --------------------------- ### ### lzw-compressor-en.pl v1.0 ### ### ... created by JK ... ### ### --------------------------- ### ### ### ### ### ### DESCRIPTION: ### ### ### ### Simple application for compression ASCII text ### ### from input or text file to LZW code. ### ### ### ### ### ######################################################### ######################################################### */ /************************** Start of program ... **************************/ % start - input point of program start:- nl, nl, write('-------------------------------------------------'), nl, write('Welcome user in application lzw-compressor-en.pl!'), nl, write('-------------------------------------------------'), nl, nl, write('Do you want input from file? (yes/no) '), read(Answer), nowWhat(Answer), % call nowWhat/1 with answer from user to question above nl. % help - cross reference to WWW help help:- nl, nl, write('For start application type command: start.'), nl, nl, write('For more information visit URL adress:'), nl, write('----------------------------------------------'), nl, write('http://home.pf.jcu.cz/'), write(~), write('krhanj00/lgp/app'), nl, write('----------------------------------------------'), nl, nl. /****************************** Input data form user ... ******************************/ % nowWhat(Answer) - decide if user type yes or no. Answer is answer from user if he want code file or input string. nowWhat(Answer):- Answer = 'yes', !, nl, write('Type name of file (relative path whitin simple apostroph ended dot - forexample: ''text.txt''.) '), nl, read(NameOfFile), nl, see(NameOfFile), read(String), seen, write('Input text from file: '), nl, loadFile(NameOfFile), % call loadFile/1 where NameOfFile is name of file for coding nl, makeCompression(String). % call makeCompression/1 where String will be compressed to LZW code nowWhat(Answer):- nl, write('Type string for compression (string must be whitin simple apostroph ended dot - forexample: ''hello hello''.) '), nl, read(String), nl, makeCompression(String). % call makeCompression/1 where String will be compressed to LZW code /******************************** Reading data from file ... ********************************/ % loadFile(NameOfFile) - read chars from file, where NameOfFile is name of file from user loadFile(NameOfFile):- see(NameOfFile), % change input device from keyboard to file repeat, % loop repeat read all chars from file until end of file get0(ASCIIChar), % get0/1 - get char from stream, but only ASCII code of char not helpPrint(ASCIIChar), % helpPrint/1 - print contain of file to screen ASCIIChar = (-1), % if end of file, we end loop repeat !, % cut repeat seen. % return input device % helpPrint(ASCIIChar) - print chars to screen, where ASCIIChar is char to print helpPrint(ASCIIChar):- char(ASCIIChar), % if ASCIIChar is really char from ASCII table of chars !, % than name(Char,[ASCIIChar]), % name/2 - convert ASCIIChar to classic char write(Char). % and write Char to screen /******************************* Engine of compression ... *******************************/ % makeCompresion(String) - String is input string for compression. Convert string to ASCII chars, make dictionary and start engin of compression. If not succeed, destroy dictionary and clean memory. makeCompression(String):- stringToASCIIChars(String,Chars), % stringToASCIIChars/2 - convert string to list of chars makeDictionary, % makeDictionary - make dictionary - see below nl, write('LZW code for input string: '), nl, engine(Chars,128), % engine/2 - start engin of compression. 128 is number to start counting for new item of dictionary !. % only green cut makeCompression(String):- retractall(replace(_,_)). % retractall/1 - erase all items from dictionary - so clean memory % engine(Chars,I) - RECURSION - main part of this application. Chars is list of chars, which convert to LZW code. I is variable for incrementation key of value. Data type is integer. engine([],I):-!. % stop of recursion engine(Chars,I):- findall(Number,sublist(Chars,Number),List), % findall/3 - find all sublist/2 and their numbers put into List maximum(Code,List), % maximum/2 - from this List find out the highest code write(Code), % print the code to screen write('-'), replace(Code,Value), % replace/2 - find out Value from our Code concatenation(Value,NewChars,Chars), % concatenation/3 - remove Value (list of chars) from Chars (whole list of chars) addCodeValue(Value,Chars,Result), % addCodeValue/3 - join new char to our Value assertz(replace(I,Result)), % assertz/1 - new value get new code and new item is add to the end of this program I1 is I + 1, % incrementation about one for I engine(NewChars,I1). % again call engine/2, but now with new list - NewChars, and new I -> RECURSION /***************************** Utilities for engin ... *****************************/ % stringToASCIIChars(String,Chars) - convert string to chars, where String is input and Chars are output stringToASCIIChars(String,Chars):- atom_chars(String,ASCIIChars), % convert string to ASCII values of chars aSCIICharsToChars(ASCIIChars,Chars). % convert list of ASCII values of chars to classic Chars % aSCIICharsToChars(Input,Output) - RECURSION - convert ASCII values of chars to classic Chars aSCIICharsToChars([],[]):-!. % stop of recursion aSCIICharsToChars([H|T],[H1|T1]):- atom_chars(H1,[H]), % convert first item list to char aSCIICharsToChars(T,T1). % again call aSCIICharsToChars/2 -> RECURSION % addCodeValue(Value,Chars,Result) - create valeu for new item of dictionary, where Value is last value, Chars is whole string and Result is new item. addCodeValue(Value,Chars,Result):- concatenation(Value,[Head|TaleOfChars],Chars), % split chars along Value and find out first char behind Value list addToEnd(Head,Value,Result). % addToEnd/3 - "first char" put to the end of value list and get new item for dictionary (Result) /***************************** Utilities for lists ... *****************************/ % addToEnd(Item,SourceList,ResulList) - RECURSION - add Item to the end of SourceList and so grow up ResultList addToEnd(X,[],[X]). % stop of recursion addToEnd(X,[H|T],[H|T1]):- addToEnd(X,T,T1). % again call addToEnd/3 -> RECURSION % concatenation(FirstPartOfWholeList,SecoundPartOfWholeList,WholeList) - RECURSION - split whole list to first and secound part concatenation([],L,L). % stop of recursion concatenation([X|L1],L2,[X|L3]):- concatenation(L1,L2,L3). % again call concatenation/3 -> RECURSION % maximum(TheHighestItemOfList,List) - find out the highest item from list with help of bubbleSort - see below maximum(X,L):- bubbleSort(L,[X|_]). % bubbleSort(List,SortedList) - RECURSION - sort the list (from high to low) bubbleSort(L,SortedL) :- append(L0,[X,Y|T],L), % append/3 - split or join list Y > X, % and if Y > X !, % cut -> IF - THEN append(L0,[Y,X|T],L1), % append/3 - split or join list bubbleSort(L1,SortedL). % again call bubbleSort/2 -> RECURSION bubbleSort(SortedL,SortedL). % stop of recursion - if list is already sorted list % sublist(Chars,Code) - find out if begin list of chars like is item in dictionary sublist([V|_],C):- replace(C,[V]). sublist([V,W|_],C):- replace(C,[V,W]). sublist([V,W,X|_],C):- replace(C,[V,W,X]). sublist([V,W,X,Y|_],C):- replace(C,[V,W,X,Y]). sublist([V,W,X,Y,Z|_],C):- replace(C,[V,W,X,Y,Z]). sublist([V,W,X,Y,Z,A|_],C):- replace(C,[V,W,X,Y,Z,A]). sublist([V,W,X,Y,Z,A,B|_],C):- replace(C,[V,W,X,Y,Z,A,B]). /******************** Dictionary ... ********************/ % makeDictionary - help with assertz/1 create new items of dictionary makeDictionary:- % SOME CHARS WE ARE NOT USE /* assertz(replace(0,[''])), assertz(replace(1,['?'])), assertz(replace(2,['?'])), assertz(replace(3,['?'])), assertz(replace(4,['?'])), assertz(replace(5,['?'])), assertz(replace(6,['?'])), assertz(replace(7,['?'])), assertz(replace(8,['?'])), assertz(replace(9,['?'])), assertz(replace(10,['?'])), assertz(replace(11,['?'])), assertz(replace(12,['?'])), assertz(replace(13,['?'])), assertz(replace(14,['?'])), assertz(replace(15,['¤'])), assertz(replace(16,['?'])), assertz(replace(17,['?'])), assertz(replace(18,['?'])), assertz(replace(19,['?'])), assertz(replace(20,['¶'])), assertz(replace(21,['§'])), assertz(replace(22,['?'])), assertz(replace(23,['?'])), assertz(replace(24,['?'])), assertz(replace(25,['?'])), assertz(replace(26,['?'])), assertz(replace(27,['?'])), assertz(replace(28,['?'])), assertz(replace(29,['?'])), assertz(replace(30,['?'])), assertz(replace(31,['?'])), */ assertz(replace(32,[' '])), assertz(replace(33,['!'])), assertz(replace(34,['"'])), assertz(replace(35,['#'])), assertz(replace(36,['$'])), assertz(replace(37,['%'])), assertz(replace(38,['&'])), assertz(replace(39,[''''])), assertz(replace(40,['('])), assertz(replace(41,[')'])), assertz(replace(42,['*'])), assertz(replace(43,['+'])), assertz(replace(44,[','])), assertz(replace(45,['-'])), assertz(replace(46,['.'])), assertz(replace(47,['/'])), assertz(replace(48,['0'])), assertz(replace(49,['1'])), assertz(replace(50,['2'])), assertz(replace(51,['3'])), assertz(replace(52,['4'])), assertz(replace(53,['5'])), assertz(replace(54,['6'])), assertz(replace(55,['7'])), assertz(replace(56,['8'])), assertz(replace(57,['9'])), assertz(replace(58,[':'])), assertz(replace(59,[';'])), assertz(replace(60,['<'])), assertz(replace(61,['='])), assertz(replace(62,['>'])), assertz(replace(63,['?'])), assertz(replace(64,['@'])), assertz(replace(65,['A'])), assertz(replace(66,['B'])), assertz(replace(67,['C'])), assertz(replace(68,['D'])), assertz(replace(69,['E'])), assertz(replace(70,['F'])), assertz(replace(71,['G'])), assertz(replace(72,['H'])), assertz(replace(73,['I'])), assertz(replace(74,['J'])), assertz(replace(75,['K'])), assertz(replace(76,['L'])), assertz(replace(77,['M'])), assertz(replace(78,['N'])), assertz(replace(79,['O'])), assertz(replace(80,['P'])), assertz(replace(81,['Q'])), assertz(replace(82,['R'])), assertz(replace(83,['S'])), assertz(replace(84,['T'])), assertz(replace(85,['U'])), assertz(replace(86,['V'])), assertz(replace(87,['W'])), assertz(replace(88,['X'])), assertz(replace(89,['Y'])), assertz(replace(90,['Z'])), assertz(replace(91,['['])), assertz(replace(92,['\'])), assertz(replace(93,[']'])), assertz(replace(94,['^'])), assertz(replace(95,['_'])), assertz(replace(96,['`'])), assertz(replace(97,['a'])), assertz(replace(98,['b'])), assertz(replace(99,['c'])), assertz(replace(100,['d'])), assertz(replace(101,['e'])), assertz(replace(102,['f'])), assertz(replace(103,['g'])), assertz(replace(104,['h'])), assertz(replace(105,['i'])), assertz(replace(106,['j'])), assertz(replace(107,['k'])), assertz(replace(108,['l'])), assertz(replace(109,['m'])), assertz(replace(110,['n'])), assertz(replace(111,['o'])), assertz(replace(112,['p'])), assertz(replace(113,['q'])), assertz(replace(114,['r'])), assertz(replace(115,['s'])), assertz(replace(116,['t'])), assertz(replace(117,['u'])), assertz(replace(118,['v'])), assertz(replace(119,['w'])), assertz(replace(120,['x'])), assertz(replace(121,['y'])), assertz(replace(122,['z'])), assertz(replace(123,['{'])), assertz(replace(124,['|'])), assertz(replace(125,['}'])), assertz(replace(126,[~])), assertz(replace(127,['¦'])). /*********************************** END OF APPLICATION ***********************************/