//Copyright © 2014 whac-a-troll.blogspot.com, legal to distribute/share/use/compile/alter for non-commercial purposes
//Java API imports
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.ArrayList;
public final class PopTechStudiesParser{
public static void main(String[] args){
String filePathStr="C:/"; //Edit as per your location of source text file
String sourceFileStr="studies.txt";
String outputFileStr=filePathStr + "results.txt";
ArrayList authorsAL=new ArrayList(),orderedAuthorsAL=new ArrayList();
/*Positions in the studiesArr[] integer array to be incremented each time
the parser finds a chronological publishing match:
[0] = Studies published before 1991
[1] = Studies published during and after 1991
[2] = Total studies
*/
int[] studiesArr={0,0,0};
try{
BufferedReader br=new BufferedReader(new FileReader(new File(filePathStr + sourceFileStr)));
String line="";int counter=0;
while((line=br.readLine())!=null){
line=line.trim();
//Check for the closing parenthesis character, which can indicate
//the previous four digits are the year published
if(line!=null && !(line.isEmpty()) && line.indexOf(")")!=-1){
String checkLineStr=line.substring((line.indexOf(")")-5),line.indexOf(")"));
//Make sure we don't have a year for a referenced study...for example
//"Reply to Risbey (2002)" which is indicated by the opening parenthesis character
if(checkLineStr.indexOf("(")==-1){
String maybeDateStr=checkLineStr.substring(1).trim();
//Parse the potential date character string to make
//sure each character is a digit (or number) and not a
//letter or some other character which would mean
//something other than a year
boolean digitsB=true;
char[] nCharArr=maybeDateStr.toCharArray();
for(int x=0;x<nCharArr.length;x++) if(!(Character.isDigit(nCharArr[x]))) digitsB=false;
//OK...if we passed all the above if() statement tests,
//we make one final if() check, and then assume that we have a
//character string which indicates the year the study was
//published. We then go ahead and increment the appropriate
//array position and the total number of studies...
if(digitsB){
if(new Integer(maybeDateStr.trim()).intValue()<1991)
studiesArr[0]=studiesArr[0]+1;
else if(new Integer(maybeDateStr.trim()).intValue()>=1991)
studiesArr[1]=studiesArr[1]+1;
studiesArr[2]=studiesArr[2]+1;
}// end if block
//Succumb to coding OCD and try to force garbage collecting :)
nCharArr=null;maybeDateStr=null;
}//end if block
//Succumb to coding OCD and try to force garbage collecting :)
checkLineStr=null;
}// end if block
//Now let's check for author names and increment their appearances
//NOTE: Only lead authors are considered for "et al." studies as
//populartechnology.net's list does not include the other authors
else if(line!=null && !(line.isEmpty()) && line.startsWith("- ")){
String authorsStr=line.substring(2).trim();
while(authorsStr.indexOf(",")!=-1){
String firstAuthorStr=authorsStr.substring(0,authorsStr.indexOf(",")).trim();
authorsStr=authorsStr.substring(authorsStr.indexOf(",")+2);
//Now look for author name in Arraylist...if present,
//increment...if not, add name and 1 instance to ArrayList
boolean inArrayList=false;
for(int x=0;x<authorsAL.size();x++){
ArrayList a=(ArrayList)authorsAL.remove(x);
String authStr=(String)a.get(0);
String numStr=(String)a.get(1);
if(firstAuthorStr.equals(authStr)){
inArrayList=true;
ArrayList b=new ArrayList();
b.add(authStr);b.add(new Integer((new Integer(numStr).intValue())+1).toString());
authorsAL.add(x,b);
//Succumb to coding OCD and try to force garbage collecting :)
b=null;
}// end if block
else authorsAL.add(x,a);
//Succumb to coding OCD and try to force garbage collecting :)
authStr=null;numStr=null;a=null;
}// end for loop
if(!(inArrayList)){
ArrayList c=new ArrayList();
c.add(firstAuthorStr);c.add(new String("1"));
authorsAL.add(c);
//Succumb to coding OCD and try to force garbage collecting :)
c=null;
}// end if block
//Succumb to coding OCD and try to force garbage collecting :)
firstAuthorStr=null;
}// end while loop
//Do it again one last time for the last author in the list
//that didn't have a comma after his or her name...
boolean inArrayList=false;
for(int x=0;x<authorsAL.size();x++){
ArrayList a=(ArrayList)authorsAL.remove(x);
String authStr=(String)a.get(0);
String numStr=(String)a.get(1);
if(authorsStr.equals(authStr)){
inArrayList=true;
ArrayList b=new ArrayList();
b.add(authStr);b.add(new Integer((new Integer(numStr).intValue())+1).toString());
authorsAL.add(x,b);
//Succumb to coding OCD and try to force garbage collecting :)
b=null;
}// end if block
else authorsAL.add(x,a);
//Succumb to coding OCD and try to force garbage collecting :)
authStr=null;numStr=null;a=null;
}// end for loop
if(!(inArrayList)){
ArrayList c=new ArrayList();
c.add(authorsStr);c.add(new String("1"));
authorsAL.add(c);
//Succumb to coding OCD and try to force garbage collecting :)
c=null;
}// end if block
//Succumb to coding OCD and try to force garbage collecting :)
authorsStr=null;
}// end if block
}// end while loop
br.close();
BufferedWriter outputBW=new BufferedWriter(new FileWriter(outputFileStr));
outputBW.write("The breakdown of populartechnology.net's February 12, 2014 study list post...");
outputBW.newLine();
outputBW.newLine();
outputBW.write("The number of studies published before 1991 = " + studiesArr[0]);
outputBW.newLine();
outputBW.write("The number of studies published during and after 1991 = " + studiesArr[1]);
outputBW.newLine();
outputBW.write("The total number of studies = " + studiesArr[2]);
outputBW.newLine();
outputBW.newLine();
outputBW.write("The author credit tally...");
outputBW.newLine();
//Order the author list from greatest number of credits to least, and then print it out...
while(!(authorsAL.isEmpty())){
int greatest=-1;
int index=-1;
for(int x=0;x<authorsAL.size();x++){
ArrayList a=(ArrayList)authorsAL.get(x);
String numStr=(String)a.get(1);
if((new Integer(numStr).intValue())>greatest){
greatest=new Integer(numStr).intValue();
index=x;
}// end if block
//Succumb to coding OCD and try to force garbage collecting :)
numStr=null;a=null;
}// end for loop
orderedAuthorsAL.add(authorsAL.remove(index));
}//end while loop
for(int x=0;x<orderedAuthorsAL.size();x++){
ArrayList a=(ArrayList)orderedAuthorsAL.get(x);
String authStr=(String)a.get(0);
String numStr=(String)a.get(1);
outputBW.write(authStr + " " + numStr + " credits.");
outputBW.newLine();
//Succumb to coding OCD and try to force garbage collecting :)
authStr=null;numStr=null;a=null;
}// end for loop
outputBW.close();
//Succumb to coding OCD and try to force garbage collecting :)
outputBW=null;br=null;line=null;
}// end try block
catch(Exception e){
System.out.println("ERROR!");
System.out.println(e.getMessage());
e.printStackTrace();
}//end catch block
//Succumb to coding OCD and try to force garbage collecting :)
filePathStr=null;studiesArr=null;outputFileStr=null;sourceFileStr=null;authorsAL=null;orderedAuthorsAL=null;
}// end main() method
}// end class
No comments:
Post a Comment