Ads

Monday, February 15, 2010

Ever expanding web

I am in the phase of my research where I am documenting a few things.

Its sort of overwhelming because every source you find leads you to new ones. And it doesn't seem to end!

So I am acquiring new sources faster than I am integrating them. Which is normal, but I want to be done acquiring sources so I can get to my work!

and !'s seem popular in this post for some reason.

I have some nice wholesome fiber-filled fat-free C code that I will post soon, no one seemed to like the lisp...

Friday, February 05, 2010

Slide Title Extractor Part 3 of 3

I hope this post is better than the last one. I assumed in the last post that you knew common lisp and could follow along. The lines were also long, so some stuff might have been clipped.

Instead of posting the code, as it flows, I am going to try and post it in a way that makes it easier to understand, so I am going to show missing code as "[...]"

I wanted to make it a stand alone executable, so I used ECL, a common lisp that makes it relatively easy to do so. Stand alone is sort of a misnomer, as you always need libraries. If you have them though, it is a very small executable.

Here is the boiler plate stuff, which is actually at the end of the source code, to take the names of the input and output file from the command line and form a stream that we can pass to extract-frame-titles. I have some code that attempts to see if you are testing the code or actually running it from the executable. Next, I use the input file name (*infile-name*) and the output filename (*outfile-name*) to open some streams that I will be pulling chars from or pushing a string into (write-string).

The outline of extract-slide-titles is as follows. I make a scope using the let to hold the output string (out-paragraph). Then I define two helper functions, get-next-chars which gets the next n characters from a stream, and add-char-to-out which appends a character to the variable out-paragraph. Next, I define the three functions that step through the characters in a state machine like manner. decide-next-step is used to decide what to do next if you are not currently checking for a frame title (check-for-frame-title), in a comment (throw-away-until-newline), or storing characters (store-char). At the end, I start the process off by calling decide-next-step.

Ok, so now I need to actually define those functions. I will start with the helper functions.

get-next-chars is really simple. I concatenate the next n characters into a string and return that. The loop returns a list of characters.

add-char-to-out is also very simple. I just append a character to the end of the string.

Now that that is all out of the way, I can define the fun stuff.

decide-next-step is the main loop. I have four things that can happen.
1) I am at the end, where I just add a newline to out-paragraph
2) I am in a comment, therefore I throw-away-until-newline
3) I am at the beginning of a command, so I check if it is a frame title
4) I just need to keep going (decide-next-step)


check-for-frame-title. I check the first two characters to see if they are "f" or "r". I use the and to then check if it is the frame title command. I split that up into two parts, because if the command is "\frame" then I might eat up some characters that I don't want to throw away. If its a "\frametitle" then I store-char, otherwise, I go back to decide-next-step.


store-char just puts the characters into the string until it gets to the end. It does not handle nested commands, but that is where you would add it.



throw-away-until-newline
does exactly what you would expect.


Thank you to tail call optimization, for without it, I would have clobbered the stack many moons ago.

Thursday, February 04, 2010

Plants update for 2010

I planted some herbs (not herb!), tomatoes, beans, peas, and a radish in my garden this year.

The picture is of the radish with two bean plants in a pot.


The next two are the pea plants in the soil with the herbs. The two plants "above" ground are the tomatoes which I am planting into a soil-less mixture.


Since I get no sun here, I am going to try to do this growing indoors with a grow-light.

Slide Title Extractor Part 2 of 3

So, I was unsatisfied with my last snippet of code. I had been reading a lot of common lisp, but not writing any. I rectified that with this poorly written code to get the frametitles out of a beamer slide that uses latex.

so, my first lisp version which I was not happy with. I am not going to comment much, as I want to spend more time on part 3.
I am going to just go down the code, with some comments as I feel like making them.

This is the main function, extract-slide-titles. I use a let to hold some variables common to the functions that are coming up.


(defun extract-slide-titles (stream)
(let ((out-paragraph "")
(cur-state)
(steps-left 10)
(command-string "")
(previous-char))

Decide next step is the first subfunction that is called on the stream. Depending on the current character, it decides to do different things. I use the variable cur-state to hold the next function to call.

(defun decide-next-step (c)
(cond
((not c) (setf out-paragraph (concatenate 'string out-paragraph (string #\newline)))
(return-from extract-slide-titles out-paragraph)) ;end of file
((char= #\% c) (setf cur-state #'throw-away-until-newline)) ;comments
((char= #\\ c) (setf cur-state #'check-for-frame-title)) ;look for latex commands
(t ())))

check-for-frame-title works when we know that a latex command has been found (when you have a "\" character. Then it checks if the command is the same as "frametitle".

(defun check-for-frame-title (c)
(setf steps-left (- steps-left 1))
(setf command-string (concatenate 'string command-string (string c)))
(cond
((char= #\{ c)
(setf steps-left 10)
(setf command-string "")
(setf cur-state #'decide-next-step))
((eq 0 steps-left)
(setf steps-left 10)
(if (equal command-string "frametitle")
(setf cur-state #'store-char)
(setf cur-state #'decide-next-step))
(setf command-string ""))
(t (setf cur-state #'check-for-frame-title)))
)

Here, I store the characters that I want to output to a string, until I reach the end of the latex command which is signaled by a "{".

(defun store-char (c)
(cond
;here is where to add support for nested commands
((and (char= #\ c) (char= #\ previous-char)) ())
((char= #\{ c) ())
((char= #\newline c) ())
((char= #\} c)
(setf out-paragraph (concatenate 'string out-paragraph ". "))
(setf cur-state #'decide-next-step))
(t
(setf out-paragraph (concatenate 'string out-paragraph (string c)))
(setf cur-state #'store-char)))
(setf previous-char c))

As before, I need to get rid of comments, so this just throws everything away until it hits a newline.

(defun throw-away-until-newline (c)
(cond
((char= #\newline c) (setf cur-state #'decide-next-step)) ;end of line, switch back
(t (setf cur-state #'throw-away-until-newline)))) ;keep going, throwing out c

Here, I start this thing by setting the cur-state "pointer" to the first function that I want called. Then I start the loop which will read characters from the stream until the end of the file. It will apply the function that cur-state refers to to the current character.


(setf cur-state #'decide-next-step)
(loop for char = (read-char stream nil) do
(funcall cur-state char))))

This is just to get the file into lisp, and to print the output to the screen.

(with-open-file (infile "test.tex" :if-does-not-exist nil)
(princ (extract-slide-titles infile)))

(quit)

Like I said, this code is ugly. I cleaned it up and hopefully made it clearer in the next iteration. The code for extract-frame-titles is much shorter the next time around.

Tuesday, February 02, 2010

Slide Title Extractor Part 1 of 3

This is a three part series about how I went about solving a very small computer problem.

I wanted to extract the text within the \frametitle{} tag in beamer (a latex slide making platform).

Example:


\frame{
\frametitle{Notes}
\begin{itemize}
\item Note 1
\end{itemize}
}

Where I would pull out the title "Notes".

My first attempt was a bash script using sed, tr, and more sed.


#!/bin/bash

#remove comments and get mostly frametitles
sed -n -f ste.sed $1 > $1.1

#remove line endings
tr "\n" " " < $1.1 > $1.2

#make new line endings
tr "}" "\n" < $1.2 > $1.3

#remove any lines with latex commands left
sed -n '/\\frametitle{/p' $1.3 > $1.4

#fix spacing
sed 's/[ ]* / /g' $1.4 > $1.5

#remove the \framtitles and put in periods and spaces
sed 's/ \\frametitle{//;s/$/./' $1.5 > $1.6

#remove line endings
tr "\n" " " < $1.6 > $1.7

cp $1.7 $1_summary.txt
rm $1.*


where ste.sed was:

/^%/d
/\\frametitle/{
N
s/.*\n}/ /
/\\frametitle.*}/p
}


Which I thought was inelegant. So I decided to rewrite it, which is part 2 and 3 of this series.

If anyone knows the regex to get it to work without all of my gymnastics, I would love to see it.

Jan 23 range report

I brought four targets back from a range trip in Jan. We traveled light (two rifles each + pistols). I was going to shoot my Marlin 795, but I forgot the magazine. A while into everything we realize that I could put a round in one at a time... NOPE, its got a magazine safety (LAME).

Anyway, on to some pics, 5.56 62gr steel cased wolf ammo...

25 yards, with el cheapo red dot sight, 6 shots, fairly rapidly, standing
1.7 inches wide, 3.8 tall:


25 yards, 10 shots, sitting, red dot, fairly rapidly
1.25 inches wide, 1.9 inches tall:


25 yards, 10 shots, sitting, iron sights, fairly rapidly
2.1 inches wide, 2.0 inches tall:


Not really that great performance, that means I can hit an 8x8 inch plate at 100 yards with 10 shots shooting rapidly. Seems huge.

We met this interesting guy at the range and he let us shoot his guns with his ammo. He was into matches so had quality versions of what I was shooting. (match grade 5.56, "tighter" rifle).

I shot at one of his targets at 75 yards at a shoot-n-c target that was about 12 inches wide. It had a red circle that was 3 inches wide that I was aiming for. He said that he might sight differently that I did, as my shots all landed high on the target. I shot 10 shots, but we only found 9.

I was shooting slowly using iron sights, 2.75 inches wide, 2.45 inches tall. Out to 100 yards that would be roughly 3.67 inches wide by 3.27 tall. Much better than 8x8! I think I called the one on the far left as being a flyer. I was experimenting with different trigger pulls as I may have figured out why I pull the rifle to the left when I shoot. I am going to change my trigger pull on rifles to use the tip tip of my finger and see if stuff changes.


I also shot my new P-3AT, using the impossible to find ammo that I ordered. No problems, except that the cases would bounce off of the ceiling onto you sometimes. We were shooting at paper plates at 25 yards. I would hit the very far right part of the plate sometimes. I need to shoot at closer things to see where it is zeroed (fixed sights, but I might just use a hammer?). The trigger pull is one of the longest and heaviest that I own, so I also move it alot during the trigger pull.