/** * Add a file to the Lucene index (and generate a xref file) * * @param file The file to add * @param path The path to the file (from source root) * @throws java.io.IOException if an error occurs */ private void addFile(File file, String path) throws IOException { try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { FileAnalyzer fa = AnalyzerGuru.getAnalyzer(in, path); for (IndexChangedListener listener : listeners) { listener.fileAdd(path, fa.getClass().getSimpleName()); } fa.setCtags(ctags); fa.setProject(Project.getProject(path)); Document d; try { d = analyzerGuru.getDocument(file, in, path, fa); } catch (Exception e) { log.log( Level.INFO, "Skipped file ''{0}'' because the analyzer didn''t " + "understand it.", path); StringBuilder stack = new StringBuilder(); for (StackTraceElement ste : e.getStackTrace()) { stack.append(ste.toString()).append(System.lineSeparator()); } StringBuilder sstack = new StringBuilder(); for (Throwable t : e.getSuppressed()) { for (StackTraceElement ste : t.getStackTrace()) { sstack.append(ste.toString()).append(System.lineSeparator()); } } log.log( Level.FINE, "Exception from analyzer {0}: {1} {2}{3}{4}{5}{6}", new String[] { fa.getClass().getName(), e.toString(), System.lineSeparator(), stack.toString(), System.lineSeparator(), sstack.toString() }); return; } writer.addDocument(d, fa); Genre g = fa.getFactory().getGenre(); if (xrefDir != null && (g == Genre.PLAIN || g == Genre.XREFABLE)) { File xrefFile = new File(xrefDir, path); // If mkdirs() returns false, the failure is most likely // because the file already exists. But to check for the // file first and only add it if it doesn't exists would // only increase the file IO... if (!xrefFile.getParentFile().mkdirs()) { assert xrefFile.getParentFile().exists(); } fa.writeXref(xrefDir, path); } setDirty(); for (IndexChangedListener listener : listeners) { listener.fileAdded(path, fa.getClass().getSimpleName()); } } }
/** @param args */ public static void main(String[] args) { SingleUserAnalyse sua = new SingleUserAnalyse("胡新辰点点点", "usr.txt"); LuceneAnalyser ts = new LuceneAnalyser(); try { String semiFile = "C:\\Users\\Edward\\Desktop\\semi.txt"; String resultFile = "C:\\Users\\Edward\\Desktop\\result.txt"; // PrintWriter Pout = new PrintWriter(new FileWriter(semiFile)); // JSONObject semiData = sua.getIndexData(); // Pout.println(semiData.toString()); // Pout.close(); File input = new File(semiFile); JSONObject js = new JSONObject(new JSONTokener(new FileReader(input))); // js=sua.weightAdjust(js); JSONObject result = ts.getKeyWords(js); PrintWriter resultOut = new PrintWriter(new FileWriter(resultFile)); resultOut.println(result.toString()); resultOut.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO Auto-generated method stub }
@SuppressWarnings("finally") public JSONObject getKeyWords(JSONObject semiData) { JSONObject keyWords = new JSONObject(); try { this.buildIndex(semiData); this.getIndexInfo(this.indexDir, 4); this.generateWekaFile(this.termList, this.maxDocNum, this.wekaFile); JSONArray array = this.Cluster(this.wekaFile, 7); int totalNum = 0; for (int i = 0; i < array.length(); i++) { totalNum += array.getJSONArray(i).length(); } keyWords.put("maxFreq", this.maxFreq); keyWords.put("totalNum", totalNum); keyWords.put("WordList", array); } catch (WeiboException e) { System.out.print("getKeyWords++++++weibo\n"); System.out.print("error:" + e.getError() + "toString:" + e.toString()); keyWords.put("error", e.getError()); e.printStackTrace(); } catch (Exception e) { System.out.print("getKeyWords++++++Exception"); keyWords.put("error", e.toString()); e.printStackTrace(); } finally { try { this.myDelete(this.indexDir); this.myDelete(this.wekaFile); } catch (Exception e) { e.printStackTrace(); } return keyWords; } }
/** * Search a picture * * @param imageBytes * @param maxHits * @param feature * @param pictures * @return * @throws IOException */ public static List<LirePictureSortable> search( byte[] imageBytes, int maxHits, FeatureEnumerate feature, List<LirePictureSortable> pictures) throws IOException { File path = getPath(feature.getText()); log.debug("reading from indexed path " + path.getAbsolutePath()); List<LirePictureSortable> result = new ArrayList<>(); try { IndexReader ir = DirectoryReader.open(FSDirectory.open(path)); ImageSearcher searcher = new GenericFastImageSearcher(maxHits, feature.getValueClass()); // searching with a image file ... InputStream in = new ByteArrayInputStream(imageBytes); ImageSearchHits hits = searcher.search(in, ir); float score = 0.0F; for (int i = 0; i < hits.length(); i++) { score = hits.score(i); LirePictureSortable lp = new LirePictureSortable( UUID.fromString(hits.doc(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]), score, feature); /* check is its a picture was repeated by picture UUID */ if (pictures.contains(lp)) { lp = pictures.get(pictures.indexOf(lp)); lp.addDescriptor(feature, score); lp.addScore(score); } else { result.add(lp); } } } catch (Exception e) { e.printStackTrace(); } return result; }
/** * Index for each builder type * * @param image * @param picture_id * @param prefix * @param builder * @param conf * @throws IOException */ private static void luceneIndexer( BufferedImage image, UUID picture_id, String prefix, DocumentBuilder builder, IndexWriterConfig conf) throws IOException { File path = getPath(prefix); log.debug("creating indexed path " + path.getAbsolutePath()); IndexWriter iw = new IndexWriter(FSDirectory.open(path), conf); try { Document document = builder.createDocument(image, picture_id.toString()); iw.addDocument(document); } catch (Exception e) { System.err.println("Error reading image or indexing it."); e.printStackTrace(); } // closing the IndexWriter iw.close(); }