<commit_before>package ch.ice.controller.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import ch.ice.controller.interf.SearchEngine;
import ch.ice.exceptions.NoUrlFoundException;
import ch.ice.utils.JSONStandardizedKeys;
import ch.ice.utils.JSONUtil;

public class GoogleSearchEngine implements SearchEngine {

	private static final Logger logger = LogManager.getLogger(GoogleSearchEngine.class.getName());

	public JSONArray search(String requestedQuery, int limitSearchResult) throws NoUrlFoundException {
		try {
			
			String accountKey = "";
			String config_cx = "";
			
	    	PropertiesConfiguration config;
	    	
	    	/*
			 * Load Configuration File
			 */
			try {
				config = new PropertiesConfiguration("conf/app.properties");
				
				accountKey = config.getString("searchEngine.google.accountKey");
				config_cx = config.getString("searchEngine.google.cx");
				
			} catch (ConfigurationException e) {
				System.out.println(e.getLocalizedMessage());
				e.printStackTrace();
			}

			String charset = Charset.defaultCharset().name();

			final String apiKey = URLEncoder.encode(accountKey, charset);
			final String cx =  URLEncoder.encode(config_cx, charset);

			/*
			 * for field options check:
			 * https://developers.google.com/apis-explorer/?hl=de#p/customsearch/v1/search.cse.list
			 */
			final String fields =  URLEncoder.encode("items(link,title),searchInformation/searchTime", charset);
			final String googleHost =  URLEncoder.encode("google.com", charset);
			int searchResultsLimit = limitSearchResult;

			requestedQuery = URLEncoder.encode(requestedQuery, charset);

			// google only ever returns max 10 results
			if(searchResultsLimit < 1){
				searchResultsLimit = 1;
			} else if(searchResultsLimit > 10){
				searchResultsLimit = 10;
			}
			
			String googleSearchUrl = "https://www.googleapis.com/customsearch/v1?q="+ requestedQuery +"&key="+ apiKey +"&cx="+ cx +"&googlehost="+ googleHost +"&fields="+ fields+"&num="+limitSearchResult;
			logger.info("Lookup Google with request URL: "+googleSearchUrl);
<commit_msg>Check for searchlimit. can only be max 10<commit_after>package ch.ice.controller.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import ch.ice.controller.interf.SearchEngine;
import ch.ice.exceptions.NoUrlFoundException;
import ch.ice.utils.JSONStandardizedKeys;
import ch.ice.utils.JSONUtil;

public class GoogleSearchEngine implements SearchEngine {

	private static final Logger logger = LogManager.getLogger(GoogleSearchEngine.class.getName());

	public JSONArray search(String requestedQuery, int limitSearchResult) throws NoUrlFoundException {
		try {
			
			String accountKey = "";
			String config_cx = "";
			
	    	PropertiesConfiguration config;
	    	
	    	/*
			 * Load Configuration File
			 */
			try {
				config = new PropertiesConfiguration("conf/app.properties");
				
				accountKey = config.getString("searchEngine.google.accountKey");
				config_cx = config.getString("searchEngine.google.cx");
				
			} catch (ConfigurationException e) {
				System.out.println(e.getLocalizedMessage());
				e.printStackTrace();
			}

			String charset = Charset.defaultCharset().name();

			final String apiKey = URLEncoder.encode(accountKey, charset);
			final String cx =  URLEncoder.encode(config_cx, charset);

			/*
			 * for field options check:
			 * https://developers.google.com/apis-explorer/?hl=de#p/customsearch/v1/search.cse.list
			 */
			final String fields =  URLEncoder.encode("items(link,title),searchInformation/searchTime", charset);
			final String googleHost =  URLEncoder.encode("google.com", charset);
			
			requestedQuery = URLEncoder.encode(requestedQuery, charset);

			// google only ever returns max 10 results
			if(limitSearchResult <= 1){
				limitSearchResult = 1;
			} else if(limitSearchResult >= 10){
				limitSearchResult = 10;
			}
			
			String googleSearchUrl = "https://www.googleapis.com/customsearch/v1?q="+ requestedQuery +"&key="+ apiKey +"&cx="+ cx +"&googlehost="+ googleHost +"&fields="+ fields+"&num="+limitSearchResult;
			logger.info("Lookup Google with request URL: "+googleSearchUrl);

{'fraction_non_alphanumeric': 0.11512027491408934, 'fraction_numerical': 0.004963726613211149, 'mean_word_length': 4.579339723109691, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '4738385', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1509, 'n_words': 316}