<commit_before>/**
 * Copyright 2014 McGill University
 *
 * 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 ca.mcgill.cs.creco.data;

import java.io.IOException;

public class CRData 
{
	private static CRData instance = null;
	
	private CategoryList catList;
	private ProductList prodList;
	
	private double JACCARD_THRESHHOLD = 0.8;
	private static String[] DEFAULT_PRODUCT_FILENAMES = 
	{
		"appliances.json", "electronicsComputers.json",
		"cars.json", "health.json", "homeGarden.json", 
		"food.json", "babiesKids.json", "money.json"
	};
	private static String DEFAULT_CATEGORY_FILENAME = "category.json";

	/**
	 * Private constructor. Use CRData.getData() instead.
	 * @throws IOException
	 */
	private CRData() throws IOException
	{
		this(DEFAULT_PRODUCT_FILENAMES, DEFAULT_CATEGORY_FILENAME);
	}
	
	/**
	 * Private constructor. Use CRData.getData() instead.
	 * @throws IOException
	 */
	private CRData(String[] productFileNames, String categoryFileName) throws IOException
	{
		String dataPath = DataPath.get();
		
		// Build the CategoryList
		catList = CategoryReader.read(dataPath, categoryFileName, JACCARD_THRESHHOLD);
		catList.eliminateSingletons();
		
		// Build the products list
		ProductList prodList = ProductReader.read(dataPath, productFileNames);
		
		// Put links from products to categories and vice-versa
		catList.associateProducts(prodList);
		
		// Roll up useful pre-processed statistics and find equivalence classes
		catList.refresh();
		catList.findEquivalenceClasses();		
	}
	
	/**
	 * Initializes the CR Data on the first call, and subsequently
	 * returns the singleton instance of the CR data.
	 * @return CRData singleton
	 * @throws IOException
	 */
	public static CRData getData() throws IOException
	{
		if (instance == null)
		{
			instance = new CRData();
		}
		return instance;
	}

	/**
	 * Initializes the CRData based on specified filenames.
	 * @return CRData singleton
	 * @throws IOException
	 */
	public static CRData setupWithFileNames(String[] productFileNames, String categoryFileName) throws IOException 
	{
		if (instance == null)
		{
			instance = new CRData(productFileNames, categoryFileName);
		} else
		{
			throw new IOException("CR Database was already initialized. Use CRData.getData() instead.");
		}
		return instance;
		
	}
	
	
	public String[] getProductFileNames()
	{
		return CRData.DEFAULT_PRODUCT_FILENAMES;
	}
	
	public CategoryList getCategoryList()
	{
		return this.catList;
	}
	
	public ProductList getProductList()
	{
		return this.prodList;
	}
	
}<commit_msg>Issue #22 Cleaned up CRData<commit_after>/**
 * Copyright 2014 McGill University
 *
 * 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 ca.mcgill.cs.creco.data;

import java.io.IOException;

/**
 * Root of the object graph representing the consumer reports database. 
 * All of the data is accessible through a singleton CRData object. Just 
 * create a new CRData object to get started. Normally in production this
 * will be built when the server starts up, and might be provided to your
 * class by a master controller. For now, make one yourself:
 * CRData crData = new CRData(DataPath.get()); 
 * The main entry points to the data are via the CategoryList and ProductList.
 * CategoryList catList = crData.getCategoryList();
 * ProductList prodList = crData.getProductList(); 
 * These provide access to categories or products by id, and are iterables.
 */
public final class CRData 
{
	private static final double JACCARD_THRESHOLD = 0.8;
	
	private static final String DEFAULT_CATEGORY_FILENAME = "category.json";
	
	private static final String[] DEFAULT_PRODUCT_FILENAMES = {
			"appliances.json", "electronicsComputers.json",
			"cars.json", "health.json", "homeGarden.json", 
			"food.json", "babiesKids.json", "money.json"
		};
	
	private static CRData instance = null;
	
	private CategoryList aCategoryList;
	private ProductList aProductList;
	
	private CRData(String[] pProductFileNames, String pCategoryFileName) throws IOException
	{
		String dataPath = DataPath.get();
		
		// Build the CategoryList
		aCategoryList = CategoryReader.read(dataPath, pCategoryFileName, JACCARD_THRESHOLD);
		aCategoryList.eliminateSingletons();
		
		// Build the products list
		ProductList prodList = ProductReader.read(dataPath, pProductFileNames);
		
		// Put links from products to categories and vice-versa
		aCategoryList.associateProducts(prodList);
		
		// Roll up useful pre-processed statistics and find equivalence classes
		aCategoryList.refresh();
		aCategoryList.findEquivalenceClasses();		
	}
	
	/**
	 * Initializes the CR Data on the first call, and subsequently
	 * returns the singleton instance of the CR data.
	 * @return CRData singleton
	 * @throws IOException if the data cannot be accessed
	 */
	public static CRData getData() throws IOException
	{
		if (instance == null)
		{
			instance = new CRData(DEFAULT_PRODUCT_FILENAMES, DEFAULT_CATEGORY_FILENAME);
		}
		return instance;
	}

	/**
	 * Initializes the CRData based on specified filenames.
	 * @return CRData singleton
	 * @param pProductFileNames The list of file names for the product data.
	 * @param pCategoryFileName The name of the category file.
	 * @throws IOException If the database was already initialized.
	 */
	public static CRData setupWithFileNames(String[] pProductFileNames, String pCategoryFileName) throws IOException 
	{
		if (instance == null)
		{
			instance = new CRData(pProductFileNames, pCategoryFileName);
		} 
		else
		{
			throw new IOException("CR Database was already initialized. Use CRData.getData() instead.");
		}
		return instance;
	}
	
	/**
	 * @return The list of categories.
	 */
	public CategoryList getCategoryList() 
	{ return aCategoryList; }
	
	/**
	 * @return The list of products.
	 */
	public ProductList getProductList() 
	{ return aProductList; }
	
}
{'fraction_non_alphanumeric': 0.08486707566462168, 'fraction_numerical': 0.0032135553607946245, 'mean_word_length': 4.034558823529411, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3219912', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1869, 'n_words': 682}