List of usage examples for org.apache.lucene.store RAMDirectory close
@Override public void close()
From source file:simseer.cosine.CosineSimilarity.java
License:Open Source License
/** * Calculates the cosine similarity between two documents. * @param d1 the first document//from w ww. ja va 2s . c o m * @param d2 the second document * @return the cosine similarity * @throws IOException */ private double getCosineSimilarity(String d1, String d2) throws IOException { RAMDirectory ramDir = new RAMDirectory(); //Index the full text of both documents IndexWriter writer = new IndexWriter(ramDir, new StandardAnalyzer(Version.LUCENE_36), true, IndexWriter.MaxFieldLength.UNLIMITED); Document doc = new Document(); doc.add(new Field("text", FileUtils.readFileToString(new File(d1), "UTF-8"), Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.YES)); writer.addDocument(doc); doc = new Document(); doc.add(new Field("text", FileUtils.readFileToString(new File(d2), "UTF-8"), Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.YES)); writer.addDocument(doc); writer.close(); //Build a term vector for each document IndexReader RAMreader = IndexReader.open(ramDir); TermFreqVector doc1FreqVector = RAMreader.getTermFreqVector(0, "text"); TermFreqVector doc2FreqVector = RAMreader.getTermFreqVector(1, "text"); RAMreader.close(); ramDir.close(); //Return the cosine similarity of the term vectors return cosineSimilarity(doc1FreqVector, doc2FreqVector); }
From source file:spell.SpellIndex.java
License:Apache License
/** * Index a Dictionary from either a file or another Lucene index. * @param dict the dictionary to index//w w w .j a v a 2 s . co m * @throws IOException */ public void indexDictionary(Dictionary dict) throws IOException { RAMDirectory ramdir = new RAMDirectory(); IndexWriter ramwriter = new IndexWriter(ramdir, s_analyzer, true); ramwriter.mergeFactor = 10000; Iterator iter = dict.getWordsIterator(); while (iter.hasNext()) { String word = (String) iter.next(); // Don't add existing words if (this.exist(word)) { // if the word already exist in the gramindex continue; } // add the word and index it if it's long enough Document doc = createDocument(word); ramwriter.addDocument(doc); } ramwriter.optimize(); IndexReader.unlock(spellindex); IndexWriter writer = new IndexWriter(spellindex, s_analyzer, false); writer.addIndexes(new Directory[] { ramdir }); writer.optimize(); writer.close(); ramwriter.close(); ramdir.close(); }
From source file:test.ad.date.Datetest.java
License:Open Source License
public static void main(String[] args) throws Exception { RAMDirectory dir = new RAMDirectory(); // FSDirectory dir = FSDirectory.open(new File("lt/")); IndexWriter writer = new IndexWriter(dir, new KeywordAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); List<Day> days = new ArrayList<Day>(); days.add(Day.All);// w w w.jav a 2s . co m List<State> states = new ArrayList<State>(); states.add(State.All); writer.addDocument(getDoc("b1", "0600", "0800", "all", "all", days, states)); writer.addDocument(getDoc("b2", "all", "all", "all", "all", days, states)); writer.addDocument(getDoc("b3", "0600", "0700", "all", "all", days, states)); writer.addDocument(getDoc("b4", "0500", "0700", "all", "all", days, states)); writer.addDocument(getDoc("b5", "0630", "0700", "all", "all", days, states)); writer.addDocument(getDoc("b6", "0800", "0900", "all", "all", days, states)); writer.optimize(); writer.close(); searcher = new IndexSearcher(dir); System.out.println("\n6Uhr"); searchTest("0600", "20101221", Day.All, 4); System.out.println("\n6:30Uhr"); searchTest("0630", null, Day.All, 5); System.out.println("\n5Uhr"); searchTest("0500", null, Day.All, 2); System.out.println("\n8:30Uhr"); searchTest("0830", null, Day.All, 2); dir.close(); }