Suggestions for improving code

Discussion in 'Ruby' started by Aditya Mahajan, Mar 11, 2006.

  1. I am trying to convert a plain text file into a tex file with
    somemarkup. I have the following piece of code that does most of the
    work. It works fine but does not "look good". Can someone suggest how
    can I improve this code.

    ---------------[snip]----------------------
    file = File.new(filename, 'r')
    texfile = File.open(basename + ".tex", 'w')

    CHAPTER = Regexp.new("CHAPTER")
    SPACES = Regexp.new("\s\s\s\s")
    BLANK = Regexp.new(/^\s*$/)

    chapter = true
    verse = false
    prev_line = ""

    file.each_line do |line|
    if chapter && !BLANK.match(line)
    chapter = false
    texfile.puts "\\chapter{#{line.chomp}}"
    elsif CHAPTER.match(line)
    chapter = true
    elsif verse && !SPACES.match(line)
    texfile.puts '\stoplines\stopnarrower'
    texfile.puts line.chomp
    verse = false
    elsif !verse && BLANK.match(prev_line) && SPACES.match(line)
    texfile.puts '\startnarrower\startlines'
    texfile.puts line.chomp
    verse = true
    else
    texfile.puts line.chomp
    end
    prev_line = line
    end
    ------------------[snip]--------------------

    Thanks,
    Aditya

    --
    Aditya Mahajan, EECS Systems, University of Michigan
    http://www.eecs.umich.edu/~adityam || Ph: 7342624008
     
    Aditya Mahajan, Mar 11, 2006
    #1
    1. Advertising

  2. Aditya Mahajan wrote:
    > I am trying to convert a plain text file into a tex file with
    > somemarkup. I have the following piece of code that does most of the
    > work. It works fine but does not "look good". Can someone suggest how
    > can I improve this code.
    >
    > ---------------[snip]----------------------
    > file = File.new(filename, 'r')
    > texfile = File.open(basename + ".tex", 'w')
    >
    > CHAPTER = Regexp.new("CHAPTER")
    > SPACES = Regexp.new("\s\s\s\s")
    > BLANK = Regexp.new(/^\s*$/)
    >
    > chapter = true
    > verse = false
    > prev_line = ""
    >
    > file.each_line do |line|
    > if chapter && !BLANK.match(line)
    > chapter = false
    > texfile.puts "\\chapter{#{line.chomp}}"
    > elsif CHAPTER.match(line)
    > chapter = true
    > elsif verse && !SPACES.match(line)
    > texfile.puts '\stoplines\stopnarrower'
    > texfile.puts line.chomp
    > verse = false
    > elsif !verse && BLANK.match(prev_line) && SPACES.match(line)
    > texfile.puts '\startnarrower\startlines'
    > texfile.puts line.chomp
    > verse = true
    > else
    > texfile.puts line.chomp
    > end
    > prev_line = line
    > end
    > ------------------[snip]--------------------
    >
    > Thanks,
    > Aditya
    >
    > --
    > Aditya Mahajan, EECS Systems, University of Michigan
    > http://www.eecs.umich.edu/~adityam || Ph: 7342624008



    ruby -p01e'gsub(/(\n\s*\n)((?:^\s{4}.*\n)+)/,
    "\\1\\startnarrower\\startlines\n\\2\\stoplines\\stopnarrower\n");
    gsub(/(CHAPTER\s+)(.*)\n/,"\\chapter{\\2}\n")' in >out
     
    William James, Mar 12, 2006
    #2
    1. Advertising

  3. <--- On Mar 12, William James wrote --->

    > Aditya Mahajan wrote:
    >> I am trying to convert a plain text file into a tex file with
    >> somemarkup. I have the following piece of code that does most of the
    >> work. It works fine but does not "look good". Can someone suggest how
    >> can I improve this code.
    >>
    >> ---------------[snip]----------------------
    >> file = File.new(filename, 'r')
    >> texfile = File.open(basename + ".tex", 'w')
    >>
    >> CHAPTER = Regexp.new("CHAPTER")
    >> SPACES = Regexp.new("\s\s\s\s")
    >> BLANK = Regexp.new(/^\s*$/)
    >>
    >> chapter = true
    >> verse = false
    >> prev_line = ""
    >>
    >> file.each_line do |line|
    >> if chapter && !BLANK.match(line)
    >> chapter = false
    >> texfile.puts "\\chapter{#{line.chomp}}"
    >> elsif CHAPTER.match(line)
    >> chapter = true
    >> elsif verse && !SPACES.match(line)
    >> texfile.puts '\stoplines\stopnarrower'
    >> texfile.puts line.chomp
    >> verse = false
    >> elsif !verse && BLANK.match(prev_line) && SPACES.match(line)
    >> texfile.puts '\startnarrower\startlines'
    >> texfile.puts line.chomp
    >> verse = true
    >> else
    >> texfile.puts line.chomp
    >> end
    >> prev_line = line
    >> end
    >> ------------------[snip]--------------------
    >>

    > ruby -p01e'gsub(/(\n\s*\n)((?:^\s{4}.*\n)+)/,
    > "\\1\\startnarrower\\startlines\n\\2\\stoplines\\stopnarrower\n");
    > gsub(/(CHAPTER\s+)(.*)\n/,"\\chapter{\\2}\n")' in >out
    >

    Thank you for the regex. The chapter part of your regex is better than
    what I was doing, it does not correctly identify narrower region.
    I think that I can tweak it a little to make it work correctly. But
    what I want to know is there a better way to do this in ruby.

    I am not too comfortable with coding using a regex as it can be very
    difficult to maintain. Each time I have to look into the expression
    and try to understand it again.

    Basically the logic of the program depends on the "state" which I am
    keeping track of using flags. Your code gets rid of the flags using a
    two pass algorithm. What are the pros and cons. In using a gsub, the
    program needs to read the entire file before it can make any changes.
    I thought that this would be memory inefficient but for a ~160 kb
    file, it is almost instantaneous. Even for a 3MB file it takes less
    than a second. At what file sizes should one read the file line by
    line rather than entire file in a single shot?

    Thanks


    --
    Aditya Mahajan, EECS Systems, University of Michigan
    http://www.eecs.umich.edu/~adityam || Ph: 7342624008
     
    Aditya Mahajan, Mar 14, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lees
    Replies:
    2
    Views:
    408
    Robert Kern
    Feb 25, 2008
  2. mo reina
    Replies:
    5
    Views:
    255
    John Bokma
    Jul 27, 2010
  3. Rick Johnson
    Replies:
    0
    Views:
    111
    Rick Johnson
    Feb 28, 2013
  4. Joel Goldstick
    Replies:
    0
    Views:
    118
    Joel Goldstick
    Feb 28, 2013
  5. Ian Kelly
    Replies:
    0
    Views:
    107
    Ian Kelly
    Feb 28, 2013
Loading...

Share This Page