/* * [Venery.java] * * Summary: prepares a list of singular/plurals of animals for display on the web. * * Copyright: (c) 2005-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2005-01-01 initial * 1.1 2006-03-05 reformat with IntelliJ, add Javadoc. */ package com.mindprod.venery; import com.mindprod.common18.BigDate; import com.mindprod.common18.Build; import com.mindprod.common18.EIO; import com.mindprod.csv.CSVReader; import com.mindprod.csv.CSVWriter; import java.io.BufferedReader; import java.io.EOFException; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import static java.lang.System.*; /** * prepares a list of singular/plurals of animals for display on the web. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2006-03-05 reformat with IntelliJ, add Javadoc. * @since 2005-01-01 */ @SuppressWarnings( { "WeakerAccess" } ) public final class Venery { private static final boolean DEBUGGING = false; private static final int FIRST_COPYRIGHT_YEAR = 2005; /** * undisplayed copyright notice */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2005-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String RELEASE_DATE = "2006-03-06"; /** * embedded version string. */ private static final String VERSION_STRING = "1.1"; /** * pairs of animal/plural */ private static final ArrayList pairs = new ArrayList<>( 1600 ); private static final String DO_NOT_EDIT = "\n"; /** * and a pair to our collection of strange plurals * * @param singular usually an animal species, the singular word. * @param plural the word for a group of such creatures. */ private static void addPair( String singular, String plural ) { pairs.add( new Pair( singular, plural ) ); } /** * dump csv singular, plural, plurall * * @throws IOException if cannot write list */ private static void csvBySingular() throws IOException { final CSVWriter w = new CSVWriter( EIO.getPrintWriter( new File( Build.MINDPROD_SOURCE + "/venery/bysingular" + ".new.csv" ), 4 * 1024, EIO.UTF8 ) ); Pair prev = null; for ( Pair p : pairs ) { if ( prev == null || !p.getSingular().equals( prev.getSingular() ) ) { // finish old line if ( prev != null ) { w.nl(); } // start a new line w.put( p.getSingular() ); prev = p; } w.put( p.getPlural() ); } // finish last line w.nl(); w.close(); } /** * remove duplicate pairs, where both singular and plural match. */ private static void deDup() { // remove dups final int oldSize = pairs.size(); Pair prev = null; for ( int i = oldSize - 1; i >= 0; i-- ) { final Pair current = pairs.get( i ); if ( current.equals( prev ) ) { out.println( "removing duplicate " + current ); pairs.remove( i ); } else { prev = current; } } final int newSize = pairs.size(); out.println( ( oldSize - newSize ) + " duplicates removed" ); } /** * dump sorted, deduped list for examination. */ private static void dump() { for ( Pair p : pairs ) { out.println( p ); } } /** * dump HTML plural x singular * * @throws IOException if cannot write list */ private static void htmlByPlural() throws IOException { PrintWriter pw = EIO.getPrintWriter( new File( Build.MINDPROD_WEBROOT + "/jgloss/include/venerybyplural.htmlfrag" ), 4 * 1024, EIO.UTF8 ); Pair prev = null; int cellsInRow = 0; pw.print( DO_NOT_EDIT + "\n" + "\n" + "\n" + "\n" ); for ( Pair p : pairs ) { if ( prev == null || !p.getPlural().equals( prev.getPlural() ) ) { // finish old line if ( prev != null ) { pw.print( "\n" ); } // start a new line pw.print( "\n" ); prev = p; cellsInRow = 1; } if ( cellsInRow >= 10 ) { // split the row pw.print( "\n" ); pw.print( "\n" ); cellsInRow = 1; } pw.print( "\n" ); cellsInRow++; } // finish last line pw.print( "
\n" + "quaint plurals, collective nouns from the medievaral art of venery.\n" + "
Collective Nouns: The Art of Venery : Collective ×\n" + "Singular
CollectiveSingulars
" + p.getPlural() + "
 " + p.getSingular() + "
\n" ); pw.close(); } /** * dump HTML singular x plural * * @throws IOException if cannot write list */ private static void htmlBySingular() throws IOException { final PrintWriter pw = EIO.getPrintWriter( new File( Build.MINDPROD_WEBROOT + "/jgloss/include/venerybysingular.htmlfrag" ), 4 * 1024, EIO.UTF8 ); Pair prev = null; int cellsInRow = 0; pw.print( DO_NOT_EDIT + "\n" + "\n" + "\n" + "\n" ); for ( Pair p : pairs ) { if ( prev == null || !p.getSingular().equals( prev.getSingular() ) ) { // finish old line if ( prev != null ) { pw.print( "\n" ); } // start a new line pw.print( "\n" ); prev = p; cellsInRow = 1; } if ( cellsInRow >= 10 ) { // split the row pw.print( "\n" ); pw.print( "\n" ); cellsInRow = 1; } pw.print( "\n" ); cellsInRow++; } // finish last line pw.print( "
\n" + "quaint plurals, collective nouns from the medievaral art of venery.\n" + "
Collective Nouns: The Art of Venery : Singular ×\n" + "Collective
SingularCollectives
" + p.getSingular() + "
 " + p.getPlural() + "
\n" ); pw.close(); } /** * read data plural,singular,singular * * @throws IOException if cannot read list */ private static void readByPlural() throws IOException { // get all pairs plural/singular // reader, separatorChar, quoteChar, commentChars, hideComments, trimQuoted, allowMultipleLineFields final CSVReader r = new CSVReader( new BufferedReader( new FileReader( Build.MINDPROD_SOURCE + "/venery/byplural.csv" ) ) ); try { while ( true ) { String[] f = r.getAllFieldsInLine(); for ( int i = 1; i < f.length; i++ ) { // plural, singular addPair( f[ i ], f[ 0 ] ); } } // end while } catch ( EOFException e ) { // normal, do nothing } } /** * read data singlular, plural, plural * * @throws IOException if cannot read list */ private static void readBySingular() throws IOException { final CSVReader r = new CSVReader( new BufferedReader( new FileReader( Build.MINDPROD_SOURCE + "/venery/bysingular.csv" ) ) ); try { while ( true ) { final String[] f = r.getAllFieldsInLine(); for ( int i = 1; i < f.length; i++ ) { // singular, plural addPair( f[ 0 ], f[ i ] ); } } // end while } catch ( EOFException e ) { // normal, do nothing } } /** * Main method
Prepare website tables for the collective noun venery collection.
To add a new entry, insert * it in either
E:\com\mindprod\venery\bysinglur.csv or byplural.csv
* Duplicates are fine. Pairs don't have to appear in both files. *

* run venery.exe
It will generate * files:
E:/mindprod/jgloss/include/venerybysingular.htmlfrag
and
* E:/mindprod/jgloss/include/venerybyplural.htmlfrag
* They will automatically be included in the next publish.
* * @param args not used */ public static void main( String[] args ) { try { // read and merge our raw data by plural and by singular. // byplural.csv will usually be empty readByPlural(); readBySingular(); // sort pairs for deDup Collections.sort( pairs, new BySingular() ); // remove duplicates deDup(); // dump sorted, deduped list for examination. if ( DEBUGGING ) { dump(); } // generate html singularxplural in a table csvBySingular(); htmlBySingular(); // sort pairs by plural Collections.sort( pairs, new ByPlural() ); // generate html pluralxsingular in a table htmlByPlural(); } catch ( Exception e ) { err.println(); e.printStackTrace( err ); err.println(); } } // end main /** * sort Pairs by the plural. *

* Defines an alternate sort order for Pair. */ private static class ByPlural implements Comparator { /** * sort Pairs by the plural. * Defines an alternate sort order for Pair. * Compare two Pair Objects. * Compares plural then singular. * Informally, returns (a-b), or +ve if a is more positive than b. * * @param a first Pair to compare * @param b second Pair to compare * * @return +ve if a>b, 0 if a==b, -ve if a<b */ public final int compare( Pair a, Pair b ) { int diff = a.getPlural().compareTo( b.getPlural() ); if ( diff != 0 ) { return diff; } return a.getSingular().compareTo( b.getSingular() ); } } /** * sort Pairs by the singlular form. *

* Defines an alternate sort order for Pair. */ private static class BySingular implements Comparator { /** * sort Pairs by the singlular form. * Defines an alternate sort order for Pair. * Compare two Pair Objects. * Compares singular then plural. * Informally, returns (a-b), or +ve if a is more positive than b. * * @param a first Pair to compare * @param b second Pair to compare * * @return +ve if a>b, 0 if a==b, -ve if a<b */ public final int compare( Pair a, Pair b ) { int diff = a.getSingular().compareTo( b.getSingular() ); if ( diff != 0 ) { return diff; } return a.getPlural().compareTo( b.getPlural() ); } } } /** * a pair singular/plural *

* created with Intellij Idea * * @author Roedy Green, Canadian Mind Products */ final class Pair { /** * singular animal e.g. hamster */ private String plural; /** * group of animals e.g. horde */ private String singular; /** * constructor * * @param singular the singular for the animal. * @param plural the plural a group of such animals. */ Pair( String singular, String plural ) { if ( singular == null || singular.length() == 0 ) { throw new IllegalArgumentException( "bad singular: " + singular ); } if ( plural == null || plural.length() == 0 ) { throw new IllegalArgumentException( "bad plural: " + plural ); } this.singular = singular; this.plural = plural; } /** * Two Pairs are equal if both singular and plural fields match. * * @param o Object to compare with this one. * * @return true if objects are equal */ public boolean equals( Object o ) { if ( o != null && o instanceof Pair ) { Pair p = ( Pair ) o; return ( singular.equals( p.singular ) && plural.equals( p.plural ) ); } else { return false; } } public String getPlural() { return plural; } public String getSingular() { return singular; } /** * debug display of pair * * @return string to represent the pair. */ public String toString() { return singular + ":" + getPlural(); } }