/** * Builds a model from a turtle representation in a file * * @param path */ protected Model readModel(String path) { Model model = null; if (path != null) { model = ModelFactory.createDefaultModel(); InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path); String fakeUri = "http://w3c.github.io/ldp-testsuite/fakesubject"; // Even though null relative URIs are used in the resource representation file, // the resulting model doesn't keep them intact. They are changed to "file://..." if // an empty string is passed as base to this method. model.read(inputStream, fakeUri, "TURTLE"); // At this point, the model should contain a resource named // "http://w3c.github.io/ldp-testsuite/fakesubject" if // there was a null relative URI in the resource representation // file. Resource subject = model.getResource(fakeUri); if (subject != null) { ResourceUtils.renameResource(subject, ""); } try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return model; }
/** * Central method of the class, reading the corpus file and filling an OntModel for output * * @param fileIn * @param inputModel * @param outputModel */ public void transformConLL( Reader input, OntModel inputModel, OntModel outputModel, NIFParameters nifParameters) { BufferedReader reader = null; try { reader = new BufferedReader(input); String line; int offset = 0; List<String> sentence = new ArrayList<String>(); String contextString = ""; int count = 0; System.out.println("parsing file"); while ((line = reader.readLine()) != null) { count++; if (count % 1000 == 0) { System.out.println(count); } if (!line.isEmpty()) { sentence.add(line); } else { this.transformSentenceToConLL( sentence, contextString, offset, inputModel, outputModel, nifParameters); contextString += tempSentence; this.tempSentence = new String(); offset = contextString.length(); sentence = new ArrayList<String>(); } } contextResource.addProperty( NIFDatatypeProperties.endIndex.getDatatypeProperty(outputModel), offset + ""); contextResource.addLiteral( NIFDatatypeProperties.isString.getDatatypeProperty(outputModel), outputModel.createLiteral(contextString)); ResourceUtils.renameResource(contextResource, contextResource.getURI() + offset); reader.close(); } catch (IOException ioe) { log.error("Could not read input"); return; } return; }