GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Feb-2005, 01:17
twigboy twigboy is offline
New Member
 
Join Date: Feb 2005
Posts: 7
twigboy is on a distinguished road

justifying text with dynamic programming


hi everyone
im working on a program to justify text but it has to be done using dynamic programming so that the program finds the optimal number of words to put on each line by reading in from a text file. the only thing that i need left to build is the string format paragraph function.
right now im using a vector of vecors table but i was told that all i reall need is just a 1d vector. my problem is, with what i have now how do i convert it to a 1d vector
heres what i have so far

CPP / C++ / C Code:
#include <iostream>
#include <vector>
#include <string>
#include "sprint.h"

using std::vector;
using std::map;
using std::string;

using std::cout;
using std::cerr;
using std::cin;
using std::endl;

#define DEFAULT_TEXT_WIDTH 60
#define INT_INFINITY (1<<30)

#define MIN(a,b) ((a)<(b)?(a):(b))

// trim trailing blanks from a given string
void trim(string& s) {
  int i = s.size()-1;  
  while (i >= 0 && s[i] == ' ')
    --i;
  s.erase(i+1);
}

// return the penalty associated with a given line
int line_cost(string line, int text_width) {
  trim(line);
  int gap = text_width - line.size();
  if (gap < 0) return 1000000;
  return gap*gap*gap;
}



string format_paragraph( vector<string> words, inttext_width) {
  int i,j,n,space;
  
   
  n = words.size();
  
  vector< vector<int> >length (n,n);
  
  for(i = 0; i < n; ++i){
  length[i][i] = words[i][i];
	for(j=i+1; j<n; ++j)
	length[i][j] = length[i][j-1] + 1;
		}
			
vector< vector<int> >penalty (n,n);
	
for( i=0; i<n; ++i)
	for( j = i; j < n; ++j){
		if(length[i][j] < text_width)
		penalty[i][j] = abs(text_width - length[i][j] -(j-i) * space);
		else 
		penalty[i][j] = INT_INFINITY;			
  	}
	 
vector< vector<int> >cost (n,n);

for( j = 0; j < n; ++j){
cost[j][j] = penalty[j][j];
	for( i = j - 1; i >= 0; --i){
	int min = penalty[i][j];
		for(int k = i; k < j; ++k){
		int temp = penalty[i][k] + cost[k+1][j];
		if(temp<min)
		min=temp;
		}
		
	cost[i][j] = min;
	
    }
  }	
}	
	

int main(int argc, char *argv[]) {

  int text_width = DEFAULT_TEXT_WIDTH;

  if (argc >= 2) {
    text_width = atoi(argv[1]);
  }

  // read, format, and output a sequence of paragraphs
  // (new paragraph starts with a word "<p>")

  while (std::cin) {

    // get paragraph
    vector<string> words;
    while (std::cin) {
      string word;
      std::cin >> word;
      if (word.empty()) break;
      if (word == "<p>") break;
      words.push_back(word);
    }

    // output paragraph, formatted
    if (words.empty()) continue;
    cout << format_paragraph(words, text_width);
    cout << endl;
  }
}
Last edited by LuciWiz : 03-Feb-2005 at 01:29. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 03-Feb-2005, 09:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by twigboy
hi everyone
im working on a program to justify text but it has to be done using dynamic programming so that the program finds the optimal number of words to put on each line by reading in from a text file. the only thing that i need left to build is the string format paragraph function.
right now im using a vector of vecors table but i was told that all i reall need is just a 1d vector. my problem is, with what i have now how do i convert it to a 1d vector
heres what i have so far


In the first place: where is the program specification? Is inter-word spacing relevant (multiple spaces treated as single spaces or what)? How would you handle tab characters? Anything else I should know? I didn't really try to analyze your actual cost calculations and formatting, since I don't know what it is supposed to be trying to do.

Saying "all you really need is just a 1D vector" means nothing. That's like saying, "all you really nead is the text processing program TeX."


In fact, all you really need is a string:
Read the entire paragraph into a string and then performing line-breaking iteratively: Go through the string multiple times, experimentally moving the line breaks, and keeping track of total cost for each iteration. Keep the best effort in a separate string or something like that.

Instead of a string, you could use a 1-D vector of strings (the words) so that multiple passes through the paragraph would not have to break the string into words every time (but that depends on spacing and tab considerations that I mentioned).

By the way, the program you posted is obviously defective. Here are a few things I noticed:

This line is incorrect (inttext_width needs a type specifier).
CPP / C++ / C Code:
string format_paragraph( vector<string> words, inttext_width) {

Furthermore, the function uses text_width, not inttext_width. Oh --- now I see --- it should be
CPP / C++ / C Code:
string format_paragraph( vector<string> words, int text_width) {


Furthermore the function is declared as returning a string but returns nothing.

Furthermore the penalty calculation use the variable "space", but the variable is never given a value.

Now these makes no sense to me; maybe you can explain:
CPP / C++ / C Code:
  vector< vector<int> >length (n,n);
..
  vector< vector<int> >penalty (n,n);
..
  vector< vector<int> >cost (n,n);

What are these trying to do? They look as though they are defining vectors of vectors and trying to intialize with two ints --- or what?


Regards,

Dave
  #3  
Old 03-Feb-2005, 11:05
twigboy twigboy is offline
New Member
 
Join Date: Feb 2005
Posts: 7
twigboy is on a distinguished road
thanx for the response dave
inter word spacing is suppose to be taken into consideration but that is the only thing as far as spacing. no tab and new paragraph is already done
yes i am looking to put this into a 1-d vector of strings and yes it is pretty much like the text formatting program TeX.

as far as the vector< vector<int> > penalty (n,n) all that is saying is an n number of int with an n size. but that is what i want to get rid of by creating a single vector

oh and the var "space" is the inter word spacing i put that mainly in there as a reminder because i have yet figured out how to actually calculate it
thanx for the help
  #4  
Old 03-Feb-2005, 12:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by twigboy
thanx for the response dave
inter word spacing is suppose to be taken into consideration but that is the only thing as far as spacing. no tab and new paragraph is already done
yes i am looking to put this into a 1-d vector of strings and yes it is pretty much like the text formatting program TeX.

as far as the vector< vector<int> > penalty (n,n) all that is saying is an n number of int with an n size. but that is what i want to get rid of by creating a single vector

oh and the var "space" is the inter word spacing i put that mainly in there as a reminder because i have yet figured out how to actually calculate it
thanx for the help

If you want a vector strings with an initial size of n strings, for example just use:

CPP / C++ / C Code:
  vector< vector<string> > penalty (n);

It's still dynamic, and it can be increased or decreased in size by member functions.

(And the formatting is more like Microsoft Word, since Word does it a paragraph at a time, and Tex does it a page at a time --- that's why Tex blows the doors off of Word and other "word" processors as far as quality of output.)

Regards,

Dave
  #5  
Old 03-Feb-2005, 12:28
twigboy twigboy is offline
New Member
 
Join Date: Feb 2005
Posts: 7
twigboy is on a distinguished road
kewl thanx dave
but if i wanted to create a 1d vector out of the existing code i already have how would i do that or would i need to recode everthing?
  #6  
Old 03-Feb-2005, 12:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by twigboy
kewl thanx dave
but if i wanted to create a 1d vector out of the existing code i already have how would i do that or would i need to recode everthing?

Since your existing code doesn't work (won't compile; function format_paragraph() doesn't return anything, etc.) I'm not sure how to answer that.

In particular, I haven't tried to see exactly what your format_paragraph() is doing, so I couldn't comment on changes.

Regards,

Dave
  #7  
Old 03-Feb-2005, 15:30
twigboy twigboy is offline
New Member
 
Join Date: Feb 2005
Posts: 7
twigboy is on a distinguished road
can someone help me out in developing the program then?
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
burning problems PLEASE PLEASE HELP kelticeire Computer Hardware Forum 4 01-Dec-2006 16:39
saving html text dopee MySQL / PHP Forum 1 17-Jan-2005 05:15
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.