package gannuWSD;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import gannuNLP.corpus.Corpus;
import gannuNLP.dictionaries.DataBroker;
import gannuNLP.dictionaries.Dictionary;
import gannuUtil.Util;
/**
* Command for loading an offline dictionary into the DataBroker.<br/>
* Usage: java -cp "gannu.jar" gannuWSD.DataLoader GannuConnectorClass dictionaryVersion dictionaryFilesPath
* @author Francisco Viveros-Jiménez
*/
public class DataLoader {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
if(args.length<3)
{
System.out.println("Usage: java -cp \"gannu.jar\" gannuWSD.DataLoader connectorClass dictionaryVersion dictFilesPath for parsing a corpora/dictionary first.");
}
else
{
Dictionary dict=(Dictionary)Class.forName(args[0]).newInstance();
dict.setVersion(args[1]);
File f;
File sourceList=new File("./data/"+dict.getName()+".sl");
f=new File("./data/"+dict.getName()+"/");
f.mkdirs();
f=new File("./data/"+dict.getName()+"/"+dict.getName()+".sta");
if(f.exists())//then try to upload a corpus
{
DataBroker db=new DataBroker(args[0],args[1]);
db.load("Glosses;");
System.out.println("Dictionary uploaded!");
System.out.println("Loading samples from SemCor files!");
f=new File(args[2]);
if(f.exists())
{
Corpus c=new Corpus(args[2],db, true);
DataLoader.addSourceList(sourceList,c.getName());
c.WriteSuperLemmas("./data/"+dict.getName()+"/");
System.out.println("Finished!");
}
else
{
System.out.println("Corpus not found!");
}
}
else//then try to upload a dictionary
{
if(!dict.isWeb())
{
dict.setPath("./Resources/"+dict.getName());
ArrayList<File> files=Util.getAllFiles(new File(args[2]));
File dir=new File("Resources/"+dict.getName());
dir.mkdirs();
for(File file:files)
{
FileChannel source=new FileInputStream(file).getChannel();
File tfile=new File("Resources/"+dict.getName()+"/"+file.getName());
tfile.createNewFile();
FileChannel target=new FileOutputStream(tfile).getChannel();
if (target != null && source != null) {
target.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (target != null) {
target.close();
}
}
System.out.println("Uploading dictionary! Wait some minutes please!");
dict.loadCoreData();
dict.parseSamplesFromDictionary();
dict.load("Glosses;Samples");
dict.WriteSuperLemmas("./data/"+dict.getName()+"/");
System.out.println("Dictionary uploaded!");
}
else
{
System.out.println("Online dictionaries cannot be uploaded with this tool!");
}
DataLoader.addSourceList(sourceList,dict.getName());
}
}
}
public static void addSourceList(File sourceList, String name)throws Exception {
String write="";
if(sourceList.exists())
{
FileReader fin=new FileReader(sourceList);
BufferedReader in=new BufferedReader(fin);
String line;
while((line=in.readLine())!=null)
{
if(!line.equals(name))
write+=line+"\n";
}
in.close();
fin.close();
}
write+=name+"\n";
FileWriter fout=new FileWriter(sourceList);
BufferedWriter out=new BufferedWriter(fout);
out.write(write);
out.close();
fout.close();
}
}