org.apache.commons.lang3.text.translate.UnicodeEscaper Java Examples

The following examples show how to use org.apache.commons.lang3.text.translate.UnicodeEscaper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TaskManagerBean.java    From AIDR with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
@Asynchronous
public void importTrainingDataForClassifier(Long targetCollectionId, Long sourceCollectionId, Long nominalAttributeId) {
	try {
		
		List<Long> nominalLabelIds = remoteNominalLabelEJB.getNominalLabelIdsByAttributeID(nominalAttributeId);
		List<DocumentDTO> documentDTOs = remoteDocumentEJB.getDocumentForNominalLabelAndCrisis(nominalLabelIds, sourceCollectionId);
		
		CollectionDTO collectionDTO = remoteCrisisEJB.findCrisisByID(targetCollectionId);
		CollectionDTO sourceCollection = remoteCrisisEJB.findCrisisByID(sourceCollectionId);
		
		// save model family
		ModelFamilyDTO modelFamilyDTO = new ModelFamilyDTO();
		modelFamilyDTO.setCrisisDTO(collectionDTO);
		NominalAttributeDTO attributeDTO = new NominalAttributeDTO();
		attributeDTO.setNominalAttributeId(nominalAttributeId);
		modelFamilyDTO.setNominalAttributeDTO(attributeDTO);
		modelFamilyDTO.setIsActive(true);
		boolean success = modelFamilyResourceFacade.addCrisisAttribute(modelFamilyDTO);
		
		if(success) {
			
			// iterate through each tagged document 
			for(DocumentDTO documentDTO : documentDTOs) {
				DocumentDTO documentToSave = new DocumentDTO();
				documentToSave.setCrisisDTO(collectionDTO);
				documentToSave.setData(UnicodeEscaper.outsideOf(32, 0x7f).translate(documentDTO.getData()));
				documentToSave.setGeoFeatures(documentDTO.getGeoFeatures());
				documentToSave.setDoctype(documentDTO.getDoctype());
				documentToSave.setHasHumanLabels(true);
				documentToSave.setLanguage(documentDTO.getLanguage());
				documentToSave.setWordFeatures(UnicodeEscaper.outsideOf(32, 0x7f).translate(documentDTO.getWordFeatures()));
				documentToSave.setValueAsTrainingSample(documentDTO.getValueAsTrainingSample());
				documentToSave.setIsEvaluationSet(documentDTO.getIsEvaluationSet());
				documentToSave.setReceivedAt(documentDTO.getReceivedAt());
				documentToSave.setSourceCollection(sourceCollection);
				
				// save new document
				DocumentDTO newDocument = remoteDocumentEJB.addDocument(documentToSave);
				
				// fetch document nominal label for existing doc
				List<DocumentNominalLabelDTO> documentNominalLabelDTOs = remoteDocumentNominalLabelEJB.findLabeledDocumentListByID(documentDTO.getDocumentID());
				
				// add new document labels
				if(documentNominalLabelDTOs != null) {
					for(DocumentNominalLabelDTO documentNominalLabelDTO : documentNominalLabelDTOs) {
						DocumentNominalLabelDTO labelDTOToSave = new DocumentNominalLabelDTO();
						labelDTOToSave.setDocumentDTO(newDocument);
						labelDTOToSave.setNominalLabelDTO(documentNominalLabelDTO.getNominalLabelDTO());
						labelDTOToSave.setIdDTO(new DocumentNominalLabelIdDTO(newDocument.getDocumentID(), 
										documentNominalLabelDTO.getIdDTO().getNominalLabelId(), documentNominalLabelDTO.getIdDTO().getUserId()));
						this.saveDocumentNominalLabel(labelDTOToSave);
					}
				}
				
				// fetch task answers for existing doc
				List<TaskAnswerDTO> answers = remoteTaskAnswerEJB.getTaskAnswer(documentDTO.getDocumentID());
				
				// save task answers 
				for(TaskAnswerDTO answer : answers) {
					TaskAnswerDTO answerToSave = new TaskAnswerDTO();
					answerToSave.setAnswer(answer.getAnswer());
					answerToSave.setDocumentID(newDocument.getDocumentID());
					
					answerToSave.setUserID(answer.getUserID());
					answerToSave.setTimestamp(new Date());
					remoteTaskAnswerEJB.insertTaskAnswer(answerToSave);
				}
			}
		}
	} catch (Exception e) {
		logger.error("Error in importing training data for collection id : " + sourceCollectionId +
				" and attribute : " + nominalAttributeId, e);
		
	}
}