<commit_before>/*
    ***** BEGIN LICENSE BLOCK *****
    
    Copyright © 2009-2011 Center for History and New Media
                          George Mason University, Fairfax, Virginia, USA
                          http://zotero.org
    
    This file is part of Zotero.
    
    Zotero is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    Zotero is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with Zotero.  If not, see <http://www.gnu.org/licenses/>.
    
    ***** END LICENSE BLOCK *****
*/

/**
 * Handles UI for lookup panel
 * @namespace
 */
const Zotero_Lookup = new function () {
	/**
	 * Performs a lookup by DOI, PMID, or ISBN
	 */
	this.accept = function(textBox) {
		var identifier = textBox.value;
		//first look for DOIs
		var ids = identifier.split(/[\s\u00A0]+/);	//whitespace + non-breaking space
		var items = [], doi;
		for(var i=0, n=ids.length; i<n; i++) {
			if(doi = Zotero.Utilities.cleanDOI(ids[i])) {
				items.push({itemType:"journalArticle", DOI:doi});
			}
		}

		//then try ISBNs
		if(!items.length) {
			//first try replacing dashes
			ids = identifier.replace(/[\u002D\u00AD\u2010-\u2015\u2212]+/g, "");	//hyphens and dashes

			var ISBN_RE = /(?:\D|^)(\d{10}|\d{13})(?!\d)/g;
			var isbn;

			while(isbn = ISBN_RE.exec(ids)) {
				items.push({itemType:"book", ISBN:isbn[1]});
			}

			//now try spaces
			if(!items.length) {
				ids = ids.replace(/[ \u00A0]+/g, "");	//space + non-breaking space
				while(isbn = ISBN_RE.exec(ids)) {
					items.push({itemType:"book", ISBN:isbn[1]});
				}
			}
		}

		//finally try for PMID
		if(!items.length) {
			// PMID; right now, PMIDs are 8 digits, so there doesn't seem like we will need to
			// discriminate for a fairly long time
			var PMID_RE = /(?:\D|^)(\d{8})(?!\d)/g;
			var pmid;
			while(pmid = PMID_RE.exec(identifier)) {
				items.push({itemType:"journalArticle", contextObject:"rft_id=info:pmid/"+pmid[1]});
			}
		}

		if(!items.length) {
			var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
											.getService(Components.interfaces.nsIPromptService);
			prompts.alert(window, Zotero.getString("lookup.failure.title"),
				Zotero.getString("lookup.failureToID.description"));
			return false;
		}

		var notDone = items.length;	 //counter for asynchronous fetching
		var successful = 0;					//counter for successful retrievals

		var libraryID = null;
		var collection = false;
		try {
			libraryID = ZoteroPane_Local.getSelectedLibraryID();
			collection = ZoteroPane_Local.getSelectedCollection();
		} catch(e) {}

		Zotero_Lookup.toggleProgress(true);

		var item;
		while(item = items.pop()) {
			(function(item) {
				var translate = new Zotero.Translate("search");
				translate.setSearch(item);

				// be lenient about translators
				var translators = translate.getTranslators();
				translate.setTranslator(translators);

				translate.setHandler("done", function(translate, success) {
					notDone--;
					successful += success;

					if(!notDone) {	//i.e. done
						Zotero_Lookup.toggleProgress(false);
						if(successful) {
							document.getElementById("zotero-lookup-panel").hidePopup();
						} else {
							var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
																			.getService(Components.interfaces.nsIPromptService);
							prompts.alert(window, Zotero.getString("lookup.failure.title"),
								Zotero.getString("lookup.failure.description"));
						}
					}
				});

				translate.setHandler("itemDone", function(obj, item) {
					if(collection) collection.addItem(item.id);<commit_msg>Some tweaks to ISBN discovery<commit_after>/*
    ***** BEGIN LICENSE BLOCK *****
    
    Copyright © 2009-2011 Center for History and New Media
                          George Mason University, Fairfax, Virginia, USA
                          http://zotero.org
    
    This file is part of Zotero.
    
    Zotero is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    Zotero is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with Zotero.  If not, see <http://www.gnu.org/licenses/>.
    
    ***** END LICENSE BLOCK *****
*/

/**
 * Handles UI for lookup panel
 * @namespace
 */
const Zotero_Lookup = new function () {
	/**
	 * Performs a lookup by DOI, PMID, or ISBN
	 */
	this.accept = function(textBox) {
		var identifier = textBox.value;
		//first look for DOIs
		var ids = identifier.split(/[\s\u00A0]+/);	//whitespace + non-breaking space
		var items = [], doi;
		for(var i=0, n=ids.length; i<n; i++) {
			if(doi = Zotero.Utilities.cleanDOI(ids[i])) {
				items.push({itemType:"journalArticle", DOI:doi});
			}
		}

		//then try ISBNs
		if(!items.length) {
			//first try replacing dashes
			ids = identifier.replace(/[\u002D\u00AD\u2010-\u2015\u2212]+/g, "")	//hyphens and dashes
											.toUpperCase();

			var ISBN_RE = /(?:\D|^)(97[89]\d{10}|\d{9}[\dX])(?!\d)/g;
			var isbn;

			while(isbn = ISBN_RE.exec(ids)) {
				isbn = Zotero.Utilities.cleanISBN(isbn[1]);
				if(isbn) items.push({itemType:"book", ISBN:isbn});
			}

			//now try spaces
			if(!items.length) {
				ids = ids.replace(/[ \u00A0]+/g, "");	//space + non-breaking space
				while(isbn = ISBN_RE.exec(ids)) {
					isbn = Zotero.Utilities.cleanISBN(isbn[1]);
					if(isbn) items.push({itemType:"book", ISBN:isbn});
				}
			}
		}

		//finally try for PMID
		if(!items.length) {
			// PMID; right now, PMIDs are 8 digits, so it doesn't seem like we will need to
			// discriminate for a fairly long time
			var PMID_RE = /(?:\D|^)(\d{8})(?!\d)/g;
			var pmid;
			while(pmid = PMID_RE.exec(identifier)) {
				items.push({itemType:"journalArticle", contextObject:"rft_id=info:pmid/"+pmid[1]});
			}
		}

		if(!items.length) {
			var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
											.getService(Components.interfaces.nsIPromptService);
			prompts.alert(window, Zotero.getString("lookup.failure.title"),
				Zotero.getString("lookup.failureToID.description"));
			return false;
		}

		var libraryID = null;
		var collection = false;
		try {
			libraryID = ZoteroPane_Local.getSelectedLibraryID();
			collection = ZoteroPane_Local.getSelectedCollection();
		} catch(e) {
			/** TODO: handle this **/
		}

		var notDone = items.length;	 //counter for asynchronous fetching
		var successful = 0;					//counter for successful retrievals

		Zotero_Lookup.toggleProgress(true);

		var item;
		while(item = items.pop()) {
			(function(item) {
				var translate = new Zotero.Translate.Search();
				translate.setSearch(item);

				// be lenient about translators
				var translators = translate.getTranslators();
				translate.setTranslator(translators);

				translate.setHandler("done", function(translate, success) {
					notDone--;
					successful += success;

					if(!notDone) {	//i.e. done
						Zotero_Lookup.toggleProgress(false);
						if(successful) {
							document.getElementById("zotero-lookup-panel").hidePopup();
						} else {
							var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
																			.getService(Components.interfaces.nsIPromptService);
							prompts.alert(window, Zotero.getString("lookup.failure.title"),
								Zotero.getString("lookup.failure.description"));
						}
					}
				});

				translate.setHandler("itemDone", function(obj, item) {
					if(collection) collection.addItem(item.id);
{'fraction_non_alphanumeric': 0.13612817732803276, 'fraction_numerical': 0.011203469461510662, 'mean_word_length': 2.995187680461983, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '3604542', 'n_tokens_mistral': 3189, 'n_tokens_neox': 2631, 'n_words': 663}