Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extremely inefficient and unelegant code

Tags:

nlp

prolog

latin

I have a problem with my Latin translator which I am building for a school project. I have created a grammar for Latin/English sentences. I have created a predicate (possiblesentence(Latin,English).) which can translate a Latin sentence in the right order. However, as Latin sentences are in any order, I have resorted to generating a big list of permutations to find the right order of Latin to translate into English. This is my translate(Latin,English) predicate. This seems very inefficient. Is there a better way of doing this?

I also have another file that loads a big set of predicates from a .csv that look like this:

noun("femin","femin",1,f,"woman","women").
verb("port","porta","portav","portat", 1, carry, carrying, carried).

Here is my main program:

% Nick's Latin translator

% --------------
% word generator
% --------------

% predicate used to see if any two of the stems join to the ending.

stem_ending_joiner(Stem1,Stem2,Ending,Latin) :-
    (
        append(Stem1,Ending,Latin),
        !
    ;   append(Stem2,Ending,Latin),
        !
    ).

% predicate containing all possible data about nouns including endings
% and such

word_nounx(Latin,Translation,Gender,Case,Number) :-
    noun(Stem1,Stem2,_,Gender,Sgtrans,Pltrans),
    nounending(Ending,Prefix,Number,Case),
    stem_ending_joiner(Stem1,Stem2,Ending,Latin),
    (
        Number = sg,
        Translationw = Sgtrans;
        Number = pl,
        Translationw = Pltrans
    ),
    append(Prefix," ",Prefixspace),
    append(Prefixspace,Translationw,Translation).

% predicate containing all possible data about verbs including endings
% and such
% arguments: Latin,Translation,Tense,Number,Mood,Voice

word_verbx(Latin,Translation,Tense,Number,Mood,Voice,Person) :-
    verb(Stem1,_,_,_,_,Ptrans,_,Pastrans),
    verbending(Ending,Tense,Prefix,Number,Mood,Voice,Person),
    stem_ending_joiner(Stem1,_,Ending,Latin),
    (
        Tense = present,
        Translationw = Ptrans;
        Tense = past,
        Translationw = Pastrans
    ),
    append(Prefix," ",Prefixspace),
    append(Prefixspace,Translationw,Translation).

% -------
% parsing
% -------

word_noun(Latin,Translation,Gender,Case,Number) :-
    word_nounx(Latinx,Translationx,Gender,Case,Number),
    name(Latin,Latinx),
    name(Translation,Translationx).

word_verb(Latin,Translation,Tense,Number,Mood,Voice,Person) :-
    word_verbx(Latinx,Translationx,Tense,Number,Mood,Voice,Person),
    name(Latin,Latinx),
    name(Translation, Translationx).

pnoun(Gender,Case,Number) -->
   [[English,Latin]],
   {word_noun(Latin,English,Gender,Case,Number)}.
nounphrase(Gender,Case,Number) -->
   pnoun(Gender,Case,Number).
pverb(Tense,Number,Mood,Voice,Person) -->
   [[English,Latin]],
   {word_verb(Latin,English,Tense,Number,Mood,Voice,Person)}.
verbphrase(Tense,Number,Mood,Voice,Person,Gender,Case,Nnumber) -->
   pverb(Tense,Number,Mood,Voice,Person),
   nounphrase(Gender,Case,Nnumber).
sentence -->
   nounphrase(_,nom,Number),
   verbphrase(_,Number,_,_,3,_,nom,_).

% Predicates which manipulate lists of list-pairs to get the first and
% last elements of each list.

headofelements([],[]).
headofelements([H|T],[[H|_]|T1]) :-
    headofelements(T,T1).

tailofelements([],[]).
tailofelements([H|T],[[_|H]|T1]) :-
    tailofelements(T,T1).

lastofelements([],[]).
lastofelements([H|T],[X|T1]) :-
    last(X,N),
    H = N,
    lastofelements(T,T1).

% generates possible sentences with the latin and the english.

possible_sentence(X,N) :-
    phrase(sentence,Y),
    lastofelements(X,Y),
    headofelements(N,Y).

translate(Latin,English) :-
    permutation(Latin,X),
    possible_sentence(X,English).

Here are the endings:

% --------------------------
% ADJECTIVE ENDINGS
% ------------------------------

adjending("us",sg,nom,m).

% -----------------------------------
% VERBENDINGS
% ------------------------------------

verbending("o",present,"I",sg,_,_,1).
verbending("as",present,"you",sg,_,_,2).
verbending("at",present,"He",sg,_,_,3).
verbending("amus",present,"We",pl,_,_,1).
verbending("atis",present,"You",pl,_,_,2).
verbending("ant",present,"They",pl,_,_,3).


% -----------------------------------
% NOUN ENDINGS
% -----------------------------------

nounending("a","",sg,nom).
nounending("am","",sg,acc).
nounending("ae","of",sg,gen).
nounending("ae","to",sg,dat).
nounending("a","with",sg,abl).
nounending("ae","",pl,nom).
nounending("as","",pl,acc).
nounending("arum","of",pl,gen).
nounending("is","to",pl,dat).
nounending("is","with",pl,abl).
like image 375
user2850249 Avatar asked May 14 '26 06:05

user2850249


1 Answers

Latin should be a favourite application for Free-Word-Order Dependency Parsing.

The ProNTo Toolkit describes such technique and provides some introductory SW.

Incidentally, the dependency parser documentation uses Latin as primary application domain.

like image 83
CapelliC Avatar answered May 16 '26 03:05

CapelliC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!