Morphological analyzer SWI-Prolog example.


/* File:      Dict_Example.pl  -- morphological analyser example
** Author:    Sergey Sikorsky
** Contact:   sergey_sikorsky@yahoo.com
** Version:   1.0
**
** Copyright (C) Sergey Sikorsky 2000
**
** Dict_Example is free software; you can redistribute it and/or modify it under the
** terms of the GNU Library General Public License as published by the Free
** Software Foundation; either version 2 of the License, or (at your option)
** any later version.
**
** Dict_Example is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
** more details.
**
** You should have received a copy of the GNU Library General Public License
** along with this software; if not, write to the Free Software Foundation,
** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*/

do:-
	banner,
	load_foreign_library('mdswi.dll'),
	prolog_to_os_filename(OsFNameIn, "rus.tst"),
	( open(OsFNameIn, read, HandleIn), !
	; pr('File not found: ', OsFNameIn), fail	),
	(
		read_sentence(HandleIn, SentAtr),
		SentAtr \= [],
		pr('Original sentence: ', SentAtr), nl,
		prepare_sentence(SentAtr, L),
		pr('Prepared sentence: ', L), nl,
		fail
	;
		close(HandleIn)
	).

read_sentence(HandleIn, Result):-
	\+ at_end_of_stream(HandleIn),
	get0(HandleIn, C),
	r_sentence(HandleIn, C, Result).
read_sentence(HandleIn, Result):-
	\+ at_end_of_stream(HandleIn),
	read_sentence(HandleIn, Result).

r_sentence(HandleIn, CIn, [AWord|RestWords]):-
	remove_space(HandleIn, CIn, C),
	\+ eos(C, _),
	read_token(HandleIn, C, Word, COut), !,
	name(AWord, Word),
	r_sentence(HandleIn, COut, RestWords).
r_sentence(_, _, []).

read_token(HandleIn, CIn, Token, COut):-
	is_rus_char(CIn), !,
	read_rus_word(HandleIn, CIn, Token, COut).
read_token(HandleIn, 44, [99, 111, 109, 109, 97], C):- %% 'comma'
	\+ at_end_of_stream(HandleIn),
	get0(HandleIn, C).

read_rus_word(HandleIn, CIn, [NewC|RestC], COut):-
	rus_char(CIn, NewC), !,
	get0(HandleIn, C),
	read_rus_word(HandleIn, C, RestC, COut).
read_rus_word(_, CIn, [], CIn).

is_rus_char(C):-
	between(192, 255, C).

rus_char(C, NewC):-
	between(192, 223, C), !,
	NewC is C + 32.
rus_char(C, C):-
	between(224, 255, C), !.

eos(46, 46):-!. %% '.'
eos(33, 33):-!. %% '!'
eos(63, 63):-!. %% '?'
eos(-1, 46):-!. %% EOF

sign(44, [99, 111, 109, 109, 97]). %% 'comma'

remove_space(_, CIn, CIn):-
	is_rus_char(CIn), !.
remove_space(_, CIn, COut):-
	eos(CIn, COut), !.
remove_space(_, 44, 44):- !. %% 'comma'
remove_space(HandleIn, _, COut):-
	get0(HandleIn, C),
	remove_space(HandleIn, C, COut).

prepare_sentence([], []):-!.
prepare_sentence([comma|T], [comma|ResL]):- !,
	prepare_sentence(T, ResL).
%% Morphological dictionary
prepare_sentence([H|T], [wd(H, AtrL, _)|ResL]):-
	( morpho_rus(H, AtrL), !
	; ( pr("Word not found: ", H), fail)
	),
	prepare_sentence(T, ResL).


banner:-
	write_ln('Russian morphological dictionary demo.'),
	write_ln('Author: Sergey Sikorsky.'),
	nl.

pr(Str,Term):-
	write(Str), writeq(Term),nl.

My Home Page