#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    [Homework] Having trouble with overwriting a portion of an ArrayList


    Here is the portion of my code that is giving me throuble:

    Code:
    for (int i = 0; i < words.length - 1; i++)
    		{
    			int nextIndex = i + 1;
    			if (this.stateContainer.getStateByWord(words[i]).findTransition(this.stateContainer.getStateByWord(words[nextIndex])) == null)
    			{
    				Transition newTransition = new Transition();
    				newTransition = this.stateContainer.getStateByWord(words[i]).incrementTransition(this.stateContainer.getStateByWord(words[nextIndex]));
    				newTransition.setOriginId(this.stateContainer.getStateByWord(words[i]).getId());
    				newTransition.setDestinationId(this.stateContainer.getStateByWord(words[nextIndex]).getId());
    				this.stateContainer.getStateByWord(words[i]).addTransition(newTransition);
    			}
    			else if (this.stateContainer.getStateByWord(words[i]).findTransition(this.stateContainer.getStateByWord(words[nextIndex])) != null)
    			{
    				this.stateContainer.getStateByWord(words[i]).incrementTransition(stateContainer.getStateByWord(words[nextIndex]));//increments transition
    			}
    		}
    The output that I'm getting for an input of "i am i the" is:
    [i]
    2 --> [the]
    2 --> [the]
    [am]
    1 --> [i]

    it should be:
    [i]
    1 --> [am]
    1 --> [the]
    [am]
    1 --> [i]


    Here is the entire code for the class in question:

    Code:
    import java.util.ArrayList;
    
    /**
     * This class implements a Markov model for use as in a nonsense
     * text generator.  Each state in the model represents a single
     * word in the input document.
     */
    public class MarkovModel 
    {
    	// TODO: Create private instance variables
    	//private Transition transition = new Transition();
    	//private State state = new State();
    	private StateContainer stateContainer = new StateContainer();
        //private Transition newTransition = new Transition();
        private ArrayList<Transition> transitions = new ArrayList<Transition>();
    	/**
    	 * The constructor accepts an array of words representing the input 
    	 * text.
    	 * <br>
    	 * For example, if the original text read "Sam I am, that Sam I am", 
    	 * the array of words would store:
    	 * <br>
    	 * <br>
    	 * ["Sam", "I", "am,", "that", "Sam", "I", "am"]
    	 * <br>
    	 * <br>
    	 * The constructor then uses this array of words to build the states 
    	 * and transitions that comprise the Markov model.
    	 * <br>
    	 * <br>
    	 * <b>IMPORTANT:</b> the state associated with the last word in the 
    	 * input text MUST include a transition to the state associated with 
    	 * the first word in the inputtext.
    	 * 
    	 * @param words all of the words from the input text box in their 
    	 * original order
    	 */
    	public MarkovModel(String[] words)
    	{			
    		// TODO: Implement this method.
    
    		// HINT: For each word, check if you already have a State 
    		//		 corresponding to that word in your state container. If not, 
    		//		 create a new state and add it to your state container.
    
    		// HINT: There should be AT MOST one Transition between any two 
    		//		 given states.  If word A follows word B three times in the 
    		//		 text, we would only create ONE transition moving from A 
    		//		 to B. This Transition's counter would store the the value 
    		//		 of 3 because A follows B three times.
    
    		//adds each unique word to stateContainer
    		for (int i = 0; i < words.length; i++)	
    		{
    			if (!stateContainer.containsWord(words[i]))
    			{
    				State newState = new State();
    				newState.setWord(words[i]);
    				newState.setId(i);
    				newState.incrementStateWordOccurenceCount();
    				this.stateContainer.addState(newState);
    			}
    			else if (stateContainer.containsWord(words[i]))
    			{
    				this.stateContainer.getStateByWord(words[i]).incrementStateWordOccurenceCount();
    			}
    		}
    
    		for (int i = 0; i < words.length - 1; i++)
    		{
    			int nextIndex = i + 1;
    			if (this.stateContainer.getStateByWord(words[i]).findTransition(this.stateContainer.getStateByWord(words[nextIndex])) == null)
    			{
    				Transition newTransition = new Transition();
    				newTransition = this.stateContainer.getStateByWord(words[i]).incrementTransition(this.stateContainer.getStateByWord(words[nextIndex]));
    				newTransition.setOriginId(this.stateContainer.getStateByWord(words[i]).getId());
    				newTransition.setDestinationId(this.stateContainer.getStateByWord(words[nextIndex]).getId());
    				this.stateContainer.getStateByWord(words[i]).addTransition(newTransition);
    			}
    			else if (this.stateContainer.getStateByWord(words[i]).findTransition(this.stateContainer.getStateByWord(words[nextIndex])) != null)
    			{
    				this.stateContainer.getStateByWord(words[i]).incrementTransition(stateContainer.getStateByWord(words[nextIndex]));//increments transition
    			}
    		}
    		
    		/*Transition newTransition = new Transition();
    		this.stateContainer.getStateById(this.stateContainer.getStates().size() - 1).incrementTransition(this.stateContainer.getStateById(0));
    		newTransition.setOriginId(this.stateContainer.getStateById(this.stateContainer.getStates().size() - 1).getId());
    		newTransition.setDestinationId(this.stateContainer.getStateById(0).getId());
    		this.stateContainer.getStateById(this.stateContainer.getStateById(this.stateContainer.getStates().size() - 1).getId()).addTransition(newTransition);
    		//creates transition from last word to first word*/
    	}
    
    	/**
    	 * @return this MarkovModel's StateContainer object
    	 */
    	public StateContainer getStateContainer() 
    	{
    		return this.stateContainer;
    	}
    
    	/**
    	 * Return the model as a String.  Each state should be separated by a 
    	 * line break. The first line of each state should print the state's 
    	 * word enclosed in square brackets. Each line thereafter should print 
    	 * each outward transition moving from the current state. Each transitions 
    	 * should be printed as follows:
    	 * <br>
    	 * <br>
    	 * &lt;transition count> --> [&lt;destination state's word>]
    	 * <br>
    	 * <br>
    	 * Example of printing a state whose word is "Sam":
    	 * <br>
    	 * 
    	 * 				<br>[sam]<br>
    	 *				2 --> [i]<br>
    	 *				1 --> [sam]<br>
    	 *				1 --> [let]<br>
    	 *				1 --> [you]<br>
    	 *				1 --> [if]<br><br>
    	 *
    	 * <b>The states must be printed in the order they were created.</b>
    	 */
    	@Override
    	public String toString()
    	{
    		ArrayList<State> allStates = stateContainer.getStates();
    		String s = "";
    
    		for (int n = 0; n < allStates.size(); n++)
    		{
    			s = s + "[" + allStates.get(n).getWord() + "]" + "\n";
    			for (int i = 0; i < allStates.get(n).getTransitions().size(); i++)
    			{
    				s = s + allStates.get(n).getTransitions().get(i).getCount() + " --> [" + stateContainer.getStateById(allStates.get(n).getTransitions().get(i).getDestinationId()).getWord() + "]" + "\n";
    			};
    		}
    
    		return s;
    	}
    
    	/**
    	 * This method traverses the states of the Markov model and generates the 
    	 * final nonsense text.  For each state visited during the traversal, this 
    	 * method should append the word from the current state to a String 
    	 * variable that will store the entire resultant text. This method should 
    	 * then return this String.
    	 * <br>
    	 * <br>
    	 * <b>IMPORTANT: The traversal starts at the state associated with the 
    	 * first word of the input text.</b>
    	 * <br>
    	 * <br>
    	 * <b>IMPORTANT: The output text moves to a new line after every 10 words 
    	 * that are generated.
    	 * <b>
    	 * 
    	 * @param numWords the number of words to output (i.e. the number of states 
    	 * to visit)
    	 *	@return a concatenation of all of the words from each state that was 
    	 * visited when traversing the model.
    	 */
    	public String generateText(int numWords)
    	{
    		// TODO: Implement this method.
    
    		// HINT: In order to meet the requirement of printing a new line after 
    		//		 every 10 words, you must append the newline character, "\n", 
    		//		 to the resulting string after appending each set of 10 words.
    		int nextStateId = 0;
    		String st = "";
    		
    		st = stateContainer.getStateById(0).getWord().toString();
    		nextStateId = moveToNextState(stateContainer.getStateById(0));
    		for (int i = 1; i < numWords; i++)
    		{
    			st = st + " " + stateContainer.getStateById(nextStateId).getWord().toString();
    			nextStateId = moveToNextState(stateContainer.getStateById(nextStateId));
    			
    			if (((i - 9) % 10) == 0)
    			{
    				st = st + "\n";
    			}
    		}
    		
    		/*st.split(" ");
    		for (int i = 0; i < numWords; i++)
    		{
    			stArray [i] = new String [numWords];
    		}*/
    		return st;
    	}
    
    	/**
    	 * This method accepts a State object that represents the "current" state 
    	 * while traversing the model, and outputs the ID of the "next" state.  The 
    	 * next state is determined by examining all of the transitions from the 
    	 * current state and randomly picking the next state based on the 
    	 * probabilities of these transitions.
    	 * <br>
    	 * <br>
    	 * For example, consider a state, <b>A</b>, with two transitions. The first 
    	 * of these transitions moves from <b>A</b> to <b>B</b> with a count 
    	 * variable of 3.  The second transition moves from <b>A</b> to <b>C</b> 
    	 * with a count variable of 4.  Then the probability that this method 
    	 * returns <b>B</b>'s ID is 3/7.  Similarly, the probability that this 
    	 * method returns <b>C</b>'s ID is 4/7.
    	 * 
    	 * @param currState the current state
    	 * @return the ID of the next state we should move to
    	 */
    	public int moveToNextState(State currState)
    	{		
    		// TODO: Implement this method.
    		int transitionCount = 0;
    		int indexOfTransition = 0;
    		int destinationId = 0;
    		
    		for (int i = 0; i < currState.getTransitions().size(); i++)
    		{
    			transitionCount = currState.getTransitions().get(i).getCount();
    			for (int j = 0; j < transitionCount; j++)
    			{
    				transitions.add(currState.getTransitions().get(i));
    			}
    		}
    		
    		indexOfTransition = Utility.RNG.nextInt(transitions.size());
    		destinationId = transitions.get(indexOfTransition).getDestinationId();
    		
    		return destinationId;
    	}
    
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    I'm sorry but your question is unclear and your code is not better (due to long lines it looks like a total mess).

    The title asks about substituting portion of ArrayList - but your code fragment is not related to any ArrayList (I think your ArrayList is called "transitions" and it is not mentioned here).

    So you'd better locate the fragment in question (perhaps by printing out intermediate results - remember that lists could be printed by itself and arrays require Arrays.toString call) - and describe what are input conditions for this fragment and what are expected and real results.
    CodeAbbey - programming problems for novice coders

IMN logo majestic logo threadwatch logo seochat tools logo