Click or drag to resize

imbSCI.BibTex Namespace

imbSCI | imbACE | imbNLP | imbWEM | imbWBI

imbSCI.BibTex is a comprehensive BibTex format API, allowing you to: Load, combine, modify and save BibTex citation definitions, access BibTex entries via value pair dictionaries (auto-discovery of entry fields) or via strong typed class; Extend basic BibTexEntryModel class to gain support less popular and/or special application BibTex tags, Generate Excel spreadsheet and CSV data table export, with data from BibTex file(s), High-level, single-call citation report generation, using one or all BibTex files from a directory, Encoding of LaTeX symbols to UTF-8 equivalents, and vice versa. Fast parsing, using multi-threading and compiled Regex queries; Generation of BibTex format output;

Classes
  ClassDescription
Public classBibTexCollectionT
Object model twin of BibTexDataFile, contains BibTexEntryModel instances
Public classBibTexDataFile
BibTex data with load and export methods
Public classBibTexEntryBase
BibTex untyped entry
Public classBibTexEntryModel
Strong typed object model, with data from/for BibTexEntryBase. Inherit this class to introduce support for additional BibTex tags.
Public classBibTexEntryTag
KeyValue entry, a property of a BibTexEntryBase
Public classBibTexExamples
Class with example - test methods
Public classBibTexLoadSettings
Settings for BibTex file loading and preprocessing
Public classBibTexSourceProcessor
Preprocessing text from Bibtex file
Public classBibTexTools
Tools for BibTex data conversion
Remarks
Examples
How to use the library
  1// --------------------------------------------------------------------------------------------------------------------
  2// <copyright file="BibTexExamples.cs" company="imbVeles" >
  3// 
  4// Copyright (C) 2018 imbVeles
  5// 
  6// This program is free software: you can redistribute it and/or modify
  7// it under the +terms of the GNU General Public License as published by
  8// the Free Software Foundation, either version 3 of the License, or
  9// (at your option) any later version.
 10// 
 11// This program is distributed in the hope that it will be useful,
 12// but WITHOUT ANY WARRANTY; without even the implied warranty of
 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14// GNU General Public License for more details.
 15// 
 16// You should have received a copy of the GNU General Public License
 17// along with this program.  If not, see http://www.gnu.org/licenses/. 
 18// </copyright>
 19// <summary>
 20// Project: imbSCI.BibTex
 21// Author: Goran Grubic
 22// ------------------------------------------------------------------------------------------------------------------
 23// Project web site: http://blog.veles.rs
 24// GitHub: http://github.com/gorangrubic
 25// Mendeley profile: http://www.mendeley.com/profiles/goran-grubi2/
 26// ORCID ID: http://orcid.org/0000-0003-2673-9471
 27// Email: hardy@veles.rs
 28// </summary>
 29// ------------------------------------------------------------------------------------------------------------------
 30using imbSCI.Core.extensions.text;
 31using imbSCI.Core.style.preset;
 32using imbSCI.Data.enums.fields;
 33using imbSCI.DataComplex.data;
 34using imbSCI.DataComplex.extensions.data.formats;
 35using imbSCI.DataComplex.tables;
 36using System;
 37using System.Data;
 38using System.Drawing;
 39using System.IO;
 40
 41namespace imbSCI.BibTex
 42{
 43    /// <summary>
 44    /// Class with example - test methods
 45    /// </summary>
 46    /// <see cref="Example1_Basic"/>
 47    /// <seealso cref="imbSCI.DataComplex.data.TestMicroEnvironmentBase" />
 48    public class BibTexExamples : TestMicroEnvironmentBase
 49    {
 50        /// <summary>Method demonstrating basic operation of loading BibTex file</summary>
 51        /** <example><para>Method demonstrating basic operation of loading BibTex file</para>
 52         *  <code> // Example 1: Loading BibTex file
 53            BibTexDataFile bib_1 = new BibTexDataFile("Resources\\test\\S0306457309000259.bib");
 54            // Converting BibTex data into object model dictionary
 55            System.Collections.Generic.Dictionary{string, BibTexEntryModel} model = bib_1.ConvertToModel(log);
 56            // Printing [Author : Title] to a ILogBuilder log builder
 57            foreach (var pair in model)
 58            {
 59                log.log(pair.Value.author.or("Unknown") + ": " + pair.Value.title);
 60            }
 61            </code></example>
 62        */
 63        public void Example1_Basic()
 64        {
 65            // Example 1: Loading BibTex file
 66            BibTexDataFile bib_1 = new BibTexDataFile("Resources\\test\\S0306457309000259.bib");
 67
 68            // Converting BibTex data into object model dictionary
 69            BibTexCollection<BibTexEntryModel> model = bib_1.ConvertToModel<BibTexEntryModel>(log);
 70
 71            // Printing [Author : Title] to a ILogBuilder log builder
 72            foreach (var pair in model)
 73            {
 74                log.log(pair.author.or("Unknown") + ": " + pair.title);
 75            }
 76        }
 77
 78        /// <summary>Load BibTex file, convert data into DataTable, create Excel file without Legend information and generate full Excel report</summary>
 79        /** <example><para>Load BibTex file, convert data into DataTable, create Excel file without Legend information and generate full Excel report</para>
 80         *  <code>
 81             // Example 2: Loading BibTex file
 82            String path = folderResources.findFile("S0306457309000259.bib", SearchOption.AllDirectories);
 83
 84            // initializes bibtex data file object
 85            BibTexDataFile bib = new BibTexDataFile();
 86
 87            // loads .bib or .bibtex file from path specified
 88            bib.Load(path, log);
 89
 90            // converts loaded BibTex entries into DataTable, with all columns discovered in the entries
 91            DataTable dt = bib.ConvertToDataTable();
 92
 93            // saves DataTable to Excel file, without adding Legend spreadsheet
 94            var finalPath = dt.serializeDataTable(Data.enums.reporting.dataTableExportEnum.excel, bib.name, folderResults, notation);
 95
 96            // creates extended version of Excel file, with additional spreadsheet for Legend and other meta information
 97            var reportDataTable_ref = dt.GetReportAndSave(folderResults, notation);</code></example>
 98        */
 99
100        public void Example2_LoadAndExport()
101        {
102            // Example 2: Loading BibTex file
103            String path = folderResources.findFile("S0306457309000259.bib", SearchOption.AllDirectories);
104
105            // initializes bibtex data file object
106            BibTexDataFile bib = new BibTexDataFile();
107
108            // loads .bib or .bibtex file from path specified
109            bib.Load(path, log);
110
111            // converts loaded BibTex entries into DataTable, with all columns discovered in the entries
112            DataTable dt = bib.ConvertToDataTable();
113
114            // saves DataTable to Excel file, without adding Legend spreadsheet
115            var finalPath = dt.serializeDataTable(Data.enums.reporting.dataTableExportEnum.excel, bib.name, folderResults, notation);
116
117            // creates extended version of Excel file, with additional spreadsheet for Legend and other meta information
118            var reportDataTable_ref = dt.GetReportAndSave(folderResults, notation);
119        }
120
121        /// <summary>
122        /// Short way for exporting BibTex files ...
123        /// </summary>
124        /** <example><para>Short way for exporting BibTex files ...</para>
125         *  <code>
126            // Example 3: Short way - using imbSCI find file
127            String path = folderResources.findFile("S0306457309000259.bib", SearchOption.AllDirectories);
128
129            // High-level method, creates extended version of Excel file, with additional spreadsheet for Legend and other meta information
130            var reportDataTable = BibTexTools.ExportToExcel(path, notation, log);
131
132            // Now, let's load all *.bib files
133            System.Collections.Generic.List<string> paths = folderResources.findFiles("*.bib", SearchOption.AllDirectories);
134
135            // Exports all *.bib files
136            System.Collections.Generic.List<DataTableForStatistics> exports = BibTexTools.ExportToExcel(paths, notation, log, null, folderResults);
137
138            // Printing filename and destination path, to a ILogBuilder log builder
139            foreach (var export in exports)
140            {
141                log.log(export.TableName + " exported to: " + export.lastFilePath);
142            }</code></example>
143        */
144
145    public void Example3_LoadAndExportToExcel_ShortWay()
146    {
147        // Example 3: Short way - using imbSCI find file
148        String path = folderResources.findFile("S0306457309000259.bib", SearchOption.AllDirectories);
149
150        // High-level method, creates extended version of Excel file, with additional spreadsheet for Legend and other meta information
151        var reportDataTable = BibTexTools.ExportToExcel(path, notation, log);
152
153        // Now, let's load all *.bib files
154        System.Collections.Generic.List<string> paths = folderResources.findFiles("*.bib", SearchOption.AllDirectories);
155
156        // Exports all *.bib files
157        System.Collections.Generic.List<DataTableForStatistics> exports = BibTexTools.ExportToExcel(paths, notation, log, null, folderResults);
158
159        // Printing filename and destination path, to a ILogBuilder log builder
160        foreach (var export in exports)
161        {
162            log.log(export.TableName + " exported to: " + export.lastFilePath);
163        }
164    }
165
166        /// <summary>Creation of BibTex entry from code, and generation of Excel table</summary>
167        /** <example><para>Creation of BibTex entry from code, and generation of Excel table</para>
168         *  <code>
169            // --- We create the entry and write its source code to the disk
170
171            // Creation of BibTex entry from code
172            BibTexEntryModel entry = new BibTexEntryModel()
173            {
174                EntryKey = "SOKOLOVA2009427",
175                EntryType = "article",
176                journal = "Information Processing & Management",
177                title = "A systematic analysis of performance measures for classification tasks",
178                keywords = "Performance evaluation, Machine Learning, Text classification",
179                year = 2005,
180                number = 2,
181                issn = "0000-0000",
182                @abstract = "Abs",
183                doi = "https://doi.org/10.1016/j.ipm.2009.03.002",
184                url = "http://www.sciencedirect.com/science/article/pii/S0306457309000259",
185                author = "Marina Sokolova and Guy Lapalme"
186            };
187
188            // New instance of TextProcessor object, this one you would share with other parts of your code.
189            BibTexSourceProcessor processor = new BibTexSourceProcessor();
190
191            // Generating BibTex code
192            String code = entry.GetSource(processor.latex, log);
193
194            // Making path
195            String path = folderResults.pathFor(nameof(Example4_UsingObjectModel) + ".txt");
196
197            File.WriteAllText(path, code);
198
199            // --- Now we export it to the Excel file
200
201            // Creation of data table collection
202            DataTableTypeExtended<BibTexEntryModel> bibTable = new DataTableTypeExtended<BibTexEntryModel>("RuntimeCreatedBibTex", "BibTex table, created in Run Time");
203            bibTable.AddRow(entry);
204
205            // creates extended version of Excel file, with additional spreadsheet for Legend and other meta information
206            var codeDataTable_ref = bibTable.GetReportAndSave(folderResults, notation, nameof(Example4_UsingObjectModel));
207        */
208        public void Example4_UsingObjectModel()
209        {
210            // --- We create the entry and write its source code to the disk
211
212            // Creation of BibTex entry from code
213            BibTexEntryModel entry = new BibTexEntryModel()
214            {
215                EntryKey = "SOKOLOVA2009427",
216                EntryType = "article",
217                journal = "Information Processing & Management",
218                title = "A systematic analysis of performance measures for classification tasks",
219                keywords = "Performance evaluation, Machine Learning, Text classification",
220                year = 2005,
221                number = 2,
222                issn = "0000-0000",
223                @abstract = "Abs",
224                doi = "https://doi.org/10.1016/j.ipm.2009.03.002",
225                url = "http://www.sciencedirect.com/science/article/pii/S0306457309000259",
226                author = "Marina Sokolova and Guy Lapalme"
227            };
228
229            // New instance of TextProcessor object, this one you would share with other parts of your code.
230            BibTexSourceProcessor processor = new BibTexSourceProcessor();
231
232            // Generating BibTex code
233            String code = entry.GetSource(processor.latex, log);
234
235            // Making path
236            String path = folderResults.pathFor(nameof(Example4_UsingObjectModel) + ".txt");
237
238            File.WriteAllText(path, code);
239
240            // --- Now we export it to the Excel file
241
242            // Creation of data table collection
243            DataTableTypeExtended<BibTexEntryModel> bibTable = new DataTableTypeExtended<BibTexEntryModel>("RuntimeCreatedBibTex", "BibTex table, created in Run Time");
244            bibTable.AddRow(entry);
245
246            // creates extended version of Excel file, with additional spreadsheet for Legend and other meta information
247            var codeDataTable_ref = bibTable.GetReportAndSave(folderResults, notation, nameof(Example4_UsingObjectModel));
248        }
249
250
251        /// <summary>
252        /// Example5s this instance.
253        /// </summary>
254        /** <example><para></para>
255         * <code> var files = folderResources.findFiles("*.bib*", SearchOption.AllDirectories);
256           var targetFolder = folderResults.Add("WithoutTemplate", "Without template", "Exporting Excel files without column data annotation template");
257           BibTexTools.ExportToExcel(files, notation, log, null, targetFolder);
258           targetFolder = folderResults.Add("WithTemplate", "With template", "Exporting Excel files with column data annotation template");
259           // creating template from Type
260           propertyAnnotationPreset template = new propertyAnnotationPreset(typeof(BibTexEntryModel));
261           template.defaultItem.definitions.Add(templateFieldDataTable.columnWidth, 10);
262           template.defaultItem.definitions.Add(templateFieldDataTable.col_color, "#FF6600");
263           BibTexTools.ExportToExcel(files, notation, log, template, targetFolder); </code>
264           </example>
265        */
266        public void Example5_SpecifyFormattingManually()
267        {
268            var files = folderResources.findFiles("*.bib*", SearchOption.AllDirectories);
269
270            var targetFolder = folderResults.Add("WithoutTemplate", "Without template", "Exporting Excel files without column data annotation template");
271
272            BibTexTools.ExportToExcel(files, notation, log, null, targetFolder);
273
274            targetFolder = folderResults.Add("WithTemplate", "With template", "Exporting Excel files with column data annotation template");
275
276            // creating template from Type
277            propertyAnnotationPreset template = new propertyAnnotationPreset(typeof(BibTexEntryModel));
278            template.defaultItem.definitions.Add(templateFieldDataTable.columnWidth, 10);
279            template.defaultItem.definitions.Add(templateFieldDataTable.col_color, "#FF6600");
280
281            template.GetAnnotationPresetItem(nameof(BibTexEntryModel.journal)).definitions.Add(templateFieldDataTable.col_color, Color.Red);
282
283            BibTexTools.ExportToExcel(files, notation, log, template, targetFolder);
284        }
285    }
286}