view src/main/java/de/mpiwg/itgroup/ismi/servlets/jsonmethods/JSONNormString.java @ 131:8ae989269f51

New ArabicNormalizer. NormalizerUtils.normalize() now does both translit and arabic normalization. 108: arabic normalization is not applied Task-Url: https://it-dev.mpiwg-berlin.mpg.de/tracs/ismi/ticket/108
author casties
date Thu, 02 Feb 2017 17:59:35 +0100
parents 34d0f1187b0f
children
line wrap: on
line source

package de.mpiwg.itgroup.ismi.servlets.jsonmethods;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.mpi.openmind.cache.WrapperService;
import org.mpi.openmind.repository.utils.NormalizerUtils;

public class JSONNormString extends AbstractServletJSONMethod {

    public static void execute(WrapperService ws, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        JSONObject json = new JSONObject();
        long startExecution = System.currentTimeMillis();

        try {
            String text = request.getParameter("text");
            String type = request.getParameter("type");
            if (StringUtils.isEmpty(type)) {
                json.put(RESPONSE, EXCEPTION);
                json.put(RESPONSE_INFO, "Normalization type parameter missing.");                
                PrintWriter out = response.getWriter();
                out.print(json.toString());
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, 
                        "Normalization type parameter missing.");
                return;
            }
            if (type.equalsIgnoreCase("arabic_translit")) {
                if (text != null) {
                    String normText = NormalizerUtils.normalizeArabicTranslit(text);
                    json.put("text", text);
                    json.put("normalization_type", "arabic_translit");
                    json.put("normalized_text", normText);
                }
            } else if (type.equalsIgnoreCase("arabic")) {
                if (text != null) {
                    String normText = NormalizerUtils.normalizeArabic(text);
                    json.put("text", text);
                    json.put("normalization_type", type);
                    json.put("normalized_text", normText);
                }
            }

        } catch (Exception e) {
            PrintWriter out = response.getWriter();
            out.print(json.toString());
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        } finally {
            json = finallyExecution(json, parametersToString(request.getParameterMap()), startExecution);
        }
        PrintWriter out = response.getWriter();
        out.print(json.toString());

    }
}