/* sets the parts of speech for each node in the graph */ public void addPOS() { System.out.println("Running POS tagger..."); /* parameters for exec command */ File dir = new File("/home/josh/Desktop/Artificial_Intelligence/POS_Tagger"); /* where we are placing the file to parse */ File result = new File(file.toString() + ".pos"); int i = 1; String s = null, data = ""; try { /* command to run the POS tagger */ String command = "java -mx300m -classpath stanford-postagger.jar edu.stanford.nlp.tagger.maxent.MaxentTagger -model models/" + "bidirectional-distsim-wsj-0-18.tagger -textFile " + file.toString(); /* run the process */ Process p = Runtime.getRuntime().exec(command, null, dir); i = p.waitFor(); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); /* extract the data from the command */ while ((s = stdInput.readLine()) != null) { /* copy the data into one string */ data += s; } /* place the data in a .pos text file */ byte[] buffer = data.getBytes(); FileOutputStream fout = new FileOutputStream(result, false); fout.write(buffer); fout.close(); /* read any errors from the attempted command */ while ((s = stdError.readLine()) != null) { System.out.println(s); } } catch (Exception e) { System.out.println("Exception happened - here's what I know: "); e.printStackTrace(); System.exit(-1); } /* wait for the above process to complete */ while (i == 1) ; /* update our file to include the POS tags */ this.file = result; }
/* for a new file */ private void load(String data) { try { /* create a FileInputStream */ FileOutputStream fout = new FileOutputStream(file, false); /* add the timestamp to the data */ data = timestamp.toString() + SEPARATOR_TOKEN + data; /* read the data into a byte array */ fout.write(data.getBytes()); /* close the stream */ fout.close(); } catch (Exception e) { System.out.println("IO Error: " + e); } }
/* public methods */ public void add(String text) { try { /* create a FileOutputStream */ FileOutputStream fout = new FileOutputStream(file, true); // append /* write data to the file */ fout.write(text.getBytes()); /* close the data stream */ fout.close(); } catch (Exception e) { System.out.println("IO Error: " + e); } data += text; }
public static void writeImage(String fn, byte[] data, int width, int height) throws FileNotFoundException, IOException { if (data != null) { FileOutputStream fos = new FileOutputStream(fn); fos.write(new String("P6\n").getBytes()); fos.write(new String(width + " " + height + "\n").getBytes()); fos.write(new String("255\n").getBytes()); System.out.println(data.length); fos.write(data); fos.close(); } }
public static void main(String argv[]) throws Exception { RetsSession session = new RetsSession("http://demo.crt.realtors.org:6103/rets/login"); if (!session.Login("Joe", "Schmoe")) { System.out.println("Invalid login"); System.exit(2); } System.out.println("Action: " + session.GetAction()); RetsVersion version = session.GetDetectedRetsVersion(); System.out.println("RETS Version: " + ((version == RetsVersion.RETS_1_5) ? "1.5" : "1.0")); SearchRequest searchRequest = session.CreateSearchRequest("Property", "RES", "(ListPrice=300000-)"); searchRequest.SetSelect("ListingID,ListPrice,Beds,City"); searchRequest.SetLimit(SearchRequest.LIMIT_DEFAULT); searchRequest.SetOffset(SearchRequest.OFFSET_NONE); searchRequest.SetCountType(SearchRequest.CountType.RECORD_COUNT_AND_RESULTS); searchRequest.SetFormatType(SearchRequest.FormatType.COMPACT); searchRequest.SetStandardNames(true); try { File f = new File("rawsearch.xml"); FileOutputStream fop = new FileOutputStream(f); byte[] data = session.SearchAsArray(searchRequest); fop.write(data); fop.flush(); fop.close(); } catch (IOException e) { } LogoutResponse logout = session.Logout(); SearchResultSet results = new SearchResultSet(); // Reopen the file now for input try { File f = new File("rawsearch.xml"); byte[] buffer = new byte[(int) f.length()]; FileInputStream fip = new FileInputStream(f); int offset = 0; int numRead = 0; while (offset < buffer.length && (numRead = fip.read(buffer, offset, buffer.length - offset)) >= 0) offset += numRead; results.SetEncoding(EncodingType.RETS_XML_DEFAULT_ENCODING); results.SetDataAsArray(buffer); } catch (IOException e) { } System.out.println("Record count: " + results.GetCount()); StringVector columns = results.GetColumns(); while (results.HasNext()) { if (columns == null) { columns = results.GetColumns(); } for (int i = 0; i < columns.size(); i++) { System.out.format("%15s: %s\n", columns.get(i), results.GetString(columns.get(i))); } System.out.println(); } /* * Prototype for returning data in a stream. try { File f=new File("foobarty"); FileOutputStream fop=new FileOutputStream(f); CppInputStream data = session.SearchAsStream(searchRequest); byte [] buf = new byte[30]; int len; while ((len = data.read(buf, 0, 30)) > 0) { fop.write(buf, 0, len); } fop.flush(); fop.close(); } catch (IOException e) {} * end prototype */ }
/** Writes a char from file. */ public void writeChar(char _c) throws Exception { byte[] b = new byte[1]; b[0] = (byte) _c; fileOutput.write(b); }
/** Close the file we've writen. */ public void closeWrite() throws Exception { fileOutput.close(); }