<commit_before><commit_msg>New example for how to extract data<commit_after>package examples;

/*
 * #%L
 * Wikidata Toolkit Examples
 * %%
 * Copyright (C) 2014 Wikidata Toolkit Developers
 * %%
 * 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.
 * #L%
 */

import java.io.IOException;
import java.io.PrintStream;

import org.wikidata.wdtk.datamodel.interfaces.EntityDocumentProcessor;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;
import org.wikidata.wdtk.datamodel.interfaces.SiteLink;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;

/**
 * This simple {@link EntityDocumentProcessor} finds all items with a GND
 * identifier (property P227), and extracts for each of them the id, GND value,
 * as well as English and German labels and Wikipedia articles, if any. The
 * results are written to a CSV file "extracted-data.csv". The property can be
 * modified by changing the value for
 * {@link DataExtractionProcessor#numberPropertyId}. The current code only
 * extracts the first value for this property if many are given.
 *
 *
 * @author <NAME>
 *
 */
public class DataExtractionProcessor implements EntityDocumentProcessor {

	static final String numberPropertyId = "P227"; // "GND identifier"

	int itemsWithPropertyCount = 0;
	int itemCount = 0;
	PrintStream out;

	/**
	 * Main method. Processes the whole dump using this processor. To change
	 * which dump file to use and whether to run in offline mode, modify the
	 * settings in {@link ExampleHelpers}.
	 *
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		ExampleHelpers.configureLogging();
		DataExtractionProcessor.printDocumentation();

		DataExtractionProcessor processor = new DataExtractionProcessor();
		ExampleHelpers.processEntitiesFromWikidataDump(processor);
		processor.close();
	}

	public DataExtractionProcessor() throws IOException {
		// open file for writing results:
		out = new PrintStream(
				ExampleHelpers.openExampleFileOuputStream("extracted-data.csv"));
		// write CSV header:
		out.println("ID,Label (en),Label (de),Value,Wikipedia (en),Wikipedia (de)");
	}

	@Override
	public void processItemDocument(ItemDocument itemDocument) {
		this.itemCount++;

		// Find the first value for this property, if any:
		StringValue stringValue = itemDocument
				.findStatementStringValue(numberPropertyId);

		// If a value was found, write the data:
		if (stringValue != null) {
			this.itemsWithPropertyCount++;
			out.print(itemDocument.getItemId().getId());
			out.print(",");
			out.print(csvEscape(itemDocument.findLabel("en")));
			out.print(",");
			out.print(csvEscape(itemDocument.findLabel("de")));
			out.print(",");
			out.print(csvEscape(stringValue.getString()));
			out.print(",");
			SiteLink enwiki = itemDocument.getSiteLinks().get("enwiki");
			if (enwiki != null) {
				out.print(csvEscape(enwiki.getPageTitle()));
			} else {
				out.print("\"\"");
			}
			out.print(",");
			SiteLink dewiki = itemDocument.getSiteLinks().get("dewiki");
			if (dewiki != null) {
				out.print(csvEscape(dewiki.getPageTitle()));
			} else {
				out.print("\"\"");
			}
			out.println();
		}

		// Print progress every 100,000 items:
		if (this.itemCount % 100000 == 0) {
			printStatus();
		}
	}

	@Override
	public void processPropertyDocument(PropertyDocument propertyDocument) {
		// Nothing to do
	}

	/**
	 * Escapes a string for use in CSV. In particular, the string is quoted and
	 * quotation marks are escaped.
	 *
	 * @param string
	 *            the string to escape
	 * @return the escaped string
	 */
	private String csvEscape(String string) {
		if (string == null) {
			return "\"\"";
		} else {
			return "\"" + string.replace("\"", "\"\"") + "\"";
		}
	}

	/**
	 * Prints the current status, time and entity count.
	 */
	public void printStatus() {
		System.out.println("Found " + this.itemsWithPropertyCount
				+ " matching items after scanning " + this.itemCount
				+ " items.");
	}

	/**
	 * Prints some basic documentation about this program.
	 */
	public static void printDocumentation() {
		System.out
				.println("********************************************************************");
		System.out.println("*** Wikidata Toolkit: DataExtractionProcessor");
		System.out.println("*** ");
		System.out
				.println("*** This program will download and process dumps from Wikidata.");
		System.out
				.println("*** It will scan the dump to find the item with values for property");
		System.out.println("*** " + numberPropertyId
				+ " and print some data for these items to a CSV file. ");
		System.out.println("*** See source code for further details.");
		System.out
				.println("********************************************************************");
	}

	public void close() {
		printStatus();
		this.out.close();
	}
}

{'fraction_non_alphanumeric': 0.14436815735758027, 'fraction_numerical': 0.005381332343663017, 'mean_word_length': 4.0092936802973975, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2638059', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1522, 'n_words': 479}