import com.mongodb.*;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

public class Phrases {

    final static String MongoUri = "mongodb://localhost:27030/";
    final static String Database = "Reviews";
    final static String Reviews = "Reviews";
    final static String Phrases = "Phrases";
    MongoClient mongoClient;
    DB database;
    DBCollection reviewsCollection, phrasesCollection;

    public Phrases() {
        try {
            mongoClient = new MongoClient(new MongoClientURI(MongoUri));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        database = mongoClient.getDB(Database);
        reviewsCollection = database.getCollection(Reviews);
        phrasesCollection = database.getCollection(Phrases);
    }

    public static void main(String[] args) {
        Phrases phrases = new Phrases();
        phrases.run();
    }

    public void run() {
        DBCursor cursor = reviewsCollection.find();
        Extract extractor = new Extract();
        try {
            while (cursor.hasNext()) {
                DBObject dbItem = cursor.next();
                String text = dbItem.get("text").toString();
                String reviewId = dbItem.get("reviewId").toString();
                String business = dbItem.get("business").toString();

                List<Pattern> phrases = extractor.run(text);
                Review review = new Review(text, reviewId, business);
                savePhrases(review, phrases);
            }
        } finally {
            cursor.close();
        }
    }

    private void savePhrases(Review review, List<Pattern> patterns) {
        List<String> phrases = new ArrayList<String>();
        for (Pattern pattern : patterns) {
            phrases.add(pattern.toAspect());
        }
        ReviewPhrases result = new ReviewPhrases(review, phrases);
        phrasesCollection.insert(result.toDBObject());
    }
}

@@ -227,24 +227,28 @@
 Database = %22
+Yelp
 Reviews%22;%0A  
@@ -282,25 +282,24 @@
 ws = %22Review
-s
 %22;%0A    final
@@ -907,22 +907,124 @@
     
-phrases.run();
+try %7B%0A            phrases.run();%0A        %7D%0A        catch(Exception exception) %7B%0A            phrases.run();%0A        %7D
 %0A   
@@ -1099,24 +1099,60 @@
 ion.find();%0A
+        int count = cursor.count();%0A
         Extr
@@ -1178,24 +1178,46 @@
  Extract();%0A
+        int done = 0;%0A
         try 
@@ -1348,17 +1348,17 @@
 em.get(%22
-t
+T
 ext%22).to
@@ -1413,17 +1413,17 @@
 em.get(%22
-r
+R
 eviewId%22
@@ -1482,17 +1482,17 @@
 em.get(%22
-b
+B
 usiness%22
@@ -1506,16 +1506,78 @@
 ing();%0A%0A
+                if (!alreadyExtractedPhrases(reviewId)) %7B%0A    
         
@@ -1629,16 +1629,20 @@
 (text);%0A
+    
         
@@ -1719,16 +1719,20 @@
         
+    
 savePhra
@@ -1769,67 +1769,345 @@
     
-%7D%0A        %7D finally %7B%0A            cursor.close();%0A        %7D
+    %7D%0A%0A                done++;%0A                System.out.println(%22Done %22 + done + %22/%22 + count);%0A            %7D%0A        %7D finally %7B%0A            cursor.close();%0A        %7D%0A    %7D%0A%0A    private boolean alreadyExtractedPhrases(String reviewId) %7B%0A        return phrasesCollection.find(new BasicDBObject(%22review.reviewId%22, reviewId)).count() %3E 0;
 %0A   

make it run nicely