<commit_before>/*
 * Copyright 2016 Searsia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.searsia;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;
import org.searsia.index.ResourceEngines;
import org.searsia.xpath.SearchEngine;

public class SearchResult {
	public  static final String SEARSIA_MIME_TYPE     = "application/searsia+json";
	public  static final String SEARSIA_MIME_ENCODING = SEARSIA_MIME_TYPE + "; charset=utf-8";
	private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final String TOKENIZER = "[^A-Za-z0-9]+";
	private List<Hit> hits;
	private SearchEngine resource;
	private Random random;
	
	public SearchResult() {
		this.hits = new ArrayList<Hit>();
		this.random = new Random();
	}
	
	public SearchResult(Hit hit) {
		this.hits = new ArrayList<Hit>();
		this.random = new Random();
		this.hits.add(hit);
	}
	
	public List<Hit> getHits() {
		return hits;
	}
	
	public void addHit(Hit hit) {
  		this.hits.add(hit);
	}
	
	public void setResource(SearchEngine resource) {
		this.resource = resource;
	}
	
	public SearchEngine getResource() {
		return this.resource;
	}
	
	// TODO: maybe a list of query-resource pairs, if result found by multiple engines for multiple queries.
	public void addQueryResourceRankDate(String query, String resourceID) {
		int rank = 1;
		for (Hit hit: this.hits) {
			hit.putIfEmpty("query", query);
			hit.putIfEmpty("rid", resourceID);  // TODO: if unknown rid, then replace!
			hit.putIfEmpty("rank", rank++);
			hit.putIfEmpty("time", df.format(new Date()));
		}
	}
	
	public void removeResourceRank() {
		for (Hit hit: this.hits) {
			hit.remove("rid");
			hit.remove("rank");
		}
	}
	
	// TODO: needs a proper implementation, refactoring, and research ;-) 
	// Scoring follows these rules:
	//   1. hits are ordered such that the first hit per rid determines the resource ranking
	//   2. if a resource has a exact query match, then these are ranked highest (given rule 1) 
	//   3. order by score (given rule 1 and rule 2)
	//   4. TODO: not more than x (=10?) hits per resource<commit_msg>changes time to beforeFound date<commit_after>/*
 * Copyright 2016 Searsia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.searsia;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;
import org.searsia.index.ResourceEngines;
import org.searsia.xpath.SearchEngine;

public class SearchResult {
	public  static final String SEARSIA_MIME_TYPE     = "application/searsia+json";
	public  static final String SEARSIA_MIME_ENCODING = SEARSIA_MIME_TYPE + "; charset=utf-8";
	private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private static final String TOKENIZER = "[^A-Za-z0-9]+";
	private List<Hit> hits;
	private SearchEngine resource;
	private Random random;
	
	public SearchResult() {
		this.hits = new ArrayList<Hit>();
		this.random = new Random();
	}
	
	public SearchResult(Hit hit) {
		this.hits = new ArrayList<Hit>();
		this.random = new Random();
		this.hits.add(hit);
	}
	
	public List<Hit> getHits() {
		return hits;
	}
	
	public void addHit(Hit hit) {
  		this.hits.add(hit);
	}
	
	public void setResource(SearchEngine resource) {
		this.resource = resource;
	}
	
	public SearchEngine getResource() {
		return this.resource;
	}
	
	// TODO: maybe a list of query-resource pairs, if result found by multiple engines for multiple queries.
	public void addQueryResourceRankDate(String query, String resourceID) {
		int rank = 1;
		for (Hit hit: this.hits) {
			hit.putIfEmpty("query", query);
			hit.putIfEmpty("rid", resourceID);  // TODO: if unknown rid, then replace!
			hit.putIfEmpty("rank", rank++);
			hit.putIfEmpty("foundBefore", df.format(new Date()));
		}
	}
	
	public void removeResourceRank() {
		for (Hit hit: this.hits) {
			hit.remove("rid");
			hit.remove("rank");
		}
	}
	
	// TODO: needs a proper implementation, refactoring, and research ;-) 
	// Scoring follows these rules:
	//   1. hits are ordered such that the first hit per rid determines the resource ranking
	//   2. if a resource has a exact query match, then these are ranked highest (given rule 1) 
	//   3. order by score (given rule 1 and rule 2)
	//   4. TODO: not more than x (=10?) hits per resource
{'fraction_non_alphanumeric': 0.10668983492615117, 'fraction_numerical': 0.007298001737619461, 'mean_word_length': 3.8862478777589136, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2968970', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1735, 'n_words': 612}