Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject append.

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:course.BlogPostDAO.java

License:Apache License

public void addPostComment(final String name, final String email, final String body, final String permalink) {

    // XXX HW 3.3, Work Here
    // Hints://from w  w w  . j  a v a  2 s .  c  om
    // - email is optional and may come in NULL. Check for that.
    // - best solution uses an update command to the database and a suitable
    //   operator to append the comment on to any existing list of comments

    BasicDBObject comment = new BasicDBObject("author", name).append("body", body);
    if (email != null && !email.equals("")) {
        comment.append("email", email);
    }
    postsCollection.update(new BasicDBObject("permalink", permalink),
            new BasicDBObject("$push", new BasicDBObject("comments", comment)));
}

From source file:course.week3.BlogPostDAO.java

License:Apache License

public void addPostComment(final String name, final String email, final String body, final String permalink) {

    // XXX HW 3.3, Work Here
    // Hints:/*from www  . j a v a  2  s  .c o  m*/
    // - email is optional and may come in NULL. Check for that.
    // - best solution uses an update command to the database and a suitable
    //   operator to append the comment on to any existing list of comments
    BasicDBObject object = new BasicDBObject("author", name).append("body", body);
    if (email != null && !email.isEmpty()) {
        object.append("email", email);
    }

    DBObject query = QueryBuilder.start("permalink").is(permalink).get();
    DBObject update = new BasicDBObject("$push", new BasicDBObject("comments", object));

    postsCollection.update(query, update);

}

From source file:crawltweets2mongo.MonoThread.java

void getNewTweets(String keyword) {
    try {//from w  w  w  .  j  a  va2s . co  m
        Query query = new Query(keyword);
        query.setCount(20000);
        QueryResult result;
        result = twitter.search(query);

        //System.out.println("Getting Tweets..by Key.");
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_Rname", tweet.getUser().getName());
            basicObj.put("user_name", tweet.getUser().getScreenName());
            basicObj.put("retweet_count", tweet.getRetweetCount());
            basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());

            UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", tweet.getId());
            basicObj.put("tweet_text", tweet.getText());
            Status temp1 = tweet.getRetweetedStatus();
            if (temp1 != null)
                basicObj.put("Re_tweet_ID", temp1.getUser().getId());
            GeoLocation loc = tweet.getGeoLocation();
            if (loc != null) {
                basicObj.put("Latitude", loc.getLatitude());
                basicObj.put("Longitude", loc.getLongitude());
            }
            basicObj.put("CreateTime", tweet.getCreatedAt());
            basicObj.put("FavoriteCount", tweet.getFavoriteCount());
            basicObj.put("user_Id", tweet.getUser().getId());

            if (tweet.getUser().getTimeZone() != null)
                basicObj.put("UsertimeZone", tweet.getUser().getTimeZone());
            if (tweet.getUser().getStatus() != null)
                basicObj.put("UserStatus", tweet.getUser().getStatus());
            //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates());
            String U_Loc = tweet.getUser().getLocation();
            if (U_Loc != null)
                basicObj.put("userLocation", U_Loc);
            basicObj.put("number_of_rt", tweet.getRetweetCount());

            if (mentioned.length > 0) {
                basicObj.append("mentions", pickMentions(mentioned));
            }
            try {

                collection.insert(basicObj);
            } catch (Exception e) {
                //System.out.println("MongoDB Connection Error : " + e.getMessage());
                //                            loadMenu();
            }
        }
        collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true));
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:crawltweets2mongo.MonoThread.java

void getNewTweets(GeoLocation myLoc, double radius) {
    try {/*from w  w w .j  a  v  a2 s.  c o m*/
        Query query = new Query();
        Query.Unit unit = Query.KILOMETERS; // or Query.MILES;
        query.setGeoCode(myLoc, radius, unit);
        if (radius > 200)
            query.setCount(20000);
        else
            query.setCount(20000);

        QueryResult result;
        result = this.twitter.search(query);

        //System.out.println("Getting Tweets. by Geo..");
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_Rname", tweet.getUser().getName());
            basicObj.put("user_name", tweet.getUser().getScreenName());
            basicObj.put("retweet_count", tweet.getRetweetCount());
            basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());

            UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", tweet.getId());
            basicObj.put("tweet_text", tweet.getText());
            Status temp1 = tweet.getRetweetedStatus();
            if (temp1 != null)
                basicObj.put("Re_tweet_ID", temp1.getUser().getId());
            GeoLocation loc = tweet.getGeoLocation();
            if (loc != null) {
                basicObj.put("Latitude", loc.getLatitude());
                basicObj.put("Longitude", loc.getLongitude());
            }
            basicObj.put("CreateTime", tweet.getCreatedAt());
            basicObj.put("FavoriteCount", tweet.getFavoriteCount());
            basicObj.put("user_Id", tweet.getUser().getId());

            if (tweet.getUser().getTimeZone() != null)
                basicObj.put("UsertimeZone", tweet.getUser().getTimeZone());
            if (tweet.getUser().getStatus() != null)
                basicObj.put("UserStatus", tweet.getUser().getStatus());
            //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates());
            String U_Loc = tweet.getUser().getLocation();
            if (U_Loc != null)
                basicObj.put("userLocation", U_Loc);
            basicObj.put("number_of_rt", tweet.getRetweetCount());
            //basicObj.put("isRetweet", tweet.getPlace().getGeometryCoordinates());                
            //basicObj.put("POS", tweet.getWithheldInCountries());

            if (mentioned.length > 0) {
                basicObj.append("mentions", pickMentions(mentioned));
            }
            try {
                //items.insert(basicObj);
                collection.insert(basicObj);
            } catch (Exception e) {
                //     System.out.println("MongoDB Connection Error : " + e.getMessage());
                //                            loadMenu();
            }
        }
        collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true));
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:cz.vse.fis.keg.entityclassifier.core.THDWorker.java

License:Open Source License

public String getInterlanguageLink(String entityTitle, String langOrig, String langTo) {
    try {/*from  w w w .  j  av  a 2 s  .c o  m*/
        BasicDBObject queryObj = new BasicDBObject();
        switch (langOrig) {
        case "en":
            queryObj.append("en_uri", "http://dbpedia.org/resource/" + entityTitle.replace(" ", "_"));
            break;

        case "de":
            queryObj.append("de_uri", "http://de.dbpedia.org/resource/" + entityTitle.replace(" ", "_"));
            break;

        case "nl":
            queryObj.append("nl_uri", "http://nl.dbpedia.org/resource/" + entityTitle.replace(" ", "_"));
            break;

        }
        BasicDBObject projObj = new BasicDBObject();
        switch (langTo) {
        case "en":
            projObj.append("en_uri", 1);
            break;

        case "de":
            projObj.append("de_uri", 1);
            break;

        case "nl":
            projObj.append("nl_uri", 1);
            break;
        }

        DBObject resObj = MongoDBClient.getDBInstance().getCollection("interlanguage_links").findOne(queryObj,
                projObj);
        String otherEntityTitle = null;
        if (resObj != null) {
            switch (langTo) {
            case "en":
                Object temp = resObj.get("en_uri");
                if (temp != null) {
                    otherEntityTitle = resObj.get("en_uri").toString();
                }
                break;

            case "de":
                otherEntityTitle = resObj.get("de_uri").toString();
                break;

            case "nl":
                otherEntityTitle = resObj.get("nl_uri").toString();
                break;
            }
        }
        if (otherEntityTitle != null) {
            otherEntityTitle = otherEntityTitle
                    .substring(otherEntityTitle.lastIndexOf('/') + 1, otherEntityTitle.length())
                    .replace("_", " ");
        }
        return otherEntityTitle;

    } catch (UnknownHostException ex) {
        Logger.getLogger(THDWorker.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:cz.vse.fis.keg.entityclassifier.core.THDWorker.java

License:Open Source License

public HashSet getTHDHypernymsLHDv2(String entityTitle, String lang) throws UnknownHostException {
    HashSet hypernymsList = new HashSet();
    try {//from   w ww  .  j a v  a 2s. c  om
        BasicDBObject queryObj = new BasicDBObject();
        queryObj.append("types",
                new BasicDBObject().append("$elemMatch", new BasicDBObject().append("origin", "thd")));

        switch (lang) {
        case "en":
            String encodedURIEn = "http://dbpedia.org/resource/"
                    + URLEncoder.encode(URLEncoder.encode(entityTitle.replace(" ", "_"), "UTF-8"), "UTF-8");
            String notEncodedURIEn = "http://dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", encodedURIEn);

            DBObject resObj = MongoDBClient.getDBInstance().getCollection("en_entities_thd_lhd10")
                    .findOne(queryObj);

            if (resObj != null) {

                BasicDBList e = (BasicDBList) resObj.get("types");

                for (int i = 0; i < e.size(); i++) {

                    DBObject type = (DBObject) e.get(i);

                    String mappingStr = type.get("mapping").toString();

                    // hypernym is mapped to dbpedia ontology
                    // creating hierarchy from the dbpedia ontology
                    if (mappingStr.equals("dbOnto")) {

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(notEncodedURIEn);
                        hypernym.setType(type.get("label").toString());
                        hypernym.setTypeURL(type.get("uri").toString());
                        hypernym.setOrigin(type.get("origin").toString());
                        hypernym.setAccuracy(type.get("accuracy").toString());
                        hypernym.setBounds(type.get("bounds").toString());
                        hypernymsList.add(hypernym);

                        OntoRecord initRecord = new OntoRecord();
                        initRecord.setUri(type.get("uri").toString());

                        while (initRecord != null) {

                            initRecord = DBpediaOntologyManager.getInstance().getSubclass(initRecord.getUri(),
                                    lang);

                            if (initRecord == null) {
                                return hypernymsList;
                            } else {
                                Hypernym hypernymDerived = new Hypernym();
                                hypernymDerived.setEntity(entityTitle);
                                hypernymDerived.setEntityURL(notEncodedURIEn);
                                hypernymDerived.setType(initRecord.getLabel());
                                hypernymDerived.setTypeURL(initRecord.getUri());
                                hypernymDerived.setOrigin("thd-derived");
                                hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                hypernymDerived.setBounds(type.get("bounds").toString());
                                hypernymsList.add(hypernymDerived);
                            }
                        }
                    }
                    // the type is DBpedia instance, doesn't matter, add it it to the types list
                    else {
                        Hypernym hypernymInst = new Hypernym();
                        hypernymInst.setEntity(entityTitle);
                        hypernymInst.setEntityURL(notEncodedURIEn);
                        hypernymInst.setType(type.get("label").toString());
                        hypernymInst.setTypeURL(type.get("uri").toString());
                        hypernymInst.setOrigin(type.get("origin").toString());
                        hypernymInst.setAccuracy(type.get("accuracy").toString());
                        hypernymInst.setBounds(type.get("bounds").toString());
                        hypernymsList.add(hypernymInst);

                        // try to map the DBpedia instance type to a DBpedia Ontology type
                        String mappedType = TypeMapper.getInstance().getTypeMapping(lang,
                                type.get("uri").toString());

                        if (mappedType != null) {

                            Hypernym hypernym2 = new Hypernym();
                            hypernym2.setEntity(entityTitle);
                            hypernym2.setEntityURL(notEncodedURIEn);
                            hypernym2.setType(mappedType.split("/")[mappedType.split("/").length - 1]);
                            hypernym2.setTypeURL(mappedType);
                            hypernym2.setOrigin(type.get("origin").toString());
                            hypernym2.setAccuracy(type.get("accuracy").toString());
                            hypernym2.setBounds(type.get("bounds").toString());
                            hypernymsList.add(hypernym2);

                            OntoRecord initRecord = new OntoRecord();
                            initRecord.setUri(mappedType);

                            while (initRecord != null) {

                                initRecord = DBpediaOntologyManager.getInstance()
                                        .getSubclass(initRecord.getUri(), lang);

                                if (initRecord == null) {
                                    return hypernymsList;
                                } else {
                                    Hypernym hypernymDerived = new Hypernym();
                                    hypernymDerived.setEntity(entityTitle);
                                    hypernymDerived.setEntityURL(notEncodedURIEn);
                                    hypernymDerived.setType(initRecord.getLabel());
                                    hypernymDerived.setTypeURL(initRecord.getUri());
                                    hypernymDerived.setOrigin("thd-derived");
                                    hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                    hypernymDerived.setBounds(type.get("bounds").toString());
                                    hypernymsList.add(hypernymDerived);
                                }
                            }
                        }
                    }
                }
            }
            return hypernymsList;

        case "de":

            String encodedURIDe = "http://de.dbpedia.org/resource/"
                    + URLEncoder.encode(entityTitle.replace(" ", "_"), "UTF-8");
            String notEncodedURIDe = "http://de.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", encodedURIDe);

            resObj = MongoDBClient.getDBInstance().getCollection("de_entities_thd_lhd10").findOne(queryObj);

            if (resObj != null) {

                BasicDBList typesList = (BasicDBList) resObj.get("types");

                for (int i = 0; i < typesList.size(); i++) {

                    DBObject type = (DBObject) typesList.get(i);

                    String mappingStr = type.get("mapping").toString();
                    // hypernym is mapped to dbpedia ontology
                    // creating hierarchy from the dbpedia ontology
                    if (mappingStr.equals("dbOnto")) {

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(notEncodedURIDe);
                        hypernym.setType(type.get("label").toString());
                        hypernym.setTypeURL(type.get("uri").toString());
                        hypernym.setAccuracy(type.get("accuracy").toString());
                        hypernym.setBounds(type.get("bounds").toString());
                        hypernym.setOrigin(type.get("origin").toString());
                        hypernymsList.add(hypernym);

                        OntoRecord initRecord = new OntoRecord();
                        initRecord.setUri(type.get("uri").toString());

                        while (initRecord != null) {

                            initRecord = DBpediaOntologyManager.getInstance().getSubclass(initRecord.getUri(),
                                    lang);

                            if (initRecord != null) {

                                Hypernym hypernymDerived = new Hypernym();
                                hypernymDerived.setEntity(entityTitle);
                                hypernymDerived.setEntityURL(notEncodedURIDe);
                                hypernymDerived.setType(initRecord.getLabel());
                                hypernymDerived.setTypeURL(initRecord.getUri());
                                hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                hypernymDerived.setOrigin("thd-derived");
                                hypernymsList.add(hypernymDerived);
                            }
                        }
                    }
                    // type is DBpedia instance, doesn't matter, add it to the types list
                    else {
                        Hypernym hypernymInst = new Hypernym();
                        hypernymInst.setEntity(entityTitle);
                        hypernymInst.setEntityURL(notEncodedURIDe);
                        hypernymInst.setType(type.get("label").toString());
                        hypernymInst.setTypeURL(type.get("uri").toString());
                        hypernymInst.setOrigin(type.get("origin").toString());
                        hypernymInst.setAccuracy(type.get("accuracy").toString());
                        hypernymInst.setBounds(type.get("bounds").toString());
                        hypernymsList.add(hypernymInst);

                        // try to map the DBpedia instance type to a DBpedia Ontology type
                        String mappedType = TypeMapper.getInstance().getTypeMapping("de",
                                type.get("uri").toString());

                        if (mappedType != null) {
                            Hypernym hypernym2 = new Hypernym();
                            hypernym2.setEntity(entityTitle);
                            hypernym2.setEntityURL(notEncodedURIDe);
                            hypernym2.setType(mappedType.split("/")[mappedType.split("/").length - 1]);
                            hypernym2.setTypeURL(mappedType);
                            hypernym2.setOrigin(type.get("origin").toString());
                            hypernym2.setAccuracy(type.get("accuracy").toString());
                            hypernym2.setBounds(type.get("bounds").toString());
                            hypernymsList.add(hypernym2);

                            OntoRecord initRecord = new OntoRecord();
                            initRecord.setUri(mappedType);

                            while (initRecord != null) {

                                initRecord = DBpediaOntologyManager.getInstance()
                                        .getSubclass(initRecord.getUri(), lang);

                                if (initRecord == null) {
                                    return hypernymsList;
                                } else {
                                    Hypernym hypernymDerived = new Hypernym();
                                    hypernymDerived.setEntity(entityTitle);
                                    hypernymDerived.setEntityURL(notEncodedURIDe);
                                    hypernymDerived.setType(initRecord.getLabel());
                                    hypernymDerived.setTypeURL(initRecord.getUri());
                                    hypernymDerived.setOrigin("thd-derived");
                                    hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                    hypernymDerived.setBounds(type.get("bounds").toString());
                                    hypernymsList.add(hypernymDerived);
                                }
                            }
                        }
                    }
                }
            }
            return hypernymsList;

        case "nl":

            String encodedURINl = "http://nl.dbpedia.org/resource/"
                    + URLEncoder.encode(entityTitle.replace(" ", "_"), "UTF-8");
            String notEncodedURINl = "http://nl.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", encodedURINl);

            resObj = MongoDBClient.getDBInstance().getCollection("nl_entities_thd_lhd10").findOne(queryObj);

            if (resObj != null) {

                BasicDBList typesList = (BasicDBList) resObj.get("types");

                for (int i = 0; i < typesList.size(); i++) {

                    DBObject type = (DBObject) typesList.get(i);

                    String typeURI = type.get("mapping").toString();
                    // hypernym is mapped to dbpedia ontology
                    // creating hierarchy from the dbpedia ontology
                    if (typeURI.equals("dbOnto")) {

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(notEncodedURINl);
                        hypernym.setType(type.get("label").toString());
                        hypernym.setTypeURL(type.get("uri").toString());
                        hypernym.setAccuracy(type.get("accuracy").toString());
                        hypernym.setBounds(type.get("bounds").toString());
                        hypernym.setOrigin(type.get("origin").toString());
                        hypernymsList.add(hypernym);

                        OntoRecord initRecord = new OntoRecord();
                        initRecord.setUri(type.get("uri").toString());

                        while (initRecord != null) {

                            initRecord = DBpediaOntologyManager.getInstance().getSubclass(initRecord.getUri(),
                                    lang);

                            if (initRecord != null) {

                                Hypernym hypernymDerived = new Hypernym();
                                hypernymDerived.setEntity(entityTitle);
                                hypernymDerived.setEntityURL(notEncodedURINl);
                                hypernymDerived.setType(initRecord.getLabel());
                                hypernymDerived.setTypeURL(initRecord.getUri());
                                hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                hypernymDerived.setOrigin("thd-derived");
                                hypernymsList.add(hypernymDerived);
                            }
                        }
                    }
                    // type is DBpedia instance, doesn't matter, add it to the types list
                    else {
                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(notEncodedURINl);
                        hypernym.setType(type.get("label").toString());
                        hypernym.setTypeURL(type.get("uri").toString());
                        hypernym.setOrigin(type.get("origin").toString());
                        hypernym.setAccuracy(type.get("accuracy").toString());
                        hypernym.setBounds(type.get("bounds").toString());
                        hypernymsList.add(hypernym);

                        // try to map the DBpedia instance type to a DBpedia Ontology type
                        String mappedType = TypeMapper.getInstance().getTypeMapping("nl",
                                type.get("uri").toString());

                        if (mappedType != null) {

                            Hypernym hypernym2 = new Hypernym();
                            hypernym2.setEntity(entityTitle);
                            hypernym2.setEntityURL(notEncodedURINl);
                            hypernym2.setType(mappedType.split("/")[mappedType.split("/").length - 1]);
                            hypernym2.setTypeURL(mappedType);
                            hypernym2.setOrigin(type.get("origin").toString());
                            hypernym2.setAccuracy(type.get("accuracy").toString());
                            hypernym2.setBounds(type.get("bounds").toString());
                            hypernymsList.add(hypernym2);

                            OntoRecord initRecord = new OntoRecord();
                            initRecord.setUri(mappedType);

                            while (initRecord != null) {

                                initRecord = DBpediaOntologyManager.getInstance()
                                        .getSubclass(initRecord.getUri(), lang);

                                if (initRecord == null) {
                                    return hypernymsList;
                                } else {
                                    Hypernym hypernymDerived = new Hypernym();
                                    hypernymDerived.setEntity(entityTitle);
                                    hypernymDerived.setEntityURL(notEncodedURINl);
                                    hypernymDerived.setType(initRecord.getLabel());
                                    hypernymDerived.setTypeURL(initRecord.getUri());
                                    hypernymDerived.setOrigin("thd-derived");
                                    hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                    hypernymDerived.setBounds(type.get("bounds").toString());
                                    hypernymsList.add(hypernymDerived);
                                }
                            }
                        }
                    }
                }
            }
            return hypernymsList;
        }
        return hypernymsList;
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(THDWorker.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hypernymsList;
}

From source file:cz.vse.fis.keg.entityclassifier.core.THDWorker.java

License:Open Source License

public HashSet getDBpediaHypernyms(String entityTitle, String lang) throws UnknownHostException {

    HashSet hypernymsList = new HashSet();
    try {/*from   w  w w.  j a  v a  2 s . co m*/
        BasicDBObject queryObj = new BasicDBObject();
        queryObj.append("types",
                new BasicDBObject().append("$elemMatch", new BasicDBObject().append("origin", "dbpedia")));

        BasicDBList dbTypesList;
        switch (lang) {
        case "en":
            String uriEn = "http://dbpedia.org/resource/"
                    + URLEncoder.encode(entityTitle.replace(" ", "_"), "UTF-8");
            queryObj.append("uri", uriEn);
            DBCursor cursorEN = MongoDBClient.getDBInstance().getCollection("en_entities_dbpedia")
                    .find(queryObj);

            while (cursorEN.hasNext()) {
                DBObject resObj = cursorEN.next();
                if (resObj != null) {

                    dbTypesList = (BasicDBList) resObj.get("types");

                    for (int i = 0; i < dbTypesList.size(); i++) {
                        DBObject obj = (DBObject) dbTypesList.get(i);

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(resObj.get("uri").toString());
                        hypernym.setType(obj.get("label").toString());
                        hypernym.setTypeURL(obj.get("uri").toString());
                        hypernym.setOrigin("dbpedia");
                        hypernym.setAccuracy("-1.0");
                        if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                            hypernymsList.add(hypernym);
                        }
                    }
                }
            }
            cursorEN.close();

            return hypernymsList;

        case "de":

            String uriDe = "http://de.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", uriDe);
            DBCursor cursorDE = MongoDBClient.getDBInstance().getCollection("de_entities_dbpedia")
                    .find(queryObj);

            while (cursorDE.hasNext()) {
                DBObject resObj = cursorDE.next();
                if (resObj != null) {
                    dbTypesList = (BasicDBList) resObj.get("types");

                    for (int i = 0; i < dbTypesList.size(); i++) {

                        DBObject obj = (DBObject) dbTypesList.get(i);

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(resObj.get("uri").toString());
                        hypernym.setType(obj.get("label").toString());
                        hypernym.setTypeURL(obj.get("uri").toString());
                        hypernym.setOrigin("dbpedia");
                        hypernym.setAccuracy("-1.0");
                        if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                            hypernymsList.add(hypernym);
                        }
                    }
                }
            }
            cursorDE.close();

            return hypernymsList;

        case "nl":

            String uriNl = "http://nl.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", uriNl);
            DBCursor cursorNL = MongoDBClient.getDBInstance().getCollection("nl_entities_dbpedia")
                    .find(queryObj);

            while (cursorNL.hasNext()) {
                DBObject resObj = cursorNL.next();
                if (resObj != null) {
                    dbTypesList = (BasicDBList) resObj.get("types");

                    for (int i = 0; i < dbTypesList.size(); i++) {

                        DBObject obj = (DBObject) dbTypesList.get(i);

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(resObj.get("uri").toString());
                        hypernym.setType(obj.get("label").toString());
                        hypernym.setTypeURL(obj.get("uri").toString());
                        hypernym.setOrigin("dbpedia");
                        hypernym.setAccuracy("-1.0");

                        if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                            hypernymsList.add(hypernym);
                        }
                    }
                }
            }
            cursorNL.close();

            return hypernymsList;

        default:
            return hypernymsList;
        }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(THDWorker.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hypernymsList;
}

From source file:cz.vse.fis.keg.entityclassifier.core.THDWorker.java

License:Open Source License

public HashSet getYAGOHypernyms(String entityTitle, String lang) throws UnknownHostException {

    HashSet hypernymsList = new HashSet();
    BasicDBObject queryObj = new BasicDBObject();

    switch (lang) {

    case "en":
        queryObj.append("uri", "http://yago-knowledge.org/resource/" + entityTitle.replaceAll(" ", "_"))
                .append("labels.lang", "en");
        DBObject resObj = MongoDBClient.getDBInstance().getCollection("entities_yago").findOne(queryObj);

        if (resObj != null) {
            hypernymsList = YagoOntologyManager.getInstance().getYagoHypernyms(entityTitle,
                    "http://yago-knowledge.org/resource/" + entityTitle.replaceAll(" ", "_"), "en", "yago");
        }/*from w  ww.j  a  v a 2 s .com*/
        return hypernymsList;

    case "de":
        queryObj = new BasicDBObject();
        queryObj.append("uri", "http://yago-knowledge.org/resource/" + entityTitle.replaceAll(" ", "_"))
                .append("labels.lang", "de");

        resObj = MongoDBClient.getDBInstance().getCollection("entities_yago").findOne(queryObj);

        if (resObj != null) {
            hypernymsList = YagoOntologyManager.getInstance().getYagoHypernyms(entityTitle,
                    resObj.get("uri").toString(), "de", "yago");
        }
        return hypernymsList;

    case "nl":
        break;
    }
    return hypernymsList;
}

From source file:cz.vse.fis.keg.entityclassifier.rest.authorizator.Authorizator.java

License:Open Source License

public RateBucket getConsumersRateLimits(String apikey) {

    RateBucket rate = new RateBucket();
    try {/*ww w.  java  2  s. c o m*/
        BasicDBObject queryObj = new BasicDBObject();
        queryObj.append("apikey", apikey);

        DBObject user = MongoDBClient.getClient().getDB(Settings.MONGODB_DATABASE_NAME).getCollection("users")
                .findOne(queryObj);

        if (user != null) {
            int limit = Integer.parseInt(user.get("limit").toString());
            int interval = Integer.parseInt(user.get("interval").toString());
            return getLimits(apikey, limit, interval, rate);
        } else {
            rate.setIsKeyValid(false);
            rate.setIsAuthorized(false);
            return rate;
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(Authorizator.class.getName()).log(Level.SEVERE, null, ex);
        rate.setIsKeyValid(false);
        rate.setIsAuthorized(false);
        return rate;
    }
}

From source file:cz.vse.fis.keg.entityclassifier.rest.authorizator.Authorizator.java

License:Open Source License

public RateBucket isAuthorized(String apikey) {
    RateBucket rate = new RateBucket();
    if (apikey.equals(adminkey)) {
        int limit = 999999;
        int interval = 999999;
        return logRequest(apikey, limit, interval, rate);
    }/*from w  w  w .  j  a  v  a 2s.c  om*/
    try {
        BasicDBObject queryObj = new BasicDBObject();
        queryObj.append("apikey", apikey);

        DBObject user = MongoDBClient.getClient().getDB(Settings.MONGODB_DATABASE_NAME).getCollection("users")
                .findOne(queryObj);

        if (user != null) {
            int limit = Integer.parseInt(user.get("limit").toString());
            int interval = Integer.parseInt(user.get("interval").toString());
            return logRequest(apikey, limit, interval, rate);
        } else {
            rate.setIsKeyValid(false);
            rate.setIsAuthorized(false);
            return rate;
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(Authorizator.class.getName()).log(Level.SEVERE, null, ex);
        rate.setIsKeyValid(false);
        rate.setIsAuthorized(false);
        return rate;
    }
}