How to train Open NLP without file - machine-learning

i have the following code for training Open NLP POS Tagger
Trainer(String trainingData, String modelSavePath, String dictionary){
try {
dataIn = new MarkableFileInputStreamFactory(
new File(trainingData));
lineStream = new PlainTextByLineStream(dataIn, "UTF-8");
ObjectStream<POSSample> sampleStream = new WordTagSampleStream(lineStream);
POSTaggerFactory fac=new POSTaggerFactory();
if(dictionary!=null && dictionary.length()>0)
{
fac.setDictionary(new Dictionary(new FileInputStream(dictionary)));
}
model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), fac);
} catch (IOException e) {
// Failed to read or parse training data, training failed
e.printStackTrace();
} finally {
if (lineStream != null) {
try {
lineStream.close();
} catch (IOException e) {
// Not an issue, training already finished.
// The exception should be logged and investigated
// if part of a production system.
e.printStackTrace();
}
}
}
}
and this works just fine. Now, is it possible to do the same without involving files? I want to store the training data in a database somewhere. Then i can read it as a stream or chunks and feed it to the trainer. I do not want to create a temp file. Is this possible?

Yes, instead of passing FileInputStream to a dictionary, you can create your own implementation of InputStream, say DatabaseSourceInputStream and use it instead.

Related

Manual Offset in Kafka Consumer

I want to write a Kafka consumer and write the record in Bigquery, I want to commit the offsets manually on successful insertion in Bigquery. I have a written a sample code but it is not working, can someone help
ReceiverOptions<Integer, String> options = receiverOptions.subscription(Collections.singleton(topic))
Flux<ReceiverRecord<Integer, String>> kafkaFlux1 = KafkaReceiver.create(options).receive()
.doOnNext(r -> {
try {
writeBigquery(r);
} catch (IOException e) {
e.printStackTrace();
}
r.receiverOffset().commit().block();
});
return kafkaFlux1.subscribe(record -> {
System.out.println("hello"+record);
});

Compare two images using imagemagic-im4java and show differences with original image

I am trying to compare two jpg images using im4java and showing difference between them.
i have successfully done that but the output of difference image contrast is decreased. but i need to show the differences on output image with original contrast.
I need to get result as diff without changing the contrast
public static void compareImageswithdifferenceimage (String expected, String actual, String difference) throws Exception {
try {
String Ipath="jars\\ImageMagick-7.0.10-Q16-HDRI";
ProcessStarter.setGlobalSearchPath(Ipath);
CompareCmd compare = new CompareCmd();
//compare.setErrorConsumer(StandardStream.STDERR);
IMOperation compareoperation = new IMOperation();
//compareoperation.addRawArgs("compare");
compareoperation.addImage(expected);
compareoperation.addImage(actual);
compareoperation.fuzz(1.0);
//compareoperation.channel("red");
compareoperation.metric("AE");
compareoperation.transparentColor("Black");
// compareoperation.fill("rgba(255, 215, 0, 0.1");
//compareoperation.opaque("red");
compareoperation.addImage(difference);
try {
System.out.println ("*******Comparison has Started********");
compare.run(compareoperation);
System.out.println ("*******Comparison has Finished*******");
}
catch (Exception e) {
System.out.println ("##### Comparison has Failed #######");
e.printStackTrace();
}
}
catch(Exception exc) {
System.out.println("Exception while executing compareImageswithdifferenceimage function");
exc.printStackTrace();
}
}
Note : I am unable to post images here

I am not able to parse IOS driver page source

I got Page source using
String pageSource = driver.getPageSource();
Now i need to save this xml file to local in cache. So i need to get element attributes like x and y attribute value rather than every time get using element.getAttribute("x");. But I am not able to parse pageSource xml file to some special character. I cannot remove this character because at if i need element value/text it shows different text if i will remove special character. Appium is use same way to do this.
I was also facing same issue and i got resolution using below code which i have written and it works fine
public static void removeEscapeCharacter(File xmlFile) {
String pattern = "(\\\"([^=])*\\\")";
String contentBuilder = null;
try {
contentBuilder = Files.toString(xmlFile, Charsets.UTF_8);
} catch (IOException e1) {
e1.printStackTrace();
}
if (contentBuilder == null)
return;
Pattern pattern2 = Pattern.compile(pattern);
Matcher matcher = pattern2.matcher(contentBuilder);
StrBuilder sb = new StrBuilder(contentBuilder);
while (matcher.find()) {
String str = matcher.group(1).substring(1, matcher.group(1).length() - 1);
try {
sb = sb.replaceFirst(StrMatcher.stringMatcher(str),
StringEscapeUtils.escapeXml(str));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Writer output = null;
output = new BufferedWriter(new FileWriter(xmlFile, false));
output.write(sb.toString());
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if you will get that kind of problem then catch it with remove special character and parse again.
try {
doc = db.parse(fileContent);
} catch (Exception e) {
removeEscapeCharacter(file);
doc = db.parse(file);
}
It might works for you.
I can able to do same using SAXParser and add handler to do for this.
Refer SAX Parser

How to compress the files in Blackberry?

In my application I used html template and images for browser field and saved in the sdcard . Now I want to compress that html,image files and send to the PHP server. How can I compress that files and send to server? Provide me some samples that may help lot.
i tried this way... my code is
EDIT:
private void zipthefile() {
String out_path = "file:///SDCard/" + "newtemplate.zip";
String in_path = "file:///SDCard/" + "newtemplate.html";
InputStream inputStream = null;
GZIPOutputStream os = null;
try {
FileConnection fileConnection = (FileConnection) Connector
.open(in_path);//read the file from path
if (fileConnection.exists()) {
inputStream = fileConnection.openInputStream();
}
byte[] buffer = new byte[1024];
FileConnection path = (FileConnection) Connector
.open(out_path,
Connector.READ_WRITE);//create the out put file path
if (!path.exists()) {
path.create();
}
os = new GZIPOutputStream(path.openOutputStream());// for create the gzip file
int c;
while ((c = inputStream.read()) != -1) {
os.write(c);
}
} catch (Exception e) {
Dialog.alert("" + e.toString());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
Dialog.alert("" + e.toString());
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
Dialog.alert("" + e.toString());
}
}
}
}
this code working fine for single file but i want to compress all the file(more the one file)in the folder .
In case you are not familiar with them, I can tell you that in Java the stream classes follow the Decorator Pattern. These are meant to be piped to other streams to perform additional tasks. For instance, a FileOutputStream allows you to write bytes to a file, if you decorate it with a BufferedOutputStream then you get also buffering (big chunks of data are stored in RAM before being finally written to disc). Or if you decorate it with a GZIPOutputStream then you get also compression.
Example:
//To read compressed file:
InputStream is = new GZIPInputStream(new FileInputStream("full_compressed_file_path_here"));
//To write to a compressed file:
OutputStream os = new GZIPOutputStream(new FileOutputStream("full_compressed_file_path_here"));
This is a good tutorial covering basic I/O . Despite being written for JavaSE, you'll find it useful since most things work the same in BlackBerry.
In the API you have these classes available:
GZIPInputStream
GZIPOutputStream
ZLibInputStream
ZLibOutputStream
If you need to convert between streams and byte array use IOUtilities class or ByteArrayOutputStream and ByteArrayInputStream.

Parsing maven pom.xml, using maven jar

I have been trying to parse maven pom.xml. I was successful to an extent. But my problem is I cannot get the default values. I have to manually inject the default values.
For example, if version number is not specified in pom.xml, then it is obvious that parent version will be used.
If build directory is not specified then it will be target.
I need to know, which classes should I use to get these things populated automatically. Apart from that I would like to have dependency graph built for me. The code I am using to parse is
Model model = null;
FileReader reader = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
try {
reader = new FileReader(pomfile);
model = mavenreader.read(reader);
model.setPomFile(pomfile);
if (model.getBuild() != null) {
// location of output directory basically target
if (model.getBuild().getDirectory() == null) {
//set the build directory
} // location of compiled classes
if (model.getBuild().getOutputDirectory() == null) {
// set the classes directory
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("pom file not found " + pomfile);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("pom file is not accessible " + pomfile);
} catch (XmlPullParserException e) {
e.printStackTrace();
throw new RuntimeException("unable to parse pom " + pomfile);
} catch (NullPointerException nullPointerException)
System.out.println("Exception setting build dir "+model.getPackaging());
} finally {
if (reader != null) {
reader.close();
}
}
Thanks.
Look at the code for the help:effective-pom plugin goal, or actually use it to dump the effective POM and xml-parse the result.

Resources