Commit c01d21c8 by Sam Sussman

Inital Commitnb

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>AvgSentanceLength</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
package cs6300.AvgSentanceLength;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class AvgSentanceLength {
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "";
String delimiters = ".!?;";
short minWordLength = 3;
if(args.length > 0)
fileName = args[0];
else
{
PrintHelp();
return;
}
for(int i = 1; i < args.length; i++ ) //has -d or -l
{
if(args[i].startsWith("-"))
{
switch(args[i])
{
case "-d":
if(args.length > ++i)
delimiters = args[i];
else{
PrintHelp();
return;
}
break;
case "-l":
try{
if(args.length > ++i)
minWordLength = (short) Integer.parseInt(args[i]);
else{
PrintHelp();
return;
}
}
catch(NumberFormatException e){
System.out.println("Minimum length paramter must be a valid number");
return;
}
}
}
}
List<String> lines = null;
try {
lines = FileContent(fileName, Charset.defaultCharset());
} catch (IOException e) {
System.out.println("Invalid file path: " + e.getMessage());
return;
}
System.out.println(averageSentenceLength(lines,delimiters,minWordLength));
}
public static int averageSentenceLength(List<String> lines, String delimiters, int minWordLength)
{
int total = 0;
double sentencesCount = 0.0;
if(lines != null)
{
for(String line : lines)
{
String[] sentences = GetSentences(line,delimiters);
for(String sentence : sentences )
{
sentencesCount++;
total += GetSentenceLength(sentence,minWordLength);
}
}
}
return (int)Math.floor(total/sentencesCount);
}
public static String[] GetSentences(String line, String delimiters){
return line.split("["+delimiters+"]");
}
public static int GetSentenceLength(String sentence, int minWordLength)
{
int length = 0;
String[] words = sentence.split(" ");
for(String word : words )
{
if(word.length() >= minWordLength)
{
length++;
}
}
return length;
}
public static List<String> FileContent(String fileName, Charset encoding) throws IOException
{
return Files.readAllLines(Paths.get(fileName),encoding);
}
public static void PrintHelp()
{
System.out.println("AvgSentanceLength.exe path [-d delimters] [-l minWordLength]\n\n"
+ "-d delimiter defaults to .;?!\n"
+ "-l Minimum word length default to 3 \n\n"
+ "Example: AveSentanceLength.exe C:\\MyFile.txt -d .!?; -l 3");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment