com.intersystems.iknow.languagemodel.slavic.MorphologicalAnalysisResultSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of languagemodel-slavic Show documentation
Show all versions of languagemodel-slavic Show documentation
Slavic Language Model for use with iKnow/iFind
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 extends GrammaticalCategory> 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;
}
}