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

lv.semti.morphology.webservice.TezaursWordResource Maven / Gradle / Ivy

Go to download

Webservice API for Tēzaurs.lv and other ailab.lv Latvian computational linguistic tools

There is a newer version: 2.5.7
Show newest version
/*******************************************************************************
 * Copyright 2012, 2013, 2014 Institute of Mathematics and Computer Science, University of Latvia
 * Author: Pēteris Paikens
 * 
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see .
 *******************************************************************************/
package lv.semti.morphology.webservice;


import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import java.io.*;
import java.lang.reflect.Type;
import java.net.URLDecoder;
import java.util.*;

public class TezaursWordResource extends ServerResource {
    private static Map entries;

    @Get("json")
	public String retrieve() throws Exception {
        getResponse().setAccessControlAllowOrigin("*");
		String query = (String) getRequest().getAttributes().get("query");
        if (query != null)
            try {
                query = URLDecoder.decode(query,"UTF8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        if (query == null || query.isEmpty()) {
            System.out.println("redirecting to wordlist");
            this.redirectPermanent("http://api.tezaurs.lv/wordlist_sorted.txt");
        }

        getEntries();

        String result = entries.get(query);
        return result;
	}

    private static class JsonElementJsonDeserializer implements JsonDeserializer {
        @Override
        public String deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return json.toString();
        }
    }
	
    public static Map getEntries() throws Exception {
        if (entries != null) return entries;

        entries = new HashMap<>();
        Map temp_entries = new HashMap<>();
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(String.class, new JsonElementJsonDeserializer())
                .create();
        JsonParser parser = new JsonParser();
        System.out.println("Loading thesaurus entries");
//        List files = Arrays.asList("entries-bad.json", "entries-good.json", "entries-noParadigm.json", "references-bad.json", "references-good.json", "references-noParadigm.json");
        List files = Arrays.asList("analyzed_tezaurs.json");
        for (String filename : files) {
            filename = "/tezaurs/"+filename;
            InputStream stream = MorphoServer.class.getClass().getResourceAsStream(filename);
            if (stream == null) {
                throw new Exception("Resource " + filename + " not found.");
            }
            JsonReader reader = new JsonReader(new InputStreamReader(stream));
            reader.setLenient(true);
            reader.beginArray();
            while (reader.hasNext()) {
                String entry = gson.fromJson(reader, String.class);
                if (entry == null || entry.isEmpty()) continue;
                JsonObject obj = parser.parse(entry).getAsJsonObject();
                String lemma = obj.getAsJsonObject("Header").get("Lemma").getAsString();
                JsonArray entrylist = temp_entries.get(lemma);
                if (entrylist == null) entrylist = new JsonArray();
                entrylist.add(obj);
                temp_entries.put(lemma, entrylist);
                entries.put(lemma, entrylist.toString());
            }
            System.out.println(filename + " loaded.");
        }
        return entries;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy