001 // This file is part of the Attempto Java Packages. 002 // Copyright 2008-2009, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch). 003 // 004 // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the 005 // terms of the GNU Lesser General Public License as published by the Free Software Foundation, 006 // either version 3 of the License, or (at your option) any later version. 007 // 008 // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY 009 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 010 // PURPOSE. See the GNU Lesser General Public License for more details. 011 // 012 // You should have received a copy of the GNU Lesser General Public License along with the Attempto 013 // Java Packages. If not, see http://www.gnu.org/licenses/. 014 015 package ch.uzh.ifi.attempto.ape; 016 017 import java.io.BufferedReader; 018 import java.io.IOException; 019 import java.io.InputStreamReader; 020 import java.io.PrintWriter; 021 import java.net.Socket; 022 import java.net.UnknownHostException; 023 024 /** 025 * This class provides an interface to the Attempto Parsing Engine (APE) socket server. 026 * The socket server implementation is provided by <code>ape.exe</code>. To start 027 * a server, execute for example: 028 * 029 * <pre> 030 * ./ape.exe -server -port 2766 031 * </pre> 032 * 033 * @author Kaarel Kaljurand 034 */ 035 public class APESocket extends ACEParser { 036 037 private String host; 038 private int port; 039 040 /** 041 * Constructs a new parser object based on the hostname and the port number 042 * of the APE socket server. 043 * 044 * @param host The hostname of the socket server. 045 * @param port The port number of the socket server. 046 */ 047 public APESocket(String host, int port) { 048 this.host = host; 049 this.port = port; 050 } 051 052 /** 053 * Constructs a new parser object based on the APE socket server running on "localhost". 054 * 055 * @param port The port number of the socket server. 056 */ 057 public APESocket(int port) { 058 this("localhost", port); 059 } 060 061 public ACEParserResult getMultiOutput(String text, Lexicon lexicon, OutputType... outputTypes) { 062 String ulexStr = ""; 063 if (lexicon != null) { 064 ulexStr = ",ulextext=" + PrologUtils.escape(lexicon.toString()); 065 } 066 String paramList = "text=" + PrologUtils.escape(text) + ulexStr + getOptions(); 067 for (OutputType t : outputTypes) { 068 paramList = paramList + "," + t.toMultiFlag() + "=on"; 069 } 070 return new ACEParserResult(getParserResponseAsString(paramList)); 071 } 072 073 public String getSoloOutput(String text, Lexicon lexicon, OutputType outputType) throws ACEParserException { 074 String ulexStr = ""; 075 if (lexicon != null) { 076 ulexStr = ",ulextext=" + PrologUtils.escape(lexicon.toString()); 077 } 078 String paramList = "text=" + PrologUtils.escape(text) + ulexStr + ",solo=" + outputType.toSoloFlag() + getOptions(); 079 return checkForErrors(getParserResponseAsString(paramList)); 080 } 081 082 083 private String getParserResponseAsString(String paramList) { 084 Socket client; 085 try { 086 client = new Socket(host, port); 087 } catch (UnknownHostException e) { 088 throw new RuntimeException("Accessing APE socket failed: " + e.getMessage()); 089 } catch (IOException e) { 090 throw new RuntimeException("Accessing APE socket failed: " + e.getMessage()); 091 } 092 093 094 BufferedReader fromServer; 095 096 try { 097 fromServer = new BufferedReader(new InputStreamReader(client.getInputStream())); 098 } catch (IOException e) { 099 throw new RuntimeException("Accessing APE socket failed: " + e.getMessage()); 100 } 101 102 103 PrintWriter toServer; 104 try { 105 toServer = new PrintWriter(client.getOutputStream(), true); 106 } catch (IOException e) { 107 throw new RuntimeException("Accessing APE socket failed: " + e.getMessage()); 108 } 109 110 toServer.println("get([" + paramList + "])."); 111 112 String allLines = ""; 113 String responseLine; 114 try { 115 while ((responseLine = fromServer.readLine()) != null) { 116 if (responseLine.equals("APESERVERSTREAMEND")) { 117 break; 118 } 119 allLines = allLines + responseLine; 120 } 121 } catch (IOException e) { 122 throw new RuntimeException("Accessing APE socket failed: " + e.getMessage()); 123 } 124 try { 125 toServer.close(); 126 fromServer.close(); 127 client.close(); 128 } catch (IOException e) { 129 throw new RuntimeException("Accessing APE socket failed: " + e.getMessage()); 130 } 131 return allLines; 132 } 133 134 135 public static void main(String[] args) { 136 137 ACEParser parser = new APESocket(5000); 138 String response1 = null; 139 try { 140 response1 = parser.getSoloOutput("Every dog's friend is an an animal.", OutputType.PARAPHRASE1); 141 } catch (ACEParserException e) { 142 System.out.println(e.getMessageContainer().toString()); 143 } 144 145 if (response1 != null) { 146 System.out.println(response1); 147 } 148 149 //Lexicon lexicon = new Lexicon(); 150 //lexicon.addEntry(LexiconEntry.createNounSgEntry("dooog", "DOOOG", Gender.NEUTRAL)); 151 parser.setGuessingEnabled(true); 152 ACEParserResult response2 = parser.getMultiOutput("Every dooog's friend is an animal.", OutputType.PARAPHRASE1); 153 System.out.println(response2.get(OutputType.PARAPHRASE1)); 154 } 155 }