Saturday, July 07, 2007

Automatic Backup of All Projects

I wrote a script in groovy that backups all my source code projects automatically. I know this is actually the job of a version control system. But for the time, this script works for me well. The nice thing in this script is that it excludes all the fat files like zip, jar etc. So the generated backup is very small.

Here is the source code of the script:


import java.util.zip.*
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CharacterCodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
// Aşağıda belirtilen klasördeki tüm klasörleri zipler.
// Ancak arşiv dosyalarını (jar, ear gibi) hariç bırakır.
// klasorundenItıbaren klasörleri zipler

def zipIt = new File("D:/projects/groovy-deney-01")
def klasorundenItibaren = 'aaa'
def String arsivlenmeyecekDosyaUzantilari = /\S*\.(class|jar|ear|war|pdf|rar|7z|log|gz|swf|tld|index|psd|iws|ipr|pdb|dll|cab|obj|ocx|zip|ttf|log|mdb|bak|mat|aux)/
zipIt.listFiles().grep( {it.name > klasorundenItibaren}).each{ folder ->
if(folder.file) return
println(folder.name)

def buf = new byte[1024]
def out = new ZipOutputStream(new FileOutputStream(folder.name + ".zip"))
if(eklenecekDosyaYok(folder, arsivlenmeyecekDosyaUzantilari)) return
folder.eachFileRecurse {
if( it.file && !(it.name ==~ arsivlenmeyecekDosyaUzantilari)) {
println it.name
println it.canonicalPath
input = new FileInputStream(it.absolutePath)

String s = it.canonicalPath.substring(folder.canonicalPath.size()+1)

out.putNextEntry(new ZipEntry(s))
while((len = input.read(buf)) > 0) {
out.write(buf,0,len)
}
out.closeEntry();
input.close()
}
}
out.close()
}

def boolean eklenecekDosyaYok(File folder, String arsivlenmeyecekDosyaUzantilari){
println folder.name
println folder.listFiles().findAll{ !(it.name ==~ arsivlenmeyecekDosyaUzantilari)}.empty
println folder.listFiles().findAll{ !(it.name ==~ arsivlenmeyecekDosyaUzantilari)}
return folder.listFiles().findAll{ it.file && !(it.name ==~ arsivlenmeyecekDosyaUzantilari)}.empty
}

Read More...

Monday, July 02, 2007

Anket Verilerini Siniflandirma

Groovy ile anket sonuçlarını sınıflandıran bir program yazdım.


package com.shibi.util.text.ölçek

int ANAHTAR_SATIR_NO = 1
String KLASOR = 'resources/ölçek/Hareket ve Kuvvet 2/'
String[] cevapAnahtarı = new File(KLASOR + '00.txt')
.readLines()[ANAHTAR_SATIR_NO]
.split(/,/)
println cevapAnahtarı.size()
StringBuffer out = new StringBuffer()
new File(KLASOR + '01.txt').eachLine { line ->
def cevaplar = line.split(/,/)
def soru = 1
for( cevap in cevaplar ) {
out << olumlulukDeğeri(cevap, soru, cevapAnahtarı) << virgul(soru)
soru++
}
}
def output = new File(KLASOR + '02.txt')
output.write(out.toString())


String olumlulukDeğeri(String cevap, int soru_no, String[] cevapAnahtarı) {
switch(cevap) {
case 'A' : return (cevapAnahtarı[soru_no-1] == '1') ? '1' : '-1'
case 'B' : return (cevapAnahtarı[soru_no-1] == '1') ? '-1' : '1'
case 'C' : return '0'
case 'D' : return 'D'
default : return 'D'
}
}

String virgul(int soru_no) {
(soru_no % 30) ? ',' : '\n'
}


3 tane dosya var: 00.txt, 01.txt ve 02.txt

00.txt dosyası cevap anahtarı. Bu anket tutum ölçmeye yönelik olduğundan, her bir sorunun olumlu mu, olumsuz mu tutum içerdiğini belirtir.

01.txt elimizdeki ham veriler. Bunlar anketlere verilen yanıtlardır.
02.txt verilerin sınıflandırılmış hali. Bu örnekte 3'lü Likert tipi ölçek kullanıldığından, veriler -1, 0 veya +1 olarak sınıflandırılmaktadır.

Read More...