%%%%%%%%%%%%%%%%%%%%%%

%% sentence.pl

%%%%%%%%%%%%%%%%%%%%%%

 

 

/* Traduire "sentence" */

/* ------------------  */

 

 

:- style_check(-singleton).

 

 

/* compatibility */

%%%%%%%%%%%%%%%%%%%%

 

ifthen(Condition,Goal) :- Condition -> Goal ; true.

ifthenelse(Condition, ThenGoal, ElseGoal) :- Condition -> ThenGoal ; ElseGoal.

 

 

 

%%%%%%%%%%%%%%

/* GO step */

%%%%%%%%%%%%%%

 

go :- nl,

     write('SENTENCE: phrase ou peine ?'),nl,

     nl,

     write('A.Michiels,ULg'),nl,nl,

     write('Fichier de phrases?  [stdin. ou nom_de_fichier.] --> '),

     read(Input),

     dealwith(Input,HandleIn),

     write('Fichier de résultats? [nom_de_fichier.] --> '),

     read(Output),

     concat(Output,'.lst',Outlist),

     !,

     start(Outlist,HandleIn,Input).

  

 

/* Input is from stdin(user's terminal) or from file */

 

dealwith(Input,_):-

             Input=stdin,!.

 

dealwith(FileIn,HandleIn):-

             open(FileIn,read,HandleIn).

 

   

 

start(Outlist,_,stdin) :-

             open(Outlist,write,Lists),

             repeat,

             nl,

             write('Tapez votre phrase ou stop. pour sortir'),

             nl, 

             getsentence(Sentence,user), % getting the sentence to be parsed from the user's terminal

             nl,write(Sentence),nl,

         nl(Lists),

             writeq(Lists,Sentence),

             nl(Lists),

         process(Sentence,Lists),

         fail.

 

 

start(Outlist,HandleIn,Input) :-

             Input \= stdin,

             open(Outlist,write,Lists),

             nl,

             repeat,

             nl, 

             write('>>'),

             getsentence(Sentence,HandleIn),

             write(Sentence),nl,

         nl(Lists),

             writeq(Lists,Sentence),

             nl(Lists),

         process(Sentence,Lists),

         fail.

 

 

 

%%%%%%%%%%%%%%%%%

% PROCESSING %%%%

%%%%%%%%%%%%%%%%%

 

 

/* stopping ... */

 

process(Sentence,Lists) :-

               Sentence = [stop],

               close(Lists),

               abort.

    

 

/* working */

 

process(Sentence,Lists):-

            (member(sentence,Sentence), Target=0;

             member(sentences,Sentence), Target=1),

             process(Sentence,Lists,Target).

 

% Target is a bonus towards 'phrase' when the plural is used (sentences)

 

 

process(S,L,T) :-

             cwlaw(S,LawWeights,LawInfo),

             cwling(S,LingWeights,LingInfo),

             cwpatlaw(S,LawPats),

             cwpatling(S,LingPats),

             total(LawWeights,LawW),

             total(LingWeights,LingW),

             totalpat(LawPats,LawP),

             totalpat(LingPats,LingP),

             TotLaw is LawW+LawP,

             TotLing is LingW +LingP+ T,

             report(TotLaw,TotLing, Winner, Ratio),

             nl, write(Winner), tab(10),

             write(Ratio), nl,

             write(law(LawInfo)),nl,write(LawPats),nl,

             write(ling(LingInfo)),nl,write(LingPats),nl,

             nl(L),write(L,Winner), tab(L,10),

             write(L,Ratio), nl(L),

             write(L,law(LawInfo)),nl(L),write(L,LawPats),nl(L),

             write(L,ling(LingInfo)),nl(L),write(L,LingPats),nl(L).

 

 

report(0,0,phrase,default_solution):- !.

report(L,L,ambiguous,no_solution) :- !.

report(0,L,phrase,L) :- !.

report(L,0,peine,L) :- !.

report(Law, Ling, peine, ratio(Ratio)) :-

       Law > Ling, Ratio is Law/Ling.

report(Law, Ling, phrase, ratio(Ratio)) :-

       Ling > Law, Ratio is Ling/Law.

 

 

 

 

cwlaw([W|Ws],[Weight|OtherWeights],[W|OtherWs]) :-

        law(List),

        in(W,List,Weight),!,

        cwlaw(Ws,OtherWeights,OtherWs).

 

cwlaw([W|Ws],Weights,Words) :- cwlaw(Ws,Weights,Words).

 

cwlaw([],[],[]).

 

 

cwling([W|Ws],[Weight|OtherWeights],[W|OtherWs]) :-

        ling(List),

        in(W,List,Weight),!,

        cwling(Ws,OtherWeights,OtherWs).

 

cwling([W|Ws],Weights,Words) :- cwling(Ws,Weights,Words).

 

cwling([],[],[]).

 

 

in(W,[W-Weight|Others],Weight) :- !.

in(W,[Word-Weight|Others],Poids) :-

     in(W,Others,Poids).

 

 

cwpatlaw(WordList,WPL) :-

         findall(Weight-Pat,

               (lawpat(Pat,Weight), sublist(Pat,WordList)),

               WPL).

 

cwpatling(WordList,WPL) :-

         findall(Weight-Pat,

               (lingpat(Pat,Weight), sublist(Pat,WordList)),

               WPL).

 

 

%%%%%%%%%%

/* DATA */

%%%%%%%%%%

 

law([abate-1,abated-1,abates-1,abating-1,

     abuse-2, abuser-2, abusers-3,

     adjourn-2,adjourns-2, adjourned-2,adjourning-2,

     against-1,

     appeal-3,

     burglary-2,

     carry-1,carries-1,carried-1, carrying-1,

     commutable-2,commute-2,commuted-2,commutes-2,

     concurrent-1,concurrently-1,

     condemn-2,condemns-2, condemned-2, condemning-2,

     consecutively-1,

     convict-3, convicts-3,convicted-3,

     country-1,

     court-5,courts-5,

     crime-3,crimes-3, criminal-3,

     custodial-5,custody-5,

     day-1,days-1,

     dealer-2, dealers-2,

     death-2,

     decision-1,

     defer-2,defers-2, deferred-2, deferring-2,

     dock-2, docks-2,

     drug-2, drugs-2,

     duty-1,

     execution-1,execute-1, executes-1, executed-1,

     extended-1,

     face-1,faces-1, faced-1, facing-1,

     fines-3,

     five-1,

     fix-1, fixes-1,fixed-1, fixing-1,

     force-1, forces-1,forced-1,

     four-1,

     guilty-2,

     hanged-2,

     harsh-2, harsher-2,harshest-2,

     hearing-2, hearings-4,

     heavy-1,heavier-1,

     home-1,

     impose-1, imposes-1,imposing-1,imposed-1,

     imprisonment-3,

     incarcerate-3, incarcerated-3,

     gaol-3,jail-3,

     judge-1,judges-1,judged-1,judging-1,

     jury-3, juries-3,

     lash-2,

     law-2,laws-2,

     lenient-3,

     light-1,lighter-1,

     life-1,

     lifetime-3,

     magistrate-2, magistrates-3,

     mandatory-1,

     maximum-1,

     merciful-2,

     mitigate-1,mitigates-1,mitigation-1,

     month-1,months-1,

     murder-1,murders-1,murderer-1,murderers-1,

     nine-1,

     offence-2,offences-3,

     offender-3, offenders-3,

     overturn-1,overturns-1,overturning-1,overturned-1,

     pass-2,passed-2,passing-2,

     payment-1,

     plea-2, plead-2, pleads-2, pleaded-2, pleading-2,

     poverty-1,

     prison-2,prisoner-2,prisoners-2,prisons-2,

     probation-3,

     proceedings-1,

     punishment-2,punish-2, punishes-2, punished-2,

     ranging-2,

     rape-2, rapist-2, rapists-3,raped-3, raping-3,

     receive-1,received-2,receives-1,receiving-1,

     recommendation-1,

     reduce-1,reduces-1, reduced-1, reducing-1,

     remission-2,remit-2,remitted-2,

     repeal-3,repeals-3, repealed-3,repealing-3,

     reverse-1,reverses-1, reversing-1, reversed-1,

     review-1,reviews-1, reviewing-1,reviewed-1,

     rape-2,rapes-2, rapist-2,

     ruling-3, rulings-3,ruled-3, 

     run-1,

     scraped-1,

     serve-2,served-2,serving-2,serves-2,

     seven-1,

     severe-2,severity-2,

     sexual-1,

     six-1,

     stay-1,

     stiff-2,stiffer-3,

     suffer-1,

     suspended-3,

     ten-1,

     three-1,

     tough-1, tougher-2, toughest-2,

     trial-2, trials-3,

     unemployment-1,

     uphold-2,upholding-2,upheld-2,upholds-2,

     verdict-3,verdicts-2,

     wrong-1,

     year-1,years-2]).

 

 

 

ling([active-1,

      adjective-2, adjectives-2,

      adverb-2,adverbs-2,adverbial-2, adverbials-2,

      agreement-1,

      ambiguous-2, ambiguity-2,disambiguate-3, disambiguated-3,

      analysis-2,analyze-2,analyzed-2,analyse-2, analysed-2, analyzing-2, analysing-2,

      article-1,articles-1,

      articulate-2,

      ask-1,

      assertion-1,assertions-1,

      author-2, authors-2,

      begin-1,begins-1,beginning-1,

      book-1,books-1,

      brackets-2,

      break-1,broken-1,

      clause-1, clauses-1,

      cleft-2,

      clumsy-1, clumsier-1,clumsiest-1,

      comma-2,

      command-1,commands-1,

      communication-2,

      compact-1,

      complete-2,incomplete-2,completion-2,

      complex-2, complexity-2,

      complicated-2,

      composition-2,composed-2,

      compound-2,

      conclusion-1,

      conditional-1,

      connected-2,connector-2,connect-2, connects-2,connectors-3,

      construction-2, constructed-2, construct-2, constructions-2,

      construe-2, construed-2, construal-2,

      containing-1,contains-1,

      context-2, contexts-2,

      convoluted-2,

      cut-1,

      deviant-1,

      different-1,

      divide-1,divides-1, divided-1,

      document-1,documents-1,

      elegant-2,

      end-1,ends-1,ending-1, endings-2,

      english-1,

      example-2,examples-2,

      exclamation-2,exclamatory-3,

      expresses-1,express-1, expressed-1,expressing-1,

      final-1,

      finish-1,finishes-1,unfinished-1,finishing-1,

      finite-2,

      first-1,

      following-2,follows-1, followed-1,

      form-2,formed-2,forms-2,formation-3,

      frame-1,frames-1,

      french-1,

      full-1,

      generate-2, generates-2, generated-2,

      german-1,

      grammatical-3, ungrammatical-3,grammar-3, grammars-3,

      illiterate-1,

      indeterminate-1,

      individual-1,

      information-1, informative-3,

      initial-1,

      input-2,

      instruction-1,

      interpret-2, interprets-2, interpreted-2,

      interpretation-2,interpretations-3,

      interrupt-1, interrupted-1,interrupts-1,

      kernel-3,

      language-2, languages-3,

      last-2,

      length-2,

      letter-1,letters-2,

      lexical-3,

      linguistic-3, linguistics-3,

      literature-2, literatures-3, literary-3,literate-3, literacy-3,

      logic-2, logical-3,

      main-2,

      marks-2,mark-2,

      mean-2, means-2, meant-2,

      meaning-2,meaningful-3,meaningless-3,

      message-2, messages-2,

      mid-3,middle-2,

      misread-2,misreading-2,

      modal-2, modals-3, modality-3,

      neat-1,neatly-1,

      next-2,

      noun-2,nouns-2,

      novel-2,

      occurs-2, occurrence-2, occurrences-2,occur-2,

      offending-1,

      opening-2,opens-2,

      original-2,

      paper-1,

      paragraph-1,paragraphs-1,

      parse-4,parsing-4,parsed-4,parses-4,

      passive-2,

      pattern-2,patterns-2,

      phrase-2,phrases-2,phrasing-3,phrased-3,rephrase-3, rephrasing-3,rephrased-3,

      predicate-2,predicated-2,predicates-2,

      previous-1,

      processing-2,

      production-2,

      properly-1,

      punctuation-3,

      question-1,questions-1,

      quote-2, quotes-2, quoted-3, quotation-2, quotations-3,

      reads-1,reading-1,reader-2,readers-2,

      recast-1,

      repeat-2, repeats-2, repeated-2, repeating-2,

      report-1,reports-1,

      reword-2,reworded-2, rewording-2,

      roll-1,

      round-1,rounded-1,

      rule-1,rules-1,

      running-1,

      said-1,

      sample-2,

      scribble-2,scribbles-2, scribbled-2, scribbling-2,

      semantic-2, semantics-2,

      sense-2,senses-3,

      separate-1,

      sequence-2,sequences-2, sequencing-2,

      set-1, sets-1,

      short-1, shorter-1, shortest-2,

      signify-2, signifies-2, signification-3,

      simple-2,simpler-1,simplest-2,

      single-2,

      spanish-1,

      speak-1,speaking-1,speech-1,spoken-1,

      sprawling-1,

      starts-1,

      statement-2,statements-1,

      stop-1,

      story-1,stories-2,

      stress-1,stressed-1, unstressed-1,

      strike-1,

      string-2,strings-2,

      structure-2,

      style-1,

      subject-1,

      substitute-1,

      syllable-2, syllables-3,

      syntax-2, syntactic-2,

      teach-1,teaching-1, taught-1,teaches-1,

      tense-2,tenses-3,

      text-1,texts-1,

      theory-1,

      thought-1,thoughts-1,

      topic-2,topics-2,

      translate-2,translates-2,translated-2, translation-2, translations-2,translatable-2,

      truth-2,

      turn-1,

      typed-2, typing-2,

      understand-1,understands-1, understood-1, understanding-1,understandable-1,

      utter-2,utters-2,uttered-2,uttering-2,

      verb-3,verbs-3,

      verse-1,verses-1,

      vocabulary-2,

      voice-1,

      warning-1,

      whole-2,

      wonky-1,

      word-2,wording-3,words-3,worded-3,

      write-1,wrote-1,writes-1,writing-1,written-2, writer-2, writers-2]).

 

 

lawpat([under,sentence,of],10).

lawpat([life, imprisonment],10).

lawpat([sentences, of,death],10).

lawpat([passes,sentence,on],10).

lawpat([pass,sentence,on],10).

lawpat([passing,sentence,on],10).

lawpat([passed,sentence,on],10).

lawpat([pronounce,sentence],10).

lawpat([pronounced, sentence],10).

lawpat([pronouncing,sentence],10).

lawpat([pronounces, sentence],10).

lawpat([death,sentence],10).

lawpat([sentence, of, death],10).

lawpat([death,sentences],10).

 

 

lingpat([sentence,connector],10).

lingpat([sentence,stress],10).

lingpat([sentence, structure],10).

lingpat([sentence, connectors],10).

lingpat([a, few,sentences],10).

lingpat([of, the,sentence],5).

lingpat([the, end, of, the, sentence],10).

lingpat([following, sentences],10).

lingpat([following, sentence],10).

lingpat([sentences, such, as],10).

lingpat([of, the, sentences],5).

lingpat([in, the, sentence],5).

 

 

 

 

% UTIL

%%%%%%

 

 

total([],0).

total([Tete|Queue],Somme) :-

        total(Queue,Nouvelle_Somme),

        Somme is Nouvelle_Somme+Tete.

 

 

totalpat([],0).

totalpat([W-P|Queue],Somme) :-

        totalpat(Queue,Nouvelle_Somme),

        Somme is Nouvelle_Somme+W.

 

        

sublist(S,L) :- suffix(L,Suf),prefix(S,Suf).

prefix(Prefix,Liste) :- append(Prefix,Reste,Liste).

suffix(Liste,Suffixe) :- append(Prem,Suffixe, Liste).

       

 

 

 

%% GET_SENTENCE

%%%%%%%%%%%%%%%

 

 

/* standard string to word list converter */

 

 

getsentence(Wordlist,Handle) :- get0(Handle,Char),

                                     % ifthenelse(Char=37,Wordlist=[next],true),

                                     getrest(Handle,Char,Wordlist).

 

 

getrest(Handle,46,[]):-!.        /* period */

getrest(Handle,63,[]):-!.       /* ? */

 

getrest(Handle,32,Wordlist) :- !,getsentence(Wordlist,Handle).

% getrest(Handle,39,Wordlist) :- !,getsentence(Wordlist,Handle).

 

getrest(Handle,45,Wordlist) :- !,getsentence(Wordlist,Handle).

     

 

getrest(Handle,44,Wordlist) :- !,getsentence(Wordlist,Handle).

    

 

getrest(Handle,Letter,[Word|Wordlist]):-

             getletters(Handle,Letter,Letters,Nextchar),

             name(Word,Letters),

             getrest(Handle,Nextchar,Wordlist).

 

 

getletters(H,46,[],46):-!.   /* period */

getletters(H,32,[],32):-!.   /* space */

getletters(H,44,[],44):-!.   /* comma, making up a word on its own */ 

getletters(H,45,[],45):-!.   /* '-' used as word delimiter:

                                   est-ce que ... */

% getletters(H,39,[],39):-!. 

                        /* apostrophe : used as a word delimiter */

getletters(H,63,[],63):-!.  /* ? */

 

getletters(Handle,Let,[Let1|Letters],Nextchar):-

                 transform(Let,Let1),!,

                 get0(Handle,Char),

                 getletters(Handle,Char,Letters,Nextchar).

 

getletters(Handle,Let,Letters,Nextchar):-

                 get0(Handle,Char),

                 getletters(Handle,Char,Letters,Nextchar).

 

 

transform(C,C1):-C>64,C<91,!,C1 is C+32.  % no capital letters

 

transform(C,C) :- C>31. /* chucks non-printing chars out */

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%