I was googling for an open source library to generate RTF text in java. After navigating through lots of forums, I couldn't find a satisfactory result. Then I searched in Google Blogs and voila :)
Here is the link to the rtf generator in java.
http://it.newinstance.it/2007/03/23/msword-generation-made-easyer/
It is a very small jar file of 5 KB size, but it depends on Freemarker.
Wednesday, March 28, 2007
Generate RTF in Java
Posted by Mert Nuhoglu at 9:03 PM 0 comments
Labels: java, text-processing
Parsing JMemorize Documents with Groovy
Groovy makes a lot of things absolutely very easy. One of them is xml parsing.
I use JMemorize for learning new programming libraries like Wicket and Groovy. But I want to import my flash cards into Keynote. So I needed to parse xml documents that JMemorize uses for storing the cards.
It is not a big deal parsing xml documents. But Groovy makes it much easier. I put my code here to show Groovy's easiness and maybe some JMemorize users may benefit.
def lesson = new XmlParser().parse(new File('resources/Java.jml'))
def cards = lesson.Category.Category.grep{it.'@name' == 'Groovy'}.Deck.Card
output = new File('resources/jmemorize.txt')
output.write('')
def counter = 0
for(card in cards) {
output << "Soru $counter:\n\n" << card.'@Backside' << '\n----\nYanıtı:\n' << card.'@Frontside' << '\n====\n'
counter++
}
Posted by Mert Nuhoglu at 7:26 PM 2 comments
Monday, March 26, 2007
Ergonomic Keyboard Layout for LaTeX Authoring
Inspired by Lasse Kliemann, I am trying to build a new keyboard layout for LaTeX authoring. Lasse Kliemann has produced a very easy to use keyboard layout for LaTeX symbols.
If you have written some LaTeX code, you certainly know that LaTeX requires lots of symbols like {\}([]$. These characters are hard to type because firstly they reside in far places on the keyboard, secondly they usually require modifier keys in combination. Lasse's solution is to set space-bar as a modifier. The symbols are bound to usual alphabetic characters on the keyboard. So using a key combination like space-s produces {. Both keys "space" and "s" are very easy to type in combination.
I wondered whether Lasse's keyboard layout is the optimum layout for LaTeX authoring. He actually designed the layout for other programming tasks as well like HTML and C authoring. So I wrote a program in Groovy that measures the character statistics in a given tex file. Here is the code:
map = [:]
new File('poisson01.tex').getText('utf-8').each {
if(map[it] == null)
map[it] = 0
else
map[it] = map[it] + 1
}
def keys = map.keySet().findAll{ !(('a'..'z').contains(it) || ('A'..'Z').contains(it)) }
def frequencies = map.subMap(keys)
def characterList = frequencies.keySet().toList()
characterList.sort { frequencies[it] }
characterList.reverse().each {
println it + ' ' + frequencies[it]
}
Here is the output of a run:
\ 893
489
446
{ 388
} 387
( 222
) 220
= 207
$ 155
_ 112
[ 110
] 110
. 99
1 90
ı 81
^ 80
So the various parentheses are the mostly used characters. But this is just one latex file. For better design optimization, one should use a randomly selected set of LaTeX files.
Posted by Mert Nuhoglu at 7:11 PM 0 comments
Labels: ergonomics, groovy, latex
Friday, March 16, 2007
Groovy: Automatic Text Replacement Script
I have been doing graduate studies in Industrial Engineering at Bogazici University for a few months. Normally I use a notebook to take my notes. But in the last few weeks I try to take my notes in the laptop. But this has some difficulties. I use abbreviations for the frequently used terms. Then I change them with find/replace in word. This is a manual repetitive task. So it is a very good case for automation. Since I have been learning Groovy these weeks, it was a good opportunity to try my knowledge.
This is the code. It reads two files. One file "sd.txt" contains my lecture notes. The other file "kelimeler.txt" contains the abbreviations together with their replacements.
map = [:]
new File('kelimeler.txt').eachLine {
map[it.tokenize()[0]] = it.tokenize()[1]
}
output = new File('out.txt')
output.write('')
new File('SD.txt').newReader('ISO-8859-9').eachLine { line ->
output.append(convert(line) + '\n','ISO-8859-9')
}
def String convert(line) {
matcher = line =~ /\w+/
list = matcher.collect { it }
map.keySet().grep(list).each {
line = line.replaceAll(/\b$it\b/,map[it])
}
return line
}
Note that, I am using ISO-8859-9 encoding, which is the standard, non-unicode encoding for turkish characters.
Posted by Mert Nuhoglu at 3:54 PM 0 comments
Return Statement Inside The Closures In The Functions
Regarding Groovy Categories:
Return statements inside the functions don't return anything if used in the "use" closures:
class TokenCategory3 {
static String g(String self) {
return 'hello'
}
}
String f() {
use(TokenCategory3) {
assert 'a'.g() == 'hello'
return 'a'.g()
}
}
use(TokenCategory3) {
assert 'a'.g() == 'hello'
}
assert f() == null
Note that the last line returns null, although there is a return statement in f().
To make f() return some value, the return statement should be outside the "use" closure:
String h() {
String result
use(TokenCategory3) {
result = 'a'.g()
}
return result
}
assert h() == 'hello'
Posted by Mert Nuhoglu at 10:15 AM 0 comments