Android Open Source - android-udoo-rover Twitter Parser






From Project

Back to project page android-udoo-rover.

License

The source code is released under:

Copyright (c) 2014, Emanuele Palazzetti and contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the followin...

If you think the Android project android-udoo-rover listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.palazzetti.adkrover.twitter;
// ww w .  j a va  2 s  .  c o  m
import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.palazzetti.adkrover.arduino.Arduino;
import me.palazzetti.adkrover.utils.Helpers;

/**
 * Parse tweets into valid Arduino commands.
 */

public class TwitterParser {
    private static final int NORMALIZE_LIMIT = 5;
    private static final int MAX_CONSECUTIVE_COMMANDS = 3;
    private static final List<String> TWITTER_COMMANDS = Arrays.asList("f", "b", "l", "r", "t");

    public static List<String> tweetsToCommands(JSONArray tweetList) {
        boolean normalizationRequired = tweetList.length() >= NORMALIZE_LIMIT;
        int lastCommandCounter = 0;
        String serialCommand;
        String lastCommand = null;

        List<String> serialCommandsList = new ArrayList<String>();

        for (int i = 0; i < tweetList.length(); i++) {
            try {
                serialCommand = parseText(tweetList.getJSONObject(i).getString("text"));

                // Avoid too much occurrence of the same command
                if (normalizationRequired) {
                    if (serialCommand.equals(lastCommand)) {
                        lastCommandCounter++;
                    } else {
                        lastCommandCounter = 0;
                        lastCommand = serialCommand;
                    }

                    if (lastCommandCounter >= MAX_CONSECUTIVE_COMMANDS) {
                        continue;
                    }
                }

                serialCommandsList.add(serialCommand);

            } catch (JSONException e) {
                // Exclude this command execution cause of malformed JSON
                e.printStackTrace();
            } catch (MalformedTwitterCommand e) {
                // Exclude this command execution cause of malformed Twitter command
                e.printStackTrace();
            }
        }

        return serialCommandsList;
    }

    private static String parseText(String tweetText) throws MalformedTwitterCommand {
        int command = -1;
        int speed = 0;

        String[] splitTweet = tweetText.split(" ");

        if (splitTweet.length >= 3) {
            command = TWITTER_COMMANDS.indexOf(splitTweet[1].toLowerCase());
            speed = Helpers.isInteger(splitTweet[2]) ? Integer.valueOf(splitTweet[2]) : 0;
        }

        if (command < 0 || speed <= 0) {
            throw new MalformedTwitterCommand("Malformed Twitter command");
        }

        return Arduino.commandBuilder(command, speed);
    }
}




Java Source Code List

me.palazzetti.adkrover.RoverActivity.java
me.palazzetti.adkrover.arduino.Arduino.java
me.palazzetti.adkrover.twitter.MalformedTwitterCommand.java
me.palazzetti.adkrover.twitter.TwitterParser.java
me.palazzetti.adkrover.twitter.TwitterReceiver.java
me.palazzetti.adkrover.utils.Helpers.java
me.palazzetti.adkrover.utils.UrlConnector.java