Search in sources :

Example 1 with ReconResult

use of com.google.refine.model.recon.StandardReconConfig.ReconResult in project OpenRefine by OpenRefine.

the class StandardReconConfigTests method deserializeReconResult.

@Test
public void deserializeReconResult() throws JsonParseException, JsonMappingException, IOException {
    String json = "{\"score\":100.0," + "\"match\":false," + "\"type\":[" + "   {\"id\":\"Q17366755\"," + "    \"name\":\"hamlet in Alberta\"}]," + "\"id\":\"Q5136635\"," + "\"name\":\"Cluny\"}";
    ReconResult rr = ParsingUtilities.mapper.readValue(json, ReconResult.class);
    assertEquals(rr.types.get(0).name, "hamlet in Alberta");
}
Also used : ReconResult(com.google.refine.model.recon.StandardReconConfig.ReconResult) RefineTest(com.google.refine.RefineTest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with ReconResult

use of com.google.refine.model.recon.StandardReconConfig.ReconResult in project OpenRefine by OpenRefine.

the class GuessTypesOfColumnCommand method guessTypes.

/**
 * Run relevance searches for the first n cells in the given column and
 * count the types of the results. Return a sorted list of types, from most
 * frequent to least.
 *
 * @param project
 * @param column
 * @return
 * @throws JSONException, IOException
 */
protected List<TypeGroup> guessTypes(Project project, Column column, String serviceUrl) throws IOException {
    Map<String, TypeGroup> map = new HashMap<String, TypeGroup>();
    int cellIndex = column.getCellIndex();
    List<String> samples = new ArrayList<String>(sampleSize);
    Set<String> sampleSet = new HashSet<String>();
    for (Row row : project.rows) {
        Object value = row.getCellValue(cellIndex);
        if (ExpressionUtils.isNonBlankData(value)) {
            String s = value.toString().trim();
            if (!sampleSet.contains(s)) {
                samples.add(s);
                sampleSet.add(s);
                if (samples.size() >= sampleSize) {
                    break;
                }
            }
        }
    }
    Map<String, IndividualQuery> queryMap = new HashMap<>();
    for (int i = 0; i < samples.size(); i++) {
        queryMap.put("q" + i, new IndividualQuery(samples.get(i), 3));
    }
    String queriesString = ParsingUtilities.defaultWriter.writeValueAsString(queryMap);
    String responseString;
    try {
        responseString = postQueries(serviceUrl, queriesString);
        ObjectNode o = ParsingUtilities.evaluateJsonStringToObjectNode(responseString);
        Iterator<JsonNode> iterator = o.iterator();
        while (iterator.hasNext()) {
            JsonNode o2 = iterator.next();
            if (!(o2.has("result") && o2.get("result") instanceof ArrayNode)) {
                continue;
            }
            ArrayNode results = (ArrayNode) o2.get("result");
            List<ReconResult> reconResults = ParsingUtilities.mapper.convertValue(results, new TypeReference<List<ReconResult>>() {
            });
            int count = reconResults.size();
            for (int j = 0; j < count; j++) {
                ReconResult result = reconResults.get(j);
                // score by each result's rank
                double score = 1.0 / (1 + j);
                List<ReconType> types = result.types;
                int typeCount = types.size();
                for (int t = 0; t < typeCount; t++) {
                    ReconType type = types.get(t);
                    double score2 = score * (typeCount - t) / typeCount;
                    if (map.containsKey(type.id)) {
                        TypeGroup tg = map.get(type.id);
                        tg.score += score2;
                        tg.count++;
                    } else {
                        map.put(type.id, new TypeGroup(type.id, type.name, score2));
                    }
                }
            }
        }
    } catch (IOException e) {
        logger.error("Failed to guess cell types for load\n" + queriesString, e);
        throw e;
    }
    List<TypeGroup> types = new ArrayList<TypeGroup>(map.values());
    Collections.sort(types, new Comparator<TypeGroup>() {

        @Override
        public int compare(TypeGroup o1, TypeGroup o2) {
            int c = Math.min(sampleSize, o2.count) - Math.min(sampleSize, o1.count);
            if (c != 0) {
                return c;
            }
            return (int) Math.signum(o2.score / o2.count - o1.score / o1.count);
        }
    });
    return types;
}
Also used : ReconType(com.google.refine.model.ReconType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashSet(java.util.HashSet) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IOException(java.io.IOException) ReconResult(com.google.refine.model.recon.StandardReconConfig.ReconResult) Row(com.google.refine.model.Row)

Aggregations

ReconResult (com.google.refine.model.recon.StandardReconConfig.ReconResult)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 RefineTest (com.google.refine.RefineTest)1 ReconType (com.google.refine.model.ReconType)1 Row (com.google.refine.model.Row)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 BeforeTest (org.testng.annotations.BeforeTest)1 Test (org.testng.annotations.Test)1