use of org.apache.tika.eval.io.ExtractReaderException in project tika by apache.
the class ExtractProfiler method processFileResource.
@Override
public boolean processFileResource(FileResource fileResource) {
Metadata metadata = fileResource.getMetadata();
EvalFilePaths fps = null;
if (inputDir != null && inputDir.equals(extracts)) {
//crawling an extract dir
fps = getPathsFromExtractCrawl(metadata, extracts);
} else {
fps = getPathsFromSrcCrawl(metadata, inputDir, extracts);
}
int containerId = ID.incrementAndGet();
String containerIdString = Integer.toString(containerId);
ExtractReaderException.TYPE extractExceptionType = null;
List<Metadata> metadataList = null;
try {
metadataList = extractReader.loadExtract(fps.getExtractFile());
} catch (ExtractReaderException e) {
extractExceptionType = e.getType();
}
Map<Cols, String> contOutput = new HashMap<>();
Long srcFileLen = getSourceFileLength(fps, metadataList);
contOutput.put(Cols.LENGTH, srcFileLen > NON_EXISTENT_FILE_LENGTH ? Long.toString(srcFileLen) : "");
contOutput.put(Cols.CONTAINER_ID, containerIdString);
contOutput.put(Cols.FILE_PATH, fps.getRelativeSourceFilePath().toString());
if (fps.getExtractFileLength() > 0) {
contOutput.put(Cols.EXTRACT_FILE_LENGTH, (fps.getExtractFile() == null) ? "" : Long.toString(fps.getExtractFileLength()));
}
try {
writer.writeRow(CONTAINER_TABLE, contOutput);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (extractExceptionType != null) {
try {
writeExtractException(EXTRACT_EXCEPTION_TABLE, containerIdString, fps.getRelativeSourceFilePath().toString(), extractExceptionType);
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
List<Integer> numAttachments = countAttachments(metadataList);
int i = 0;
for (Metadata m : metadataList) {
//the first file should have the same id as the container id
String fileId = (i == 0) ? containerIdString : Integer.toString(ID.incrementAndGet());
writeProfileData(fps, i, m, fileId, containerIdString, numAttachments, PROFILE_TABLE);
writeEmbeddedPathData(i, fileId, m, EMBEDDED_FILE_PATH_TABLE);
writeExceptionData(fileId, m, EXCEPTION_TABLE);
try {
writeContentData(fileId, m, FIELD, CONTENTS_TABLE);
} catch (IOException e) {
throw new RuntimeException(e);
}
i++;
}
return true;
}
use of org.apache.tika.eval.io.ExtractReaderException in project tika by apache.
the class ExtractComparer method compareFiles.
//protected for testing, should find better way so that this can be private!
protected void compareFiles(EvalFilePaths fpsA, EvalFilePaths fpsB) throws IOException {
ExtractReaderException.TYPE extractExceptionA = null;
ExtractReaderException.TYPE extractExceptionB = null;
List<Metadata> metadataListA = null;
if (extractExceptionA == null) {
try {
metadataListA = extractReader.loadExtract(fpsA.getExtractFile());
} catch (ExtractReaderException e) {
extractExceptionA = e.getType();
}
}
List<Metadata> metadataListB = null;
try {
metadataListB = extractReader.loadExtract(fpsB.getExtractFile());
} catch (ExtractReaderException e) {
extractExceptionB = e.getType();
}
//array indices for those metadata items handled in B
Set<Integer> handledB = new HashSet<>();
String containerID = Integer.toString(ID.getAndIncrement());
//container table
Map<Cols, String> contData = new HashMap<>();
contData.put(Cols.CONTAINER_ID, containerID);
contData.put(Cols.FILE_PATH, fpsA.getRelativeSourceFilePath().toString());
long srcFileLength = getSourceFileLength(metadataListA, metadataListB);
contData.put(Cols.LENGTH, srcFileLength > NON_EXISTENT_FILE_LENGTH ? Long.toString(srcFileLength) : "");
contData.put(Cols.FILE_EXTENSION, FilenameUtils.getExtension(fpsA.getRelativeSourceFilePath().getFileName().toString()));
long extractFileLengthA = getFileLength(fpsA.getExtractFile());
contData.put(Cols.EXTRACT_FILE_LENGTH_A, extractFileLengthA > NON_EXISTENT_FILE_LENGTH ? Long.toString(extractFileLengthA) : "");
long extractFileLengthB = getFileLength(fpsB.getExtractFile());
contData.put(Cols.EXTRACT_FILE_LENGTH_B, extractFileLengthB > NON_EXISTENT_FILE_LENGTH ? Long.toString(extractFileLengthB) : "");
writer.writeRow(COMPARISON_CONTAINERS, contData);
if (extractExceptionA != null) {
writeExtractException(EXTRACT_EXCEPTION_TABLE_A, containerID, fpsA.getRelativeSourceFilePath().toString(), extractExceptionA);
}
if (extractExceptionB != null) {
writeExtractException(EXTRACT_EXCEPTION_TABLE_B, containerID, fpsB.getRelativeSourceFilePath().toString(), extractExceptionB);
}
if (metadataListA == null && metadataListB == null) {
return;
}
List<Integer> numAttachmentsA = countAttachments(metadataListA);
List<Integer> numAttachmentsB = countAttachments(metadataListB);
//now get that metadata
if (metadataListA != null) {
for (int i = 0; i < metadataListA.size(); i++) {
//the first file should have the same id as the container id
String fileId = (i == 0) ? containerID : Integer.toString(ID.getAndIncrement());
Metadata metadataA = metadataListA.get(i);
Metadata metadataB = null;
//TODO: shouldn't be fileA!!!!
writeProfileData(fpsA, i, metadataA, fileId, containerID, numAttachmentsA, PROFILES_A);
writeExceptionData(fileId, metadataA, EXCEPTION_TABLE_A);
int matchIndex = getMatch(i, metadataListA, metadataListB);
if (matchIndex > -1 && !handledB.contains(matchIndex)) {
metadataB = metadataListB.get(matchIndex);
handledB.add(matchIndex);
}
if (metadataB != null) {
writeProfileData(fpsB, i, metadataB, fileId, containerID, numAttachmentsB, PROFILES_B);
writeExceptionData(fileId, metadataB, EXCEPTION_TABLE_B);
}
writeEmbeddedFilePathData(i, fileId, metadataA, metadataB);
//prep the token counting
tokenCounter.clear(FIELD_A);
tokenCounter.clear(FIELD_B);
//write content
try {
writeContentData(fileId, metadataA, FIELD_A, CONTENTS_TABLE_A);
writeContentData(fileId, metadataB, FIELD_B, CONTENTS_TABLE_B);
} catch (IOException e) {
throw new RuntimeException(e);
}
//now run comparisons
if (tokenCounter.getTokenStatistics(FIELD_A).getTotalTokens() > 0 && tokenCounter.getTokenStatistics(FIELD_B).getTotalTokens() > 0) {
Map<Cols, String> data = new HashMap<>();
data.put(Cols.ID, fileId);
ContrastStatistics contrastStatistics = tokenContraster.calculateContrastStatistics(tokenCounter.getTokens(FIELD_A), tokenCounter.getTokenStatistics(FIELD_A), tokenCounter.getTokens(FIELD_B), tokenCounter.getTokenStatistics(FIELD_B));
writeContrasts(data, contrastStatistics);
writer.writeRow(CONTENT_COMPARISONS, data);
}
}
}
//that haven't yet been handled.
if (metadataListB != null) {
for (int i = 0; i < metadataListB.size(); i++) {
if (handledB.contains(i)) {
continue;
}
Metadata metadataB = metadataListB.get(i);
//the first file should have the same id as the container id
String fileId = (i == 0) ? containerID : Integer.toString(ID.getAndIncrement());
writeProfileData(fpsB, i, metadataB, fileId, containerID, numAttachmentsB, PROFILES_B);
writeEmbeddedFilePathData(i, fileId, null, metadataB);
writeExceptionData(fileId, metadataB, EXCEPTION_TABLE_B);
//prep the token counting
tokenCounter.clear(FIELD_B);
//write content
try {
writeContentData(fileId, metadataB, FIELD_B, CONTENTS_TABLE_B);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
Aggregations