use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class SemgrexPatternITest method testNERUniversalDependencies.
@Test
public void testNERUniversalDependencies() throws Exception {
String sentence = "John lives in Washington.";
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
props.setProperty("parse.originalDependencies", "false");
Annotation doc = new Annotation(sentence);
pipeline.annotate(doc);
CoreMap sent = doc.get(CoreAnnotations.SentencesAnnotation.class).get(0);
SemanticGraph graph = sent.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
graph.prettyPrint();
String patStr = "({word:/lives/} >/nmod:in/ {word:/\\QCalifornia\\E|\\QWashington\\E/} >nsubj {ner:PERSON})";
SemgrexPattern pat = SemgrexPattern.compile(patStr);
SemgrexMatcher mat = pat.matcher(graph, true);
assertTrue(mat.find());
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class Ssurgeon method createPatternXMLDoc.
private static Document createPatternXMLDoc(List<SsurgeonPattern> patterns) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document domDoc = db.newDocument();
Element rootElt = domDoc.createElement(SsurgeonPattern.ELT_LIST_TAG);
domDoc.appendChild(rootElt);
int ordinal = 1;
for (SsurgeonPattern pattern : patterns) {
Element patElt = domDoc.createElement(SsurgeonPattern.SSURGEON_ELEM_TAG);
patElt.setAttribute(SsurgeonPattern.ORDINAL_ATTR, String.valueOf(ordinal));
Element semgrexElt = domDoc.createElement(SsurgeonPattern.SEMGREX_ELEM_TAG);
semgrexElt.appendChild(domDoc.createTextNode(pattern.getSemgrexPattern().pattern()));
patElt.appendChild(semgrexElt);
Element uidElem = domDoc.createElement(SsurgeonPattern.UID_ELEM_TAG);
uidElem.appendChild(domDoc.createTextNode(pattern.getUID()));
patElt.appendChild(uidElem);
Element notesElem = domDoc.createElement(SsurgeonPattern.NOTES_ELEM_TAG);
notesElem.appendChild(domDoc.createTextNode(pattern.getNotes()));
patElt.appendChild(notesElem);
SemanticGraph semgrexGraph = pattern.getSemgrexGraph();
if (semgrexGraph != null) {
Element patNode = domDoc.createElement(SsurgeonPattern.SEMGREX_GRAPH_ELEM_TAG);
patNode.appendChild(domDoc.createTextNode(semgrexGraph.toCompactString()));
}
Element editList = domDoc.createElement(SsurgeonPattern.EDIT_LIST_ELEM_TAG);
patElt.appendChild(editList);
int editOrdinal = 1;
for (SsurgeonEdit edit : pattern.getEditScript()) {
Element editElem = domDoc.createElement(SsurgeonPattern.EDIT_ELEM_TAG);
editElem.setAttribute(SsurgeonPattern.ORDINAL_ATTR, String.valueOf(editOrdinal));
editElem.appendChild(domDoc.createTextNode(edit.toEditString()));
editList.appendChild(editElem);
editOrdinal++;
}
rootElt.appendChild(patElt);
ordinal++;
}
return domDoc;
} catch (Exception e) {
log.error(Ssurgeon.class.getName(), "createPatternXML");
log.error(e);
return null;
}
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class SsurgeonPattern method main.
/**
* Simply reads the given Ssurgeon pattern from file (args[0]), parses it, and prints it out.
* Use this for debugging the class and patterns.
*/
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: SsurgeonPattern FILEPATH [\"COMPACT_SEMANTIC_GRAPH\"], FILEPATH=path to ssurgeon pattern to parse and print., SENTENCE=test sentence (in quotes)");
System.exit(-1);
}
File tgtFile = new File(args[0]);
try {
Ssurgeon.inst().initLog(new File("./ssurgeon.log"));
Ssurgeon.inst().setLogPrefix("SsurgeonPattern test");
List<SsurgeonPattern> patterns = Ssurgeon.inst().readFromFile(tgtFile);
for (SsurgeonPattern pattern : patterns) {
System.out.println("- - - - -");
System.out.println(pattern);
}
if (args.length > 1) {
for (int i = 1; i < args.length; i++) {
String text = args[i];
SemanticGraph sg = SemanticGraph.valueOf(text);
Collection<SemanticGraph> generated = Ssurgeon.inst().exhaustFromPatterns(patterns, sg);
System.out.println("\n= = = = = = = = = =\nSrc text = " + text);
System.out.println(sg.toCompactString());
System.out.println("# generated = " + generated.size());
for (SemanticGraph genSg : generated) {
System.out.println(genSg);
System.out.println(". . . . .");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class CoNLLUDocumentReaderWriterTest method testComment.
public void testComment() {
CoNLLUDocumentReader reader = new CoNLLUDocumentReader();
Reader stringReader = new StringReader(COMMENT_TEST_INPUT);
Iterator<SemanticGraph> it = reader.getIterator(stringReader);
SemanticGraph sg = it.next();
assertNotNull(sg);
assertFalse("The input only contains one dependency tree.", it.hasNext());
assertEquals("[have/VBP nsubj>I/PRP neg>not/RB dobj>[clue/NN det>a/DT] punct>./.]", sg.toCompactString(true));
assertEquals(Integer.valueOf(3), sg.getNodeByIndex(1).get(CoreAnnotations.LineNumberAnnotation.class));
assertEquals(2, sg.getComments().size());
assertEquals("#comment line 1", sg.getComments().get(0));
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class CoNLLUDocumentReaderWriterTest method testSingleReadAndWrite.
/**
* Tests whether reading a Semantic Graph and printing it
* is equal to the original input.
*/
private void testSingleReadAndWrite(String input) {
String clean = input.replaceAll("[\\t ]+", "\t");
CoNLLUDocumentReader reader = new CoNLLUDocumentReader();
CoNLLUDocumentWriter writer = new CoNLLUDocumentWriter();
Reader stringReader = new StringReader(clean);
Iterator<SemanticGraph> it = reader.getIterator(stringReader);
SemanticGraph sg = it.next();
String output = writer.printSemanticGraph(sg);
assertEquals(clean, output);
}
Aggregations