All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intersystems.iknow.languagemodel.slavic.MorphologicalAnalysisResultSerializer Maven / Gradle / Ivy

The newest version!
/*-
 * $Id: a407f4c4f962d2f4c390059c106dba903b8c14fb $
 */
package com.intersystems.iknow.languagemodel.slavic;

import java.lang.reflect.Type;
import java.util.Set;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * @author Andrey Shcheglov (mailto:[email protected])
 */
public final class MorphologicalAnalysisResultSerializer implements JsonSerializer {
	/**
	 * @see JsonSerializer#serialize(Object, Type, JsonSerializationContext)
	 */
	@Override
	public JsonElement serialize(final MorphologicalAnalysisResult src, final Type typeOfSrc, final JsonSerializationContext context) {
		final JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("language", src.getLanguage());
		jsonObject.addProperty("stem", src.getStem());
		final PartOfSpeech partOfSpeech = src.getPartOfSpeech();
		if (partOfSpeech != null) {
			jsonObject.addProperty("partOfSpeech", partOfSpeech.toString());
		}
		final Set categories = src.getCategories();
		if (!categories.isEmpty()) {
			final JsonArray serializedCategories = new JsonArray();
			categories.stream().map(GrammaticalCategory::toString).map(JsonPrimitive::new).forEach(serializedCategory -> serializedCategories.add(serializedCategory));
			jsonObject.add("categories", serializedCategories);
		}
		return jsonObject;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy