Example usage for twitter4j HashtagEntity getText

List of usage examples for twitter4j HashtagEntity getText

Introduction

In this page you can find the example usage for twitter4j HashtagEntity getText.

Prototype

@Override
String getText();

Source Link

Document

Returns the text of the hashtag without #.

Usage

From source file:org.nsoft.openbus.model.Mensagem.java

License:Open Source License

public static Mensagem creteFromTwitterStatus(Status s) {
    try {/* w  ww .j a  va 2 s.  c o m*/
        Mensagem mensagem = new Mensagem();

        String text = s.getText();

        for (HashtagEntity h : s.getHashtagEntities()) {

            String subText = "<a href=\"twitter_search://do_search?search=" + h.getText() + "\">#" + h.getText()
                    + "</a>";
            text = text.replace("#" + h.getText(), subText);

        }

        for (UserMentionEntity u : s.getUserMentionEntities()) {

            String subText = "<a href=\"twitter_search_user://find_user?username=" + u.getScreenName() + "\">@"
                    + u.getScreenName() + "</a>";
            text = text.replace("@" + u.getScreenName(), subText);

        }

        mensagem.setAction(OpenTwitterStatusAction.getInstance());
        mensagem.addtions = createAddtions(s);
        mensagem.addtions.put("htmlText", text);
        mensagem.idMensagem = Long.toString(s.getId());
        mensagem.nome_usuario = s.getUser().getName();
        mensagem.mensagem = s.getText();
        mensagem.imagePath = new URL(s.getUser().getOriginalProfileImageURL());
        mensagem.data = s.getCreatedAt();
        mensagem.idUser = s.getUser().getId();
        mensagem.tipo = TIPO_STATUS;

        return mensagem;
    } catch (JSONException e) {
        return null;
    } catch (MalformedURLException e) {
        return null;
    }
}

From source file:org.richfaces.examples.tweetstream.dataserver.listeners.TweetStreamListener.java

License:Open Source License

private Tweet createTweet(Status status) {
    //create a new tweet from the status update
    Tweet tweet = new Tweet();
    tweet.setText(status.getText());/*from   w w w .  java2 s.c  o m*/
    tweet.setId(status.getUser().getId());
    tweet.setProfileImageUrl(status.getUser().getProfileImageURL().toString());
    tweet.setScreenName(status.getUser().getScreenName());
    if (status.getRetweetedStatus() != null) {
        tweet.setRetweet(status.getRetweetedStatus().isRetweet());
    }

    //process hashtags in the tweet
    HashtagEntity[] hashtagEntities = status.getHashtagEntities();
    if (hashtagEntities != null) {
        ArrayList<String> curTags = new ArrayList<String>(hashtagEntities.length);

        for (HashtagEntity tw4jTag : hashtagEntities) {
            curTags.add(tw4jTag.getText());
        }

        tweet.setHashTags(curTags.toArray(new String[0]));
    } else {
        tweet.setHashTags(new String[0]);
    }
    return tweet;
}

From source file:org.smarttechie.servlet.SimpleStream.java

public void getStream(TwitterStream twitterStream, String[] parametros, final Session session)//,PrintStream out)
{

    listener = new StatusListener() {

        @Override/*from w w  w.  java2s  .com*/
        public void onException(Exception arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatus(Status status) {
            Twitter twitter = new TwitterFactory().getInstance();
            User user = status.getUser();

            // gets Username
            String username = status.getUser().getScreenName();
            System.out.println("");
            String profileLocation = user.getLocation();
            System.out.println(profileLocation);
            long tweetId = status.getId();
            System.out.println(tweetId);
            String content = status.getText();
            System.out.println(content + "\n");

            JSONObject obj = new JSONObject();
            obj.put("User", status.getUser().getScreenName());
            obj.put("ProfileLocation", user.getLocation().replaceAll("'", "''"));
            obj.put("Id", status.getId());
            obj.put("UserId", status.getUser().getId());
            //obj.put("User", status.getUser());
            obj.put("Message", status.getText().replaceAll("'", "''"));
            obj.put("CreatedAt", status.getCreatedAt().toString());
            obj.put("CurrentUserRetweetId", status.getCurrentUserRetweetId());
            //Get user retweeteed
            String otheruser;
            try {
                if (status.getCurrentUserRetweetId() != -1) {
                    User user2 = twitter.showUser(status.getCurrentUserRetweetId());
                    otheruser = user2.getScreenName();
                    System.out.println("Other user: " + otheruser);
                }
            } catch (Exception ex) {
                System.out.println("ERROR: " + ex.getMessage().toString());
            }
            obj.put("IsRetweet", status.isRetweet());
            obj.put("IsRetweeted", status.isRetweeted());
            obj.put("IsFavorited", status.isFavorited());

            obj.put("InReplyToUserId", status.getInReplyToUserId());
            //In reply to
            obj.put("InReplyToScreenName", status.getInReplyToScreenName());

            obj.put("RetweetCount", status.getRetweetCount());
            if (status.getGeoLocation() != null) {
                obj.put("GeoLocationLatitude", status.getGeoLocation().getLatitude());
                obj.put("GeoLocationLongitude", status.getGeoLocation().getLongitude());
            }

            JSONArray listHashtags = new JSONArray();
            String hashtags = "";
            for (HashtagEntity entity : status.getHashtagEntities()) {
                listHashtags.add(entity.getText());
                hashtags += entity.getText() + ",";
            }

            if (!hashtags.isEmpty())
                obj.put("HashtagEntities", hashtags.substring(0, hashtags.length() - 1));

            if (status.getPlace() != null) {
                obj.put("PlaceCountry", status.getPlace().getCountry());
                obj.put("PlaceFullName", status.getPlace().getFullName());
            }

            obj.put("Source", status.getSource());
            obj.put("IsPossiblySensitive", status.isPossiblySensitive());
            obj.put("IsTruncated", status.isTruncated());

            if (status.getScopes() != null) {
                JSONArray listScopes = new JSONArray();
                String scopes = "";
                for (String scope : status.getScopes().getPlaceIds()) {
                    listScopes.add(scope);
                    scopes += scope + ",";
                }

                if (!scopes.isEmpty())
                    obj.put("Scopes", scopes.substring(0, scopes.length() - 1));
            }

            obj.put("QuotedStatusId", status.getQuotedStatusId());

            JSONArray list = new JSONArray();
            String contributors = "";
            for (long id : status.getContributors()) {
                list.add(id);
                contributors += id + ",";
            }

            if (!contributors.isEmpty())
                obj.put("Contributors", contributors.substring(0, contributors.length() - 1));

            System.out.println("" + obj.toJSONString());

            insertNodeNeo4j(obj);

            //out.println(obj.toJSONString());
            String statement = "INSERT INTO TweetsClassification JSON '" + obj.toJSONString() + "';";
            executeQuery(session, statement);
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStallWarning(StallWarning sw) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    };
    FilterQuery fq = new FilterQuery();

    fq.track(parametros);

    twitterStream.addListener(listener);
    twitterStream.filter(fq);
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

private void tagIOwithHashtags(InformationObject informationObject, HashtagEntity[] hashTags) {
    if (hashTags == null) {
        return;/*from   www.  j  a va2  s.  c o  m*/
    }

    for (HashtagEntity tag : hashTags) {
        informationObject.tag(tag.getText());
    }
}

From source file:org.wandora.application.tools.extractors.twitter.AbstractTwitterExtractor.java

License:Open Source License

public Topic reifyHashtagEntity(HashtagEntity e, TopicMap tm) {
    Topic entityTopic = null;//ww w . j av a2s . c om
    try {
        String str = e.getText();
        if (str != null && str.length() > 0) {
            String si = HASHTAG_SI_BODY + urlEncode(str);
            entityTopic = tm.getTopic(si);
            if (entityTopic == null) {
                entityTopic = tm.createTopic();
                entityTopic.addSubjectIdentifier(new Locator(si));
                entityTopic.setBaseName(str);
                entityTopic.setDisplayName(DEFAULT_LANG, str);
                entityTopic.addType(getHashtagType(tm));
            }
        }
    } catch (Exception ec) {
        log(ec);
    }
    return entityTopic;
}

From source file:org.xmlsh.twitter.util.TwitterWriter.java

License:BSD License

private void write(String localName, HashtagEntity e) throws XMLStreamException {
    startElement(localName);//  ww  w .  j a va 2  s  .  c  o  m
    attribute("start", e.getStart());
    attribute("end", e.getEnd());
    characters(e.getText());
    endElement();

}

From source file:storm.starter.spout.Q2FetchTweetSpout.java

License:Apache License

@Override
public void nextTuple() {
    //Utils.sleep(interval);
    Status ret = queue.poll();/* ww  w  .j a  v a  2  s.  c o  m*/
    if (ret == null) {
        Utils.sleep(10);
    } else {
        System.out.println("DEBUG\tFetchTweetSpout\t" + ret.getText());
        if (ret.getLang().equals("en")) {
            Date date = new Date();
            long currTime = date.getTime();
            ArrayList<String> hss = new ArrayList<String>();
            for (HashtagEntity htentity : ret.getHashtagEntities()) {
                hss.add(htentity.getText());
            }
            tweetInfoList.add(new TweetInfo(ret.getUser().getFriendsCount(), hss, ret.getText()));

            if ((currTime - timestamp) > interval) {
                timestamp = currTime;
                sequenceNumber++;
                _collector.emit(new Values(sequenceNumber, tweetInfoList));
                tweetInfoList.clear();
            }
        }
    }
}

From source file:storm.starter.spout.Q2SeqTwitterSpout.java

License:Apache License

@Override
public void nextTuple() {
    Status ret = queue.poll();//from  w  w  w.j a v  a2  s.  c o m
    if (ret == null) {
        Utils.sleep(50);
    } else {

        Date date = new Date();
        long currTime = date.getTime();
        ArrayList<String> hss = new ArrayList<String>();
        for (HashtagEntity htentity : ret.getHashtagEntities()) {
            hss.add(htentity.getText());
        }
        tweetInfoList.add(new TweetInfo(ret.getUser().getFriendsCount(), hss, ret.getText()));

        if ((currTime - timestamp) > interval) {
            if (tweetInfoList.size() > 0) {
                timestamp = currTime;
                sequenceNumber++;
                _collector.emit(new Values(sequenceNumber, tweetInfoList));
                tweetInfoList.clear();
            } else {
                System.out.println("DEBUG\tQ2SeqTweetSpout\tEmptyTupleFound\t" + ret.getText());
            }
        }
    }
}

From source file:twittermongodbapp.SaveTweets.java

static public void saveTweets(DB db, twitter4j.Twitter twitter) throws TwitterException {
    DBCollection collection = db.getCollection("collection2");
    DBCollection savedCollection = db.getCollection("savedCollection");
    DBCollection ConnectionCollection = db.getCollection(" ConnectionCollection");
    BasicDBObject document0 = new BasicDBObject();
    savedCollection.remove(document0);/*w  w  w  .  j a va2 s  .c  o m*/
    ConnectionCollection.remove(document0);
    Status savedTweet;

    //Read Tweets from Database
    System.out.println("Saved tweets: ");
    DBCursor cursor = collection.find();
    List<String> allUsers = new ArrayList<>();
    int count = 1;

    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        savedTweet = TwitterObjectFactory.createStatus(obj.toString());

        String name = savedTweet.getUser().getScreenName();
        String date = savedTweet.getCreatedAt().toString();
        boolean exist = false;

        BasicDBObject document = new BasicDBObject("User", name);
        BasicDBObject searchDocument = new BasicDBObject("User", name);
        DBObject theObj = null;

        if (!allUsers.contains(name)) {
            allUsers.add(name);
        } else {
            exist = true;
            DBCursor cursor2 = savedCollection.find(searchDocument);
            if (cursor2.hasNext()) {
                theObj = cursor2.next();
            }

        }

        // BasicDBList allHashtags = new BasicDBList();
        List<String> allHashtags = new ArrayList<>();
        List<String> allsortURLS = new ArrayList<>();
        List<String> allcompleteURLS = new ArrayList<>();
        //BasicDBList allURLS = new BasicDBList();
        List<String> allmentionedUsers = new ArrayList<>();
        List<String> allTweets = new ArrayList<>();

        if (exist) {
            allHashtags = (ArrayList) theObj.get("Hashtags");
        }
        HashtagEntity[] hashtagsEntities = savedTweet.getHashtagEntities();
        for (HashtagEntity hashtagsEntitie : hashtagsEntities) {
            if (!allHashtags.contains(hashtagsEntitie.getText())) {
                allHashtags.add(hashtagsEntitie.getText());
            }
            saveConnection(date, name, new BasicDBObject("hashtag", hashtagsEntitie.getText()), 1, count,
                    ConnectionCollection);
            count++;
        }
        document.append("Hashtags", allHashtags);

        if (exist) {
            allsortURLS = (ArrayList) theObj.get("sortUrls");
            allcompleteURLS = (ArrayList) theObj.get("completeUrls");
        }
        URLEntity[] urlEntities = savedTweet.getURLEntities();
        if (savedTweet.getURLEntities().length > 0) {
            for (int i = 0; i < savedTweet.getURLEntities().length; i++) {
                if (urlEntities[i].getStart() < urlEntities[i].getEnd()) {

                    BasicDBObject document1 = new BasicDBObject("url", urlEntities[i].getURL());
                    String completeURL = urlEntities[i].getExpandedURL();
                    document1.append("completeURL", completeURL);
                    if (!allsortURLS.contains(document1)) {
                        allsortURLS.add(urlEntities[i].getURL());
                        allcompleteURLS.add(urlEntities[i].getExpandedURL());
                    }
                    saveConnection(date, name, document1, 2, count, ConnectionCollection);
                    count++;
                }
            }
        }
        document.append("sortUrls", allsortURLS);
        document.append("completeUrls", allsortURLS);

        if (exist) {
            allmentionedUsers = (ArrayList) theObj.get("Mentions");
        }
        UserMentionEntity[] mentionEntities = savedTweet.getUserMentionEntities();
        for (UserMentionEntity mentionEntitie : mentionEntities) {
            BasicDBObject document2 = new BasicDBObject("mentioned_user", mentionEntitie.getText());
            if (!allmentionedUsers.contains(mentionEntitie.getText())) {
                allmentionedUsers.add(mentionEntitie.getText());
            }
            saveConnection(date, name, document2, 3, count, ConnectionCollection);
            count++;
        }
        document.append("Mentions", allmentionedUsers);

        if (exist) {
            allTweets = (ArrayList) theObj.get("Tweets");
        }
        String tweetText = " ";
        if (savedTweet.isRetweet()) {
            tweetText = savedTweet.getRetweetedStatus().getText();
        } else {
            tweetText = savedTweet.getText();
        }
        BasicDBObject document2 = new BasicDBObject("Tweet", tweetText);
        if (!allTweets.contains(tweetText)) {
            allTweets.add(tweetText);
        }
        saveConnection(date, name, document2, 4, count, ConnectionCollection);
        count++;
        document.append("Tweets", allTweets);

        if (exist) {
            savedCollection.remove(searchDocument);
        }
        savedCollection.insert(document);

    }

    JaccardSimilarity js = new JaccardSimilarity();
    List<Map<List<String>, List<Float>>> list = new ArrayList<>();//This is the final list you need
    Map<List<String>, List<Float>> map1 = new HashMap<>();//This is one instance of the   map you want to store in the above map
    List<String> usersNames = new ArrayList<>();
    List<Float> usersResults = new ArrayList<>();

    BasicDBObject doc1;
    BasicDBObject doc2;
    DBCursor cursor1;
    DBCursor cursor2;
    DBObject obj1;
    DBObject obj2;
    List<String> tl1 = new ArrayList();
    List<String> tl2 = new ArrayList();

    float countSimilarity = 0;

    for (int i = 0; i < allUsers.size(); i++) {
        for (int j = i + 1; j < allUsers.size(); j++) {
            //System.out.println(i+" "+j);
            System.out.print(allUsers.get(i) + " " + allUsers.get(j) + " ");
            usersNames.add(allUsers.get(i));
            usersNames.add(allUsers.get(j));
            doc1 = new BasicDBObject("User", allUsers.get(i));
            doc2 = new BasicDBObject("User", allUsers.get(j));
            cursor1 = savedCollection.find(doc1);
            cursor2 = savedCollection.find(doc2);
            if (cursor1.hasNext() && cursor2.hasNext()) {
                obj1 = cursor1.next();
                obj2 = cursor2.next();
                tl1 = (ArrayList) obj1.get("Hashtags");
                tl2 = (ArrayList) obj2.get("Hashtags");
                countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2);
                usersResults.add(js.findSimilarity(tl1, tl2));
                System.out.print(js.findSimilarity(tl1, tl2) + " ");
                tl1 = (ArrayList) obj1.get("Mentions");
                tl2 = (ArrayList) obj2.get("Mentions");
                countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2);
                usersResults.add(js.findSimilarity(tl1, tl2));
                System.out.print(js.findSimilarity(tl1, tl2) + " ");
                tl1 = (ArrayList) obj1.get("Tweets");
                tl2 = (ArrayList) obj2.get("Tweets");
                countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2);
                usersResults.add(js.findSimilarity(tl1, tl2));
                System.out.print(js.findSimilarity(tl1, tl2) + " ");
                tl1 = (ArrayList) obj1.get("sortUrls");
                tl2 = (ArrayList) obj2.get("sortUrls");
                countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2);
                usersResults.add(js.findSimilarity(tl1, tl2));
                System.out.print(js.findSimilarity(tl1, tl2) + " ");
                usersResults.add((float) (countSimilarity / 4));
                System.out.print((float) (countSimilarity / 4) + " ");
                System.out.println();
                countSimilarity = 0;

            }

        }
        map1.put(usersNames, usersResults);
    }
    list.add(map1);

    //System.out.println(savedCollection.count());

    /*
    // Read every Element Example
    DBCursor cursor2 = savedCollection.find(searchDocument);
    if (cursor2.hasNext()) {
    theObj = cursor2.next();
    //String l =  ( String) cursor2.one().get("exist").toString();
    }
            
    BasicDBList list = new BasicDBList();
    list = (BasicDBList) theObj.get("Hashtags");
    BasicDBList l2 = new BasicDBList();
    for (int i = 0; i < list.size(); i++) {
    BasicDBObject bj = (BasicDBObject) list.get(i);
    System.out.println(bj.getString("hashtag"));
    }
            
    DBCursor cursor2 = savedCollection.find();
     List<String> list = new ArrayList();
               
    while (cursor2.hasNext()) {
    BasicDBObject document2 = (BasicDBObject) cursor2.next();
      String bj = (String) document2.get("User");
    System.out.println(bj);
     for (int i = 0; i < list.size(); i++) {
           // String bj = (String) list.get(i);
            // System.out.println(bj.getString("hashtag"));
              System.out.println(bj);
        }
     */
}

From source file:uk.ac.susx.tag.method51.twitter.Tweet.java

License:Apache License

public Tweet(Status status) {
    this();/*from   w  w w.  j ava 2 s. c o  m*/
    created = status.getCreatedAt();
    id = status.getId();
    text = status.getText();

    inReplyToStatusId = status.getInReplyToStatusId();
    inReplyToUserId = status.getInReplyToUserId();

    originalText = null;
    retweetId = -1;
    isTruncated = status.isTruncated();
    isRetweet = status.isRetweet();
    Status entities = status;
    if (isRetweet) {
        Status origTweet = status.getRetweetedStatus();
        entities = origTweet;
        retweetId = origTweet.getId();
        originalText = text;
        text = origTweet.getText();
    }

    StringBuilder sb = new StringBuilder();

    for (HashtagEntity e : entities.getHashtagEntities()) {
        sb.append(e.getText());
        sb.append(" ");
    }
    hashtags = sb.toString();
    sb = new StringBuilder();

    for (UserMentionEntity e : entities.getUserMentionEntities()) {
        sb.append(e.getScreenName());
        sb.append(" ");
    }
    mentions = sb.toString();
    sb = new StringBuilder();

    for (URLEntity e : entities.getURLEntities()) {
        //String url = e.getURL();
        String url = e.getExpandedURL();
        if (url == null) {
            url = e.getURL();
            if (url != null) {
                sb.append(url);
            }

        } else {
            sb.append(url);
        }
        sb.append(" ");
    }
    urls = sb.toString();
    sb = new StringBuilder();

    //seems to be null if no entries
    MediaEntity[] mediaEntities = entities.getMediaEntities();
    if (mediaEntities != null) {
        for (MediaEntity e : mediaEntities) {
            String url = e.getMediaURL();
            sb.append(url);
            sb.append(" ");
        }
        mediaUrls = sb.toString();
    } else {
        mediaUrls = "";
    }

    received = new Date();

    source = status.getSource();
    GeoLocation geoLoc = status.getGeoLocation();
    Place place = status.getPlace();

    if (geoLoc != null) {

        geoLong = geoLoc.getLongitude();
        geoLat = geoLoc.getLatitude();
    } else if (place != null && place.getBoundingBoxCoordinates() != null
            && place.getBoundingBoxCoordinates().length > 0) {

        GeoLocation[] locs = place.getBoundingBoxCoordinates()[0];

        double avgLat = 0;
        double avgLon = 0;

        for (GeoLocation loc : locs) {

            avgLat += loc.getLatitude();
            avgLon += loc.getLongitude();
        }

        avgLat /= locs.length;
        avgLon /= locs.length;

        geoLat = avgLat;
        geoLong = avgLon;
    } else {

        geoLong = null;
        geoLat = null;
    }

    twitter4j.User user = status.getUser();

    if (user != null) {
        userId = user.getId();
        location = user.getLocation();
        screenName = user.getScreenName();
        following = user.getFriendsCount();
        name = user.getName();
        lang = user.getLang();
        timezone = user.getTimeZone();
        userCreated = user.getCreatedAt();
        followers = user.getFollowersCount();
        statusCount = user.getStatusesCount();
        description = user.getDescription();
        url = user.getURL();
        utcOffset = user.getUtcOffset();
        favouritesCount = user.getFavouritesCount();

        this.user = new User(user);
    }
}