% % Simple Prolog client of the APE webservice. % Note: this program is specific to SWI-Prolog. % Also: http-libraries have to be installed. % % This example uses APE to parse a pseudo-Estonian sentence using a small user lexicon. % % @author Kaarel Kaljurand % @version 2009-05-20 % :- use_module(library('http/http_open')). host('attempto.ifi.uzh.ch'). port(80). path('/ws/ape/apews.perl'). ulextext(' adv(kiiresti, eng:fast). tv_finsg(kirjutab, sym:\'✍\'). adj_itr(kontrollitud, eng:controlled). noun_sg(keel, eng:language, neutr). noun_sg(robot, eng:robot, neutr). '). %% acetext_drs(+ACE:atom, -DRS:term, -Tokens:list) is det. % % Usage: % %== % ?- acetext_drs('A robot kirjutab a kontrollitud keel kiiresti. It kirjutab a keel.', DRS, Tokens). % % DRS = drs([_G6490, _G6493, _G6496, _G6499, _G6502], [object(_G6490, eng:robot, countable, na, eq, 1)-1, object(_G6493, ... % Tokens = [['A', robot, kirjutab, a, kontrollitud, keel, kiiresti, '.'], ['It', kirjutab, a, keel, '.']] ; % % No %== % % @param ACE is an ACE text % @param DRS is an Attempto DRS % @param Tokens is a list of sentences of tokens in the input text % acetext_drs(ACE, DRS, Tokens) :- host(Host), port(Port), path(Path), ulextext(Ulextext), parse_url(URL, [ protocol(http), host(Host), port(Port), path(Path), search([ ulextext = Ulextext, cdrs = on, ctokens = on, text = ACE ]) ]), http_open(URL, Stream, []), load_xml_file(Stream, XML), close(Stream), extract_term(XML, drs, DRS), extract_term(XML, tokens, Tokens). %% extract_term(+XML:term, -ElementName:atom, -TextContentAsTerm:term) is det. % % Parses the XML to extract the textual content of an element and convert it to a Prolog term. % Only the first matching element is processed. % % @param XML is SWI Prolog's represenation of XML data % @param ElementName is the name of the element to be extracted % @param TextContentAsTerm is the text content of the element (converted to Prolog term) % extract_term(XML, ElementName, TextContentAsTerm) :- get_element_textcontent(XML, ElementName, TextContentAsAtom), !, atom_to_term(TextContentAsAtom, TextContentAsTerm, _). %% get_element_textcontent(+XML:term, +ElementName:atom, -TextContent:atom) is nondet. % % @param XML is SWI Prolog's represenation of XML data % @param ElementName is the name of the element whose text content we want to retrieve % @param TextContent is the text content of the element (as an atom) % get_element_textcontent([element(ElementName, _Attributes, [TextContent]) | _], ElementName, TextContent). get_element_textcontent([element(_, _Attributes, Children) | _], ElementName, TextContent) :- get_element_textcontent(Children, ElementName, TextContent). get_element_textcontent([_ | Elements], ElementName, TextContent) :- get_element_textcontent(Elements, ElementName, TextContent).