-
String to String Array
I'm having trouble looking for help on converting a string to a string array.
I have a program which asks for user input, once the user has input their sentence, the program tokenizes the sentences into the following format:
"word1", "word2", "word3", etc
I need to convert this into an array so that I can use a parser to parse the array.
I have looked at some examples but they all have arrays with a pre determined number of items. As the user will be inputting a sentence, the number of words they use can't really be pre-determined.
Does anyone have any links or suggestions I could look at to try resolving this?
Thank you in advance
/Mitty
-
If none of those "words" contain commas you could have a look at the String.split( ... ) method. Read the API documentation for the String class.
kind regards,
Jos
-
Hi Jos, Thanks for your reply.
I have the following code.
Code:
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
class FileInput{
public static void main(String[] args)throws IOException{
LexicalizedParser lp = new LexicalizedParser("C:/Documents and Settings/Mitesh/Desktop/eclipse/stanford-parser-2010-02-26/bin/englishPCFG.ser.gz");
Writer output = null;
String text = JOptionPane.showInputDialog("Please enter your sentence here");
String text2 = null;
File file = new File("UserInput.txt");
output = new BufferedWriter(new FileWriter(file));
StringTokenizer st2;
st2 = new StringTokenizer (text, " ");
while (st2.hasMoreElements()){
// System.out.println(" \" " + st2.nextToken() + " \", ");
text2 = ("\"" + st2.nextToken() + "\",");
//text2 = {"one", "two", "three" };
System.out.println (text2);
output.write (text2);
}
Tree parse = (Tree) lp.apply(Arrays.asList(text2));
//parse.pennPrint();
System.out.println();
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();
System.out.println(tdl);
System.out.println(text2);
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
System.err.println("Penn format:");
TreePrint treePrint = new TreePrint("penn");
treePrint.printTree(parse);
output.close ();
if(file.exists()) {
String absolutePathOfFirstFile = file.getAbsolutePath();
System.out.println("Saved to" + absolutePathOfFirstFile);
} else {
System.out.println("file does not exist");
}
}
}
This returns the following
Code:
Loading parser from serialized file C:/Documents and Settings/Mitesh/Desktop/eclipse/stanford-parser-2010-02-26/bin/englishPCFG.ser.gz ... done [4.1 sec].
"one",
"two",
"three",
[]
"three",
(ROOT
(NP (NNP "three",)))
Penn format:
(ROOT
(NP (NNP "three",)))
Saved toC:\Documents and Settings\Mitesh\Desktop\eclipse\stanford-parser-2010-02-26\UserInput.txt
This is when the user inputs the words one two three.
I'm trying to put the one two three into an Array as another class of the program requires the words to be in an array to parse them. At the moment, the only word which is being parsed is the last word as this is the final word in the array. It is not saving them in an array but is replacing the word each time until the final word.
Mitty