why can execute my code without any issues and it gives me the correct results.
package com.opennlp.demo;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;
public class ParserTest {
public static Set<String> nounPhrases = new HashSet<>();
private static String line = "I need a PHP note if";
public void getNounPhrases(Parse p) {
if (p.getType().equals("NN") || p.getType().equals("NNS") || p.getType().equals("NNP") || p.getType().equals("NNPS")) {
nounPhrases.add(p.getCoveredText());
//System.out.println(p.getCoveredText());
}
for (Parse child : p.getChildren()) {
getNounPhrases(child);
System.out.println(child.toString()+"lol");
}
}
public void parserAction() throws Exception {
InputStream is = new FileInputStream("en-parser-chunking.bin");
ParserModel model = new ParserModel(is);
Parser parser = ParserFactory.create(model);
Parse topParses[] = ParserTool.parseLine(line, parser, 1);
for (Parse p : topParses){
//p.show();
getNounPhrases(p);
}
}
public static void main(String[] args) throws Exception {
new ParserTest().parserAction();
System.out.println("List of Noun Parse : "+nounPhrases);
}
}
below is my second code.. it gives me a blank Array in console.I removed main method from above class and I tried to print the nounPhrases inside another class. it shows a blank array. I think in second code my parserAction method is not executing. How can I do execute that method without a main method in another class ?
my second code
package com.opennlp.demo;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;
public class ParserTest {
public static Set<String> nounPhrases = new HashSet<>();
//private static String line = "i need a Java book which has JSP also";
private static String line = "I need a PHP note if";
public void getNounPhrases(Parse p) {
if (p.getType().equals("NN") || p.getType().equals("NNS") || p.getType().equals("NNP") || p.getType().equals("NNPS")) {
nounPhrases.add(p.getCoveredText());
//System.out.println(p.getCoveredText());
}
for (Parse child : p.getChildren()) {
getNounPhrases(child);
System.out.println(child.toString()+"lol");
}
}
public void parserAction() throws Exception {
InputStream is = new FileInputStream("en-parser-chunking.bin");
ParserModel model = new ParserModel(is);
Parser parser = ParserFactory.create(model);
Parse topParses[] = ParserTool.parseLine(line, parser, 1);
for (Parse p : topParses){
//p.show();
getNounPhrases(p);
}
}
}
I called the nounPhrases in another class like below. But it shows a blank result. How to fix this ? I need to do this without a main method in this class.
ParserTest pt = new ParserTest();
pt.parserAction();
System.out.println(ParserTest.nounPhrases);
Related
I'm trying to create objects of the classes I have in the main method, I'm implementing the lazy simpleton pattern, but I keep getting the error cannot find symbol in class. I've checked to see if I've written the import package statements correctly as well.
This is my main class
package control;
import java.io.File;
import java.io.FileNotFoundException;
import model.ApplicationModel;
import java.util.* ;
import model.Shop;
import view.ApplicationViewer;
import model.ApplicationModel;
public class ApplicationControl {
public static void main (String[] args) throws FileNotFoundException{
ApplicationModel apm = new ApplicationModel.getInstance();
}
}
This is my Singleton class ApplicationModel
package model;
// needed for ArrayLists
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ApplicationModel {
private static ApplicationModel instance = null;
private ApplicationModel()
{
}
public static ApplicationModel getInstance (){
if (instance == null){
instance = new ApplicationModel();
}
return instance;
}
private List<Shop> shops = new ArrayList<Shop>();
public List<Shop> getShops(){
return this.shops;
}
public void setShops(List<Shop> shops){
this.shops = shops;
}
public Shop createShop(String csvString){
String[] attributes = csvString.split(",");
Shop shop = new Shop(attributes[0],attributes[1],attributes[2],
attributes[3],attributes[4]);
return shop;
}
public List<Shop> readShops(String shopFileName){
ApplicationModel am = new ApplicationModel();
List<Shop> shopList = new ArrayList<>();
try{
Scanner naughty = new Scanner(new File(shopFileName));
if (naughty.hasNext()) naughty.nextLine();
while(naughty.hasNext()){
shopList.add(am.createShop(naughty.nextLine()));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ApplicationModel.class.getName()).log(Level.SEVERE, null, ex);
}
return shopList;
}
public String printShops(){
String listOfShops ="";
for(Shop shop : shops ){
listOfShops = listOfShops +'\n'+ shop.toString().trim() + '\n';
}
return listOfShops.trim();
}
}
Whenever I type in ApplicationModel in the main class, the import statement error stating that the import has not been used goes away too, I'm not sure what's wrong (I'm using netbeans). Can anyone help?
Remove "new" from your code:
ApplicationModel apm = ApplicationModel.getInstance();
Being static, getInstance() is a class method (not an instance method). This syntax is how you call class methods.
am new to Java. I am trying to decouple the Responder class from the WeatherSystem Class. But I get an error at Public NewResponder in the Responder class (invalid method declaration; return type required), I am really stuck at this point. I have tried changing all the class points at NewResponder and responder but can't rectify it. Could anyone point out why I am getting this issue, please?
(I also have InputReader class but that's not included below).
WeatherSystem Class
import java.util.HashSet;
public class WeatherSystem
{
private InputReader reader;
private NewResponder responder;
public WeatherSystem(NewResponder responder)
{
reader = new InputReader();
this.responder = new Responder();
}
public void start()
{
boolean finished = false;
printWelcome();
while(!finished) {
HashSet<String> input = reader.getInput();
if(input.contains("exit")) {
finished = true;
}
else {
String response = this.responder.generateResponse(input);
System.out.println(response);
}
}
printGoodbye();
.............................................
Class Responder
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import WeatherSystem.NewResponder
public class Responder implements NewResponder
{
private HashMap<String, String> responseMap;
private ArrayList<String> defaultResponses;
private Random randomGenerator;
public NewResponder()
{
responseMap = new HashMap<>();
defaultResponses = new ArrayList<>();
fillResponseMap();
fillDefaultResponses();
randomGenerator = new Random();
}
public String generateResponse(HashSet<String> words)
{
for (String word : words) {
String response = this.responseMap.get(word);
if(response != null) {
return response;
}
}
return pickDefaultResponse();
....................................................
You really need to check whether you need interface NewResponder inside WeatherSystem. Which you are implementing in Responder class with wrong constructor name.
I created two classes, but I do not know how to do the payload of my function which calculates the score of each terms.I don't know if i must create author classes , please someone can help me.
The first classe is:
import org.apache.lucene.analysis.payloads.PayloadHelper;
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.util.BytesRef;
public class BoostingSimilarity extends DefaultSimilarity {
public float scorePayload(int docID, int start, int end, BytesRef payload) {
float pload = 1.0f;
if (payload != null) {
pload = PayloadHelper.decodeFloat(payload.bytes);
}
System.out.println("===> docid: " + docID + " payload: " + pload);
return pload;
}}
The seconde classe is:
I added my idflocal function as follows, But I'm not sure if what I'm doing is right :
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.TokenFilter;
import java.io.IOException;
import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
import org.apache.lucene.analysis.payloads.PayloadHelper;
import org.apache.lucene.util.BytesRef;
import static Package.FonctionIDFlocal.idflocal;
public class BulletinPayloadsFilter extends TokenFilter {
private PayloadAttribute attr;
BulletinPayloadsFilter(TokenStream in,float idflocal) {
super(in);
attr = addAttribute(PayloadAttribute.class);
}
public final boolean incrementToken() throws IOException {
if (input.incrementToken()) {
BytesRef p =new BytesRef(PayloadHelper.encodeFloat(idflocal));;
attr.setPayload(p);
} else {
attr.setPayload(null);
}
return false;
}
}
What exactly are you trying? It looks like some custom scoring mechanism that scores documents according to a float value in the payload.
I'm writing an application in which data from a text file is saved to the array and late transferred to the widget GWT Highcharts as an array of Number type. I wrote a servlet that writes data from a file into an array, and I'm stuck here. I don't know how to pass the contents of the array to the client part of the application. Is there a quick and easy way to do this?
This code written by me:
DataPointsImpl.java:
package com.pwste.gwt.server;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.pwste.gwt.client.DataPoints;
public class DataPointsImpl extends RemoteServiceServlet implements DataPoints {
private static final long serialVersionUID = 1L;
#Override
public Number[] getDataPoints() throws IOException {
File dataFile = new File("points.txt");
FileReader dataFileReader = new FileReader(dataFile);
BufferedReader dataBufferedReader = new BufferedReader(dataFileReader);
Number[] arrayNumber = new Number[10000];
String dataString = dataBufferedReader.readLine();
for (int i = 0; i < arrayNumber.length; i++) {
arrayNumber[i] = Integer.parseInt(dataString);
dataString = dataBufferedReader.readLine();
}
dataBufferedReader.close();
return arrayNumber;
}
}
DataPoints.java:
package com.pwste.gwt.client;
import java.io.IOException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
#RemoteServiceRelativePath("dataPoints")
public interface DataPoints extends RemoteService {
Number[] getDataPoints() throws IOException;
}
DataPointsAsync.java:
package com.pwste.gwt.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface DataPointsAsync {
void getDataPoints(AsyncCallback<Number[]> callback);
}
You have to use the Async-Interface on the client side:
private DataPointsAsync dataPointsService = GWT.create(DataPoints.class);
you can use the service in this way:
dataPointsService.getDataPoints(AsyncCallback<Number[]>(){
#Override
public void onSuccess(Number[] result) {
// result contains the returning values
}
#Override
public void onFailure(Throwable caught) {
Window.alert("panic");
}
});
Can someone please help me see what the problem is. I realise that using
String kind = sc.next();
might bring a problem. if that's the issue how do i fix it. Thank you in advance. Here is the code.
import java.io.*;
import java.util.*;
public abstract class Account {
protected static AccountNumber accountNumber;
protected Customer customer = null; // not to be used yet
public abstract MeterNumber[] getMeterNumbers();
public abstract boolean exists(String meterNumber, String tariff);
public static Account load(Scanner sc) {
while (sc.hasNextLine()) {
AccountNumber accountNumber = AccountNumber.fromString(sc.nextLine());
String kind = sc.next();
sc.nextLine();
if (kind.equals("D")) {
return new DomesticAccount(sc, accountNumber);
} else {
return new CommercialAccount(sc, accountNumber);
}
} {
return null;
}
}
}
The code in main is as follows.
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Testt {
public static void main(String[] args) {
Account.load(new Scanner("Accounts3.txt"));
Map <AccountNumber, String> map1 = new HashMap <AccountNumber, String>();
map1.put(Account.accountNumber, "hello");
System.out.println(map1);
}
}
and this is the error I am getting.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at Account.load(Account.java:20)
at Testt.main(Testt.java:14)
Your are creating scanner on string object. Which is just "Accounts3.txt". Which is just one line.
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.lang.String)
I think you need to create scanner on file.
Refere this:
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.io.File)
So your main method will create scanner like this:
Account.load(new Scanner(new java.io.File("Accounts3.txt")));