I'm trying to install the JWKTL library into a Java project.
JWKTL Getting started
Now I downloaded the dump file as described, and parsed it into a new directory.
My code is:
package main;
import java.io.File;
import java.util.List;
import de.tudarmstadt.ukp.jwktl.JWKTL;
import de.tudarmstadt.ukp.jwktl.api.IWiktionaryEdition;
import de.tudarmstadt.ukp.jwktl.api.IWiktionaryEntry;
import de.tudarmstadt.ukp.jwktl.api.IWiktionaryPage;
import de.tudarmstadt.ukp.jwktl.api.IWiktionaryRelation;
import de.tudarmstadt.ukp.jwktl.api.PartOfSpeech;
import de.tudarmstadt.ukp.jwktl.api.RelationType;
public class Main {
final static String PATH_TO_DUMP_FILE = "/GetWords/enwiktionary-20160601-pages-articles-multistream.xml";
final static String TARGET_DIRECTORY = "/GetWords/";
final static boolean OVERWRITE_EXISTING_FILES = true;
/**
* Simple example which parses an English dump file and prints the entries for the word <i>Wiktionary</i>
* #param args name of the dump file, output directory for parsed data, ISO language code of the Wiktionary entry language (en/de), boolean value that specifies if existing parsed data should be deleted
*/
public static void main(String[] args) throws Exception {
File dumpFile = new File(PATH_TO_DUMP_FILE);
File outputDirectory = new File(TARGET_DIRECTORY);
boolean overwriteExisting = OVERWRITE_EXISTING_FILES;
JWKTL.parseWiktionaryDump(dumpFile, outputDirectory, overwriteExisting);
IWiktionaryEdition wkt = JWKTL.openEdition(TARGET_DIRECTORY);
//TODO: Query the data you need.
// Close the database connection.
wkt.close();
}
}
But the line: IWiktionaryEdition wkt = JWKTL.openEdition(TARGET_DIRECTORY); throws an error: The method openEdition(File) in the type JWKTL is not applicable for the arguments (String), when I try to enter the dump_file as
java.io.File
the program throws the following error:
Exception in thread "main" de.tudarmstadt.ukp.jwktl.api.WiktionaryException: Unable to establish a db connection
at de.tudarmstadt.ukp.jwktl.api.entry.BerkeleyDBWiktionaryEdition.<init>(BerkeleyDBWiktionaryEdition.java:228)
at de.tudarmstadt.ukp.jwktl.api.entry.BerkeleyDBWiktionaryEdition.<init>(BerkeleyDBWiktionaryEdition.java:205)
at de.tudarmstadt.ukp.jwktl.JWKTL.openEdition(JWKTL.java:98)
at de.tudarmstadt.ukp.jwktl.JWKTL.openEdition(JWKTL.java:89)
at main.Main.main(Main.java:31)
Caused by: java.lang.IllegalArgumentException: Malformed \uxxxx encoding.
at java.util.Properties.loadConvert(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at com.sleepycat.je.dbi.DbConfigManager.applyFileConfig(DbConfigManager.java:388)
at com.sleepycat.je.Environment.setupHandleConfig(Environment.java:323)
at com.sleepycat.je.Environment.<init>(Environment.java:260)
at com.sleepycat.je.Environment.<init>(Environment.java:212)
at de.tudarmstadt.ukp.jwktl.api.entry.BerkeleyDBWiktionaryEdition.connect(BerkeleyDBWiktionaryEdition.java:241)
at de.tudarmstadt.ukp.jwktl.api.entry.BerkeleyDBWiktionaryEdition.<init>(BerkeleyDBWiktionaryEdition.java:224)
... 4 more
Does anybody know how to fix this?
I use Java 8 in Eclipse Mars.
I installed these libs:
jwktl-1.0.1.jar
je-6.4.25.jar
apache-ant-1.8.2.jar
Thank you!
I solved it...
It was too simple:
The sulution is:
File f = new File(TARGET_DIRECTORY);
IWiktionaryEdition wkt = JWKTL.openEdition(f);
Best regards!
Related
I'm using the java.util.prefs package to store some information entered by users. My understanding, based on documentation and this question is that the actual (user node) preferences are stored in ~/Library/Preferences/, in a file named after the package. So far, this all checks out: Whenever I store some data in the node, a file in this directory is created and using the command line tool plutil, I can inspect it and find the stored data.
However: When I delete the file, and restart my program, the data is still there. I couldn't find anything about that in the documentation or source code. Any help appreciated.
The following code demonstrates the behaviour, see command line session below:
package de.unistuttgart.ims.PreferencesTest;
import java.io.IOException;
import java.util.prefs.Preferences;
public class Main {
Preferences preferences = Preferences.userNodeForPackage(Main.class);
static Main app;
static String KEY = "KEY";
static String DEFAULTVALUE = "DEFAULTVALUE";
public static void main(String[] args) throws IOException {
app = new Main();
app.doStuff();
}
public void doStuff() throws IOException {
System.err.println("Retrieving value:");
System.err.println(preferences.get(KEY, DEFAULTVALUE));
System.err.println("Setting value:");
char ch = (char) System.in.read();
preferences.put(KEY, String.valueOf(ch));
}
}
Command line session:
$ java de.unistuttgart.ims.PreferencesTest.Main
Retrieving value:
DEFAULTVALUE
Setting value:
5
$ rm ~/Library/Preferences/de.unistuttgart.ims.plist
$ java de.unistuttgart.ims.PreferencesTest.Main
Retrieving value:
5
Setting value:
4
How can this be? Or: Where else are preferences stored?
The image below shows the format of my settings file for a web bot I'm developing. If you look at line 31 in the image you will see it says chromeVersion. This is so the program knows which version of the chromedriver to use. If the user enters an invalid response or leaves the field blank the program will detect that and determine the version itself and save the version it determines to a string called chromeVersion. After this is done I want to replace line 31 of that file with
"(31) chromeVersion(76/77/78), if you don't know this field will be filled automatically upon the first run of the bot): " + chromeVersion
To be clear I do not want to rewrite the whole file I just want to either change the value assigned to chromeVersion in the text file or rewrite that line with the version included.
Any suggestions or ways to do this would be much appreciated.
image
You will need to rewrite the whole file, except the byte length of the file remains the same after your modification. Since this is not guaranteed to be the case or to find out is too cumbersome here is a simple procedure:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class Lab1 {
public static void main(String[] args) {
String chromVersion = "myChromeVersion";
try {
Path path = Paths.get("C:\\whatever\\path\\toYourFile.txt");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
int lineToModify = 31;
lines.set(lineToModify, lines.get(lineToModify)+ chromVersion);
Files.write(path, lines, StandardCharsets.UTF_8);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Note that this is not the best way to go for very large files. But for the small file you have it is not an issue.
I am trying to copy the first attribute of my training dataset, and copy the corresponding from Test set for book-keeping purpose. My code compiles successfully. But I am not able to run it.
I get the following errorwhen run java WekaRF
Exception in thread "main" java.lang.NoClassDefFoundError: weka/core/converters/CSVLoader
at WekaRF.main(WekaRF.java:17)
Caused by: java.lang.ClassNotFoundException: weka.core.converters.CSVLoader
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
If I run, java -cp weka-3-8-1/weka.jar WekaRF, I get
Error: Could not find or load main class WekaRF
My code is given below
import java.io.*;
import java.util.Random;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.RandomForest;
import weka.core.Instances;
import weka.filters.unsupervised.attribute.Remove;
import weka.core.converters.*;
public class WekaRF {
public static void main(String[] args) {
int percent = 60;
Instances data = null;
try {
CSVLoader loader = new CSVLoader();
loader.setSource(new File("../../rf.csv"));
data = loader.getDataSet();
} catch (Exception e) {
e.printStackTrace();
return;
}
int TrainSize = (int) Math.round(data.numInstances() * percent/ 100);
int TestSize = data.numInstances() - TrainSize;
Instances Train = new Instances(data, 0, TrainSize);
Instances Test = new Instances(data, TrainSize, TestSize);
System.out.println(Test.attribute(0).name());
Remove remove = new Remove();
String[] options = new String[2];
options[0] = "-R";
options[1] = "1";
remove.setOptions(options);
remove.setInputFormat(data2);
Test = Filter.useFilter(Test, remove);
Train.setClassIndex(Train.numAttributes() - 1);
}
}
Your classpath does not include current directory so java didn't see your WekaRF class.
Try to use java -cp .;weka-3-8-1/weka.jar WekaRF instead.
Notice dot at the start of -cp argument. It denotes current directory.
Upd: Character used to separate individual class path entries is operation system-depended. Windows uses ; and most other systems use :. The actual path separator can be retrieved by querying java.io.File::pathSeparator field.
I have a small java program that reads a given file with data and converts it to a csv file.
I've been trying to use the arrow symbols: ↑, ↓, → and ← (Alt+24 to 27) but unless the program is run from within Netbeans (Using F6), they will always come out as '?' in the resulting csv file.
I have tried using the unicodes, eg "\u2190" but it makes no difference.
Anyone know why this is happening?
As requested, here is a sample code that gives the same issue. This wont work when run using the .jar file, just creating a csv file containing '?', however running from within Netbeans works.
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Sample {
String fileOutName = "testresult.csv";
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
Sample test = new Sample();
test.saveTheArrow();
}
public void saveTheArrow() {
try (PrintWriter outputStream = new PrintWriter(fileOutName)) {
outputStream.print("←");
outputStream.close();
}
catch (FileNotFoundException e) {
// Do nothing
}
}
}
new PrintWriter(fileOutName) uses the default charset of the JVM - you may have different defaults in Netbeans and in the console.
Google Sheet uses UTF_8 according to this thread so it would make sense to save your file using that character set:
Files.write(Paths.get("testresult.csv"), "←".getBytes(UTF_8));
Using the "<-" character in your editor is for sure not the desired byte 0x27.
Use
outputStream.print( new String( new byte[] { 0x27}, StandardCharsets.US_ASCII);
I have to connect RFID reader with my java project. I installed all necesary driver for this device and imported all necesary Libraries. I used Eclipse program to writing my java project.
Am beginner with RFID readers. The below code doesn't work. Please help me.
package com.caen.RFIDLibrary;
import com.caen.RFIDLibrary.CAENRFIDException;
import com.caen.RFIDLibrary.CAENRFIDLogicalSource;
import com.caen.RFIDLibrary.CAENRFIDPort;
import com.caen.RFIDLibrary.CAENRFIDReader;
import com.caen.RFIDLibrary.CAENRFIDReaderInfo;
import com.caen.RFIDLibrary.CAENRFIDTag;
public class reader_com {
public static void main (String[] args) throws CAENRFIDException{
CAENRFIDReader MyReader = new CAENRFIDReader(); //Create myObject
CAENRFIDReaderInfo Info = MyReader.GetReaderInfo(); // Create Object for reader info
String Model = Info.GetModel(); //Get info about model
String SerialNumber=Info.GetSerialNumber(); // Get info about serialNumber
String FWRelease = MyReader.GetFirmwareRelease(); // Get info about FW
MyReader.Connect(CAENRFIDPort.CAENRFID_USB, "COM13"); // Open a connection
CAENRFIDLogicalSource MySource = MyReader.GetSource("Source_0, Source_1"); // Choose Source 0-->RFID tags 1-->Barcode
MySource.SetQ_EPC_C1G2(3); // set Q Value
CAENRFIDTag[] MyTags = MySource.InventoryTag();
if (MyTags.length > 0){
System.out.println(Model);
System.out.println(SerialNumber);
System.out.println(FWRelease);
}
MyReader.Disconnect();
}
}
And i get back this error:
Exception in thread "main" java.lang.NullPointerException
at com.caen.RFIDLibrary.CAENRFIDReader$IOBuffer.access$1800(CAENRFIDReader.java:228)
at com.caen.RFIDLibrary.CAENRFIDReader$CAENRFIDOutPacket.AddHeader(CAENRFIDReader.java:2701)
at com.caen.RFIDLibrary.CAENRFIDReader.GetReaderInfo(CAENRFIDReader.java:3183)
at com.caen.RFIDLibrary.reader_com.main(reader_com.java:20)
In order to get any information about the reader (or call any other method of the CAENRFIDReader object), you first need to connect to it. So the flow of your program would be like this:
CAENRFIDReader myReader = new CAENRFIDReader();
myReader.Connect(CAENRFIDPort.CAENRFID_RS232, "COM13");
CAENRFIDReaderInfo info = MyReader.GetReaderInfo();
String model = info.GetModel();
String serialNumber = info.GetSerialNumber();
String fwRelease = myReader.GetFirmwareRelease();
CAENRFIDLogicalSource mySource = myReader.GetSource(...);
Also note, that if your reader is available on COM13, you would likely need to use CAENRFIDPort.CAENRFID_RS232 and not CAENRFIDPort.CAENRFID_USB.