StanfordNLP Default Model + Custom NER Model - stanford-nlp

How can I combine the default model provided by Stanford NLP(english.all.3class.distsim.crf.ser.gz) together with my custom model(ner-model.ser.gz)? I want to recognize Toyota as an 'PERS' entity and the rest of the sentence as the default 'O' entity. It must be recognised as a 'PERS' entity by the engine for later usages (Sentiment/Concept) by the StanfordNLP.
I've already trained the custom model by following the instructions in http://nlp.stanford.edu/software/crf-faq.html#a. I'm using the code as follows for the combination of classifiers:
String serializedClassifier = "ner-model.ser.gz";
String serializedClassifier2 = "edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz";
try {
NERClassifierCombiner classifier = new NERClassifierCombiner(false,
false, serializedClassifier, serializedClassifier2);
String ss = "Toyota is not an organisation, it is a person's name.";
System.out.println("---");
List<List<CoreLabel>> out = classifier.classify(ss);
for (List<CoreLabel> sentence : out) {
for (CoreLabel word : sentence) {
System.out.println(word.word() + '('
+ word.get(AnswerAnnotation.class) + ')');
}
System.out.println();
}
} catch (ClassCastException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The result is as follows:
Toyota(PERS)
is(O)
not(PERS)
an(O)
organization(PERS)
,(O)
it(PERS)
is(O)
a(PERS)
person(O)
's(PERS)
name(O)
.(PERS)
If I was to use the default model(ner-model.ser.gz) only, I would get
Toyota(ORGANIZATION)
is(O)
not(O)
an(O)
organization(O)
,(O)
it(O)
is(O)
a(O)
person(O)
's(O)
name(O)
.(O)
Thank you for your help in advance.

Related

How to add new Obect inside List<EntityClass>

I am fetching the record from the database. I want to modify new record with the existing record.
I fetching last 6 months data, I want to modify my result ? If any months records I am not getting from the database.
How I can modify existing result List ?
public List<EntityClassName> fetchByType() throws CustomExpection{
// TODO Auto-generated method stub
List<EntityClassName> result;
try {
result = (List<EntityClassName>) genDao.find(Query.byTpe, CommonUtil.getStartEndDate(), new BeanPropertyRowMapper(EntityClassName.class));
result.forEach(
userData -> {
String type = userData.getType_name();
log.info("Type:"+type);
//userData.setType_name(userData.getType_name()+" PPP");
}
);
log.info("Size:"+result.size());
System.err.println(result);
} catch (Exception e) {
e.printStackTrace();
log.error(
"Something went wrong !!!",
e.getMessage());
throw new CustomExpection("Something went wrong !!!");
}
return result;
}
List<Item> sBarang = new ArrayList<Item>();
Item mItem = new Item(); // <-- instantiate a new Item.
mItem.setCode("101");
mItem.setName("Hammer");
mItem.setQty(10);
sBarang.add(mItem); // <-- add it to your List<Item>.

Transform Optional String and return Date in Java 8?

I am currently parsing a nullable String to a Date. I try to use Optional to avoid using if statement. Here is what I have written so far :
Client client = new Client();
Optional.ofNullable(methodThatMayReturnStringOrNull())
.ifPresent((s) -> {
try {
client.setBirthDate(DateUtils.parseDate(
StringUtils.substring(s, 0, 10),
new String[]{"yyyy-MM-dd"}));
} catch (ParseException e) {
throw new TechnicalException("error.parsing.date", e);
}
});
Is it possible to transform this lambda so I can make it a method similar to the following but java 8 style?
private Date parse(String complexString) {
Date birthDate = null;
if (complexString != null) {
try {
birthDate = DateUtils.parseDate(
StringUtils.substring(complexString, 0, 10),
new String[]{"yyyy-MM-dd"});
} catch (final ParseException e) {
throw new TechnicalException("error.parsing.date", e);
}
}
return birthDate;
}
Not sure how far you want to go, but you can start with
Optional<Date> date = Optional.ofNullable(methodThatMayReturnStringOrNull())
.map((s) -> {
try {
return DateUtils.parseDate(
StringUtils.substring(s, 0, 10),
new String[]{"yyyy-MM-dd"}));
} catch (ParseException e) {
throw new TechnicalException("error.parsing.date", e);
}
});
You might also consider using flatMap instead of map and returning empty optional instead of throwing exception on error - depends on how you want to progress you flow.
On completely unrelated note, get rid of Date and use either joda or new java time classes ;)

PMD Xpath, how to compare 2 #Image?

It's is pretty easy to compare one path's #Image to a static string but how to compare 2 paths'#Image ?
Code that should trigger the rule :
public class Foo implements {
public String checkDBConnexion() {
try {
int result = jdbcTemplate.queryForObject("SELECT 1 FROM dual ", Integer.class);
} catch (DataAccessException e) {
// This is Ok : throw new MyException(test, e);
// this is not : hrow new MyException(test);
// This is also not ok if there are no thrown;
return "KO : " + e.getMessage();
}
}
}
Xpath for the param name of the Catch block :
//CatchStatement[//FormalParameter//VariableDeclaratorId]
Xpath for the variable name of the thrown method :
//ThrowStatement//Arguments//Expression//Name
It's easy to compare both to 'e' :
//CatchStatement[//FormalParameter//VariableDeclaratorId[#Image = 'e']]
//CatchStatement[//ThrowStatement//Arguments//Expression//Name[#Image = 'e']]
How do I compare them with eachother ?
Also if you have a link with detailled PMD XPath synthax exemple.
The basic PMD page has ... well the basic ;)
Answering myself :
//CatchStatement[//FormalParameter//VariableDeclaratorId/#Image =
//ThrowStatement//Arguments//Expression//Name/#Image]

Set Field Text, Java

I have two classes. MetaDataExtractor(GUI) and MetaData.
MetaData has the method which extracts the metadata from an image. MetaDataExtractor is designed to display the data in a JTextPane. (Please excuse the class names. I know it's a little confusing. I'm fairly new to Java).
MetaDataExtractor:
LongitudeField.setText("" + MetaDataTags.getLongitude());
MetaData:
public String getLongitude() {
try {
Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);
if (metadata.containsDirectory(GpsDirectory.class)) {
GpsDirectory gpsDir = (GpsDirectory) metadata.getDirectory(GpsDirectory.class);
GpsDescriptor gpsDesc = new GpsDescriptor(gpsDir);
String Longitude = "" + gpsDesc.getGpsLongitudeDescription();
}
} catch (ImageProcessingException ex) {
Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error 1");
} catch (IOException ex) {
Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error 2");
}
return longitude;
}
If I set the longitude to be displayed in the JTextPane, it returns "null". If however, I set it to print out on the command line, it prints the longitude fine?
Please excuse me if its a simple solution. I'm still getting to grips with Java.
Java is case sensitive and declare firstly your variable outside of try & catch statement.
Use a IDE like Eclipse to reduce syntax errors like these.
so you should have :
public String getLongitude() {
String longitudeDesc ="";
try {
Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);
if (metadata.containsDirectory(GpsDirectory.class)) {
GpsDirectory gpsDir = (GpsDirectory) metadata.getDirectory(GpsDirectory.class);
GpsDescriptor gpsDesc = new GpsDescriptor(gpsDir);
longitudeDesc = "" + gpsDesc.getGpsLongitudeDescription();
}
} catch (ImageProcessingException ex) {
Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error 1");
} catch (IOException ex) {
Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error 2");
}
return longitudeDesc ;
}

What is a GDATA extension profile?

I want to get the XML in atom format of a GoogleDocs spreadsheet using the [generateAtom(..,..)][1] method of the class BaseEntry which a SpreadsheetEntry inherits. But I don't understand the the second parameter in the method, ExtensionProfile. What is it and will this method call suffice if I just want to get the XML in atom format?
XmlWriter x = new XmlWriter();
spreadSheetEntry.generateAtom(x,new ExtensionProfile());
[1]: http://code.google.com/apis/gdata/javadoc/com/google/gdata/data/BaseEntry.html#generateAtom(com.google.gdata.util.common.xml.XmlWriter, com.google.gdata.data.ExtensionProfile)
From the JavaDoc for ExtensionProfile:
A profile is a set of allowed
extensions for each type together with
additional properties.
Usually if you've got a service, you can ask that for its extension profile using Service.getExtensionProfile().
Elaborating Jon Skeet's answer, you need to instanciate a service like this:
String developer_key = "mySecretDeveloperKey";
String client_id = "myApplicationsClientId";
YouTubeService service = new YouTubeService(client_id, developer_key);
Then you can write to a file using the extension profile of your service:
static void write_video_entry(VideoEntry video_entry) {
try {
String cache_file_path = Layout.get_cache_file_path(video_entry);
File cache_file = new File(cache_file_path);
Writer writer = new FileWriter(cache_file);
XmlWriter xml_writer = new XmlWriter(writer);
ExtensionProfile extension_profile = service.getExtensionProfile();
video_entry.generateAtom(xml_writer, extension_profile);
xml_writer.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Analogously, you can read a file using the extension profile of your service:
static VideoFeed read_video_feed(File cache_file_file) {
VideoFeed video_feed = new VideoFeed();
try {
InputStream input_stream = new FileInputStream(cache_file_file);
ExtensionProfile extension_profile = service.getExtensionProfile();
try {
video_feed.parseAtom(extension_profile, input_stream);
} catch (ParseException e) {
e.printStackTrace();
}
input_stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return video_feed;
}

Resources