ANTLR test class not compiling? - antlr

I've cobbled together some code to test a lexer/parser grammar but I'm a stuck on how to create the appropriate file input / stream objects to parse a file. My code is as follows, and I'm getting an error about giving the BasicLexer class constructor an ANTLRInputStream instead of a CharStream, and a similar message with giving the BasicParser a CommonTokenStream (it expects TokenStream). Any ideas on where I've gone wrong?
public static void main(String[] args) throws Exception {
String filename = args[0];
InputStream is;
try {
is = new FileInputStream(filename);
//is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ANTLRInputStream in = new ANTLRInputStream(is);
BasicLexer lexer = new BasicLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
BasicParser parser = new BasicParser(tokens);
parser.eval();
}

Related

Access JCas Annotation list

I am developing an Apache UIMA v2 application to annotate documents.
I developed properly the process() method because I obtain the correct annotations (tested with debug and UIMA CAS Visual Debugger).
My application consists in a simple instantiation of the JCas object and the process of a document, i.e. a simple string in this case. Here's the code:
public class MainProgram {
public static void main(String[] args) {
try {
XMLInputSource in = new XMLInputSource("desc/dictionaryDescriptor.xml");
ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
JCas jcas = ae.newJCas();
jcas.setDocumentText("prova di a#gmail.com, timido, word, excel. ");
ae.process(jcas);
processResults(jcas);
ae.destroy();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidXMLException e1) {
e1.printStackTrace();
} catch (ResourceInitializationException e2) {
e2.printStackTrace();
} catch (AnalysisEngineProcessException e3) {
e3.printStackTrace();
}
}
public static void processResults(JCas jcas) {
System.out.println("Done!");
// TODO read annotations from jcas
}
}
If I add a breakpoint inside the processResults() method I can see the content of jcas and the list of annotation:
I want to access to the SubTypes list in the AnnotationIndex object, without taking care of the class type.
Here is an example through a specific type:
AnnotationIndex<Annotation> programIndex = jcas.getAnnotationIndex(Programma.type);
Iterator programIter = programIndex.iterator();
while(programIter.hasNext()) {
Programma p = (Programma) programIter.next();
}
You can use the JCasUtil to extract the annotations of the JCas:
JCasUtil.select(jCas, Annotation.class).stream()....
and with the getType() method of the annotation you can check for the type of the annotation.

NullPointerException when referenceing properties file

I have the below method:
public String readJson(){
String everything = "";
try(BufferedReader br = new BufferedReader(new FileReader("/users/mypc/test/etc/one.json"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
everything = sb.toString();
logger.debug("Everything: " + everything);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.debug("line: " + everything);
return everything;
}
This works fine as it is. My problem is that I don't want to hard code the file path /users/mypc/test/etc/one.json. I tried adding it to a properties file by doing the following:
test.properties:
file1=/users/mypc/test/etc/one.json
PropertiesHelper.java:
public String GET_FILE_ONE = "file1";
private static String one;
public String getJsonFile(){
one = config.getProperty(this.GET_FILE_ONE);
return one;
}
JsonReader.java:
private static PropertiesHelper helper;
final String GET_FILE_ONE = helper.getJsonFile();
public String readJson(){
String everything = "";
try(BufferedReader br = new BufferedReader(new FileReader(GET_FILE_ONE))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
everything = sb.toString();
logger.debug("Everything: " + everything);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.debug("line: " + everything);
return everything;
}
I've done similar things in the past but this time I'm getting a NullPointerException. I've been staring at this for a while now and can't see what's wrong. Is there something wrong with the way I've set my file path in the properties file or is there something else I've missed?
Answered above by Donnacha.
The issue was resolved by adding private static PropertiesHelper helper = new PropertiesHelper(); to JsonReader.java. See below:
private static PropertiesHelper helper;
private static PropertiesHelper helper = new PropertiesHelper();
final String GET_FILE_ONE = helper.getJsonFile();
public String readJson(){
String everything = "";
try(BufferedReader br = new BufferedReader(new FileReader(GET_FILE_ONE))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
everything = sb.toString();
logger.debug("Everything: " + everything);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.debug("line: " + everything);
return everything;
}

Text getting cut while creating PDF file using Apache PDF box 2.0.6

Creating pdf file by reading a text file
using apache pdfbox 2.0.6. Text which is being read is not getting displayed and is getting cut.
Below is the sample program which I am using:-
public static void main(String[] args) {
// TODO Auto-generated method stub
PDDocument doc = null;
TextToPDF text2pdf = new TextToPDF();
try {
doc = text2pdf.createPDFFromText(new FileReader("C:/sampleTextRead2.txt"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(doc, writer);
writer.close();
doc.save("C:/SamplePDF.pdf");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Adding new revision for document in DropBox through android api

I want to add a new revision to the document(Test.doc) in Dropbox using android api. Can anyone share me any sample code or links. I tried
FileInputStream inputStream = null;
try {
DropboxInputStream temp = mDBApi.getFileStream("/Test.doc", null);
String revision = temp.getFileInfo().getMetadata().rev;
Log.d("REVISION : ",revision);
File file = new File("/sdcard0/renamed.doc");
inputStream = new FileInputStream(file);
Entry newEntry = mDBApi.putFile("/Test.doc", inputStream, file.length(), revision, new ProgressListener() {
#Override
public void onProgress(long arg0, long arg1) {
Log.d("","Uploading.. "+arg0+", Total : "+arg1);
}
});
} catch (Exception e) {
System.out.println("Something went wrong: " + e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {}
}
}
New revision is created for first time. When i execute again, another new revision is not getting created.

How to use Lucene library to extract n-grams?

I am having a rough time trying to wrap my head around the Lucene library. This is what I have so far:
public void shingleMe()
{
try
{
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
FileReader reader = new FileReader("test.txt");
ShingleAnalyzerWrapper shingleAnalyzer = new ShingleAnalyzerWrapper(analyzer, 2);
shingleAnalyzer.setOutputUnigrams(false);
TokenStream stream = shingleAnalyzer.tokenStream("contents", reader);
CharTermAttribute charTermAttribute = stream.getAttribute(CharTermAttribute.class);
while (stream.incrementToken())
{
System.out.println(charTermAttribute.toString());
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It fails at stream.incrementToken(). It's my understanding that the ShingleAnalyzerWrapper uses another Analyzer to create a shingle analyzer object. From there, I convert it to a token stream which is then parsed using an attribute filter. However, it always results in this exception:
Exception in thread "main" java.lang.AbstractMethodError: org.apache.lucene.analysis.TokenStream.incrementToken()Z
Thoughts? Thanks in advance!
AbstractMethodError cannot occur as a result of wrong API usage -- it must be the result of compiling against one JAR and then running against a different one. Since you are using both Lucene Core and Lucene Analyzers JAR here, double-check your compile-time and runtime JAR classpaths.