Texttobr2.pl

%% Important: See instructions for using texttobr.pl at https://lucianpedia.wikia.com/wiki/Instructions_for_Using_texttobr(2).pl .
use_module(library(pio)).
use_module(library(dcg/basics)).
%% texttobr2 - converts file stream to dimensions of objects represented by the words
%% replace ⁃,–,— in file.txt with - or crashes
%% replace “,” in file.txt with " or crashes
%% replace ‘,’ in file.txt with ' or crashes
%% replace MS Word return in file.txt with return or crashes
%% replace nonbreaking space " " in file.txt with normal space " " or crashes
%% Copy and paste text into VI, not TextEdit, etc to avoid inserting incorrect return characters
%% has object name as separate field for new users of texttobr to verify breasonings by hand
%% brdict1.txt word and object name, brdict2.txt object name and x, y and z
%% has object name as separate field for new users of texttobr to verify breasonings by hand
%% brdict1.txt word and object name, brdict2.txt object name and x, y and z
texttobr2 :-
phrase_from_file(string(BrDict0), 'brdict1.txt'),
%%Chars="’",
SepandPad="&#@~%`$?-+*^,()|.:;=_/[]<>{}\n\s\t\\\"!'0123456789",
%%split_string(BrDict0,SepandPad,SepandPad,BrDict01),
%%writeln([brDict0,BrDict0]),
%%writeln([brdict1]),
splitfurther(BrDict0,BrDict01),
%%writeln([brDict01,BrDict01]),
char_code(Escape,27),
delete(BrDict01,[Escape,_,_,_,_],BrDict021),
%%writeln([brDict021,BrDict021]),
char_code(Apostrophe,8217),
delete(BrDict021,[Apostrophe,_,_,_,_],BrDict02),
%%writeln([brDict02,BrDict02]),
sort(BrDict02,BrDict03),
%%writeln([brDict03,BrDict03]),
length(BrDict03,Length0),write("Number of words in dictionary: "), writeln(Length0),
%%writeln(''),
%%writeln([brdict2]),
phrase_from_file(string(BrDict0t), 'brdict2.txt'),
%%Chars="’",
%%split_string(BrDict0,SepandPad,SepandPad,BrDict01),
%%writeln([brDict0,BrDict0]),
splitfurthert(BrDict0t,BrDict01t),
%%writeln([brDict01,BrDict01]),
delete(BrDict01t,[Escape,_,_,_,_],BrDict021t),
%%writeln([brDict021,BrDict021]),
delete(BrDict021t,[Apostrophe,_,_,_,_],BrDict02t),
%%writeln([brDict02,BrDict02]),
sort(BrDict02t,BrDict03t),
%%writeln([brDict03,BrDict03]),
length(BrDict03t,Length0t),write("Number of unique breasonings in dictionary: "), writeln(Length0t),
phrase_from_file(string(String), 'file.txt'),
%%atom_codes(Atom999,String),writeln([atom999,Atom999]),
split_string(String,SepandPad,SepandPad,List),
%%writeln([list,List]),
delete(List,Escape,List11),
%%writeln([list11,List11]),
delete(List11,Apostrophe,List1),
%%writeln([list1,List1]),
length(List1,Length1),write("Number of words to breason out in file.txt: "), writeln(Length1),
sort(List1,List2),
%%writeln([list2,List2]),
length(List2,Length2),write("Number of unique words in file.txt: "), writeln(Length2),
maplist(downcase_atom, List2, List3),
%%writeln([list3,List3]),
towords(BrDict03,[],BrDict04,[],_ObjectNames,[],AllUsedNames),
towords2(BrDict03t,[],BrDict04t),
%%writeln([brDict04,BrDict04]),
subtract(List3,BrDict04,D1),
%%writeln([list3,brDict04,d1,List3,BrDict04,D1]),
%%writeln(["subtract(BrDict04,List3,D1).",List3,BrDict04,D1]),	
length(D1,Length01),Difference is abs(Length01),write("Number of words remaining to define: "), writeln(Difference),
subtract(AllUsedNames,BrDict04t,D2),
%%delete(D21,'',D2),
length(D2,Length01t),Differencet is abs(Length01t),write("Number of undefined breasonings: "), writeln(Differencet),
%%writeln([undefinedbreasonings,D2]), %% Print undefined breasonings
%%delete(D31,'',D3),
subtract(BrDict04t,AllUsedNames,D3),
length(D3,Length01t2),Differencet2 is abs(Length01t2),write("Number of orphaned breasonings: "), writeln(Differencet2),
%%writeln([orphanedbreasonings,D3]), %% Print orphaned breasonings
br(List1,BrDict03,BrDict2,BrDict03t,BrDict03t2),
sort(BrDict2,BrDict3),
open('brdict1.txt',write, Stream),
%%	string_codes(BrDict3),
    write(Stream,BrDict3),
    close(Stream),
sort(BrDict03t2,BrDict03t3),
open('brdict2.txt',write, Stream2),
%%	string_codes(BrDict3),
    write(Stream2,BrDict03t3),
    close(Stream2),!.
towords([],A,A,C,C,D,D) :- !.
towords(BrDict03,A,B,D,E,G,H) :-
BrDict03=[[Word1,Word2]|Rest],
atom_string(Atom1,Word1),
atom_string(Atom2,Word2),
(Atom2=->append(G,[Atom1],I);
append(G,[Atom2],I)),
append(A,[Atom1],C),
append(D,[Atom2],F),
towords(Rest,C,B,F,E,I,H).
towords2([],A,A) :- !.
towords2(BrDict03,A,B) :-
BrDict03=[[Word,_,_,_]|Rest],
atom_string(Atom,Word),
append(A,[Atom],C),
towords2(Rest,C,B).
string(String) --> list(String).
list([]) --> [].
list([L|Ls]) --> [L], list(Ls).
splitfurther(BrDict01,N) :-
phrase(file0(N),BrDict01).
file0(N) --> "[", file(N), "]", !.
file0([]) --> [].
%%file([]) --> [].
file([L|Ls]) --> entry(L),",",
%%{writeln(L)}, %%***
file(Ls), !. %% file(Ls),{M=[Ls]})), !. %%, {writeln(["l",L])},",", file(Ls), {writeln(["ls",Ls])},!. %%, {append(L,Ls,M)}, !.	
file([L]) --> entry(L), 
%%{writeln(L)},
!. %%(entry(L),{M=L});{M=[],(writeln("Warning - Entry in incorrect format.")
%%,abort
%%)}, !.
entry([Word2,Word4]) -->
"[", word(Word), {string_codes(Word2,Word),string(Word2)},
",",
           word(Word3), {string_codes(Word4,Word3),string(Word4)},
           "]".
splitfurthert(BrDict01,N) :-
phrase(file0t(N),BrDict01).
file0t(N) --> "[", filet(N), "]", !.
file0t([]) --> [].
%%file([]) --> [].
filet([L|Ls]) --> entryt(L),",",
%%{writeln(L)}, %%***
filet(Ls), !. %% file(Ls),{M=[Ls]})), !. %%, {writeln(["l",L])},",", file(Ls), {writeln(["ls",Ls])},!. %%, {append(L,Ls,M)}, !.	
filet([L]) --> entryt(L), 
%%{writeln(L)},
!. %%(entry(L),{M=L});{M=[],(writeln("Warning - Entry in incorrect format.")
%%,abort
%%)}, !.
entryt([Word2,X3,Y3,Z3]) -->
"[", word(Word), {string_codes(Word2,Word),string(Word2)},
",",
digits(X),",",{atom_codes(X2,X),atom_number(X2,X3),number(X3)},
           digits(Y),",",{atom_codes(Y2,Y),atom_number(Y2,Y3),number(Y3)},
           digits(Z),{atom_codes(Z2,Z),atom_number(Z2,Z3),number(Z3)},
           "]".
word([X|Xs]) --> [X], {char_type(X,csymf);(X=27;X=8217)}, word(Xs), !.
%%word([X]) --> [X], {char_type(X,csymf);(X=27;X=8217)}, !.
word([]) --> [].
digits([X|Xs]) --> [X], {(char_type(X,digit);(string_codes(Word2,[X]),Word2="."))}, digits(Xs), !.
%%digits([X]) --> [X], {(char_type(X,digit);(string_codes(Word2,[X]),Word2="."))}, !.
digits([]) --> [].
br([],B,B,C,C) :-
!.
br([Word|Words],BrDict,BrDict2,BrDict4,BrDict5) :-
downcase_atom(Word, Word2), atom_string(Word2,Word3),
/**member([Word3,X,Y,Z],BrDict4) -> %% This feature is a bug because words in brdict2 shouldn't necessarily be the words in brdict1
%%(append(BrDict,[[Word3,""]],BrDict3), BrDict3t=BrDict4,
%%br(Words,BrDict3,BrDict2,BrDict3t,BrDict5))
%%;
%%(**/
%%(member([Word3,X,Y,Z],BrDict4) -> %% This feature is a bug because words in brdict1 should correspond to those in brdict2
%%(atom_concat("The breasoning for ", Word3, P1),
%%atom_concat(P1, " is defined.  Enter object name (without spaces), if different for ", Prompt));
%Prompt="Enter object name (without spaces), if different for "),
%%writeln([word3,Word3]),
(member([Word3,String4],BrDict)-> 
BrDict3=BrDict;
((repeat,
write("Enter object name (without spaces), if different for "), writeln(Word3),read_string(user_input, "\n", "\r", _End2, String2),split_string(String2, "", " ", String3),String3=[String4]),
append(BrDict,[[Word3,String4]],BrDict3))),
brth(Word3,_Brth),
(String4=""->String5=Word3;String5=String4),
downcase_atom(String5, String52), atom_string(String52,String53),
(member([String53,X,Y,Z],BrDict4)->
BrDict3t=BrDict4;
((repeat,
write("Enter x, y and z in cm for "), writeln(String53),read_string(user_input, "\n", "\r", _End, String),split_string(String, ",", " ", Values),Values=[X1,Y1,Z1],number_string(X,X1),number_string(Y,Y1),number_string(Z,Z1)),
append(BrDict4,[[String53,X,Y,Z]],BrDict3t))),
brth(String53,_Brth),
%%write("br(\'"),write(Word3),writeln("\',)."),
%%	writeln([Word3,X,Y,Z]),
%%write(' '),
br(Words,BrDict3,BrDict2,BrDict3t,BrDict5).
%%).
brth(_,sweet).
%% finds unknown words, asks for their br in form "n of m: word", verify, (can go back x) append and sort, save