In: Computer Science
Question 13
We define the letters 'a', 'e', 'i', 'o' and 'u' as vowels. We do not consider any other letter as a vowel.
Write a function named initialVowels() that returns a list of words in a body of text that begin with a vowel.
Include both capitalized and lower case instances of the vowels.
A word should appear in the return list at most once, no matter how many times is occurs in the input string.
Input: a string that consists of words, separated by spaces
Return: an list of words in the input string that begin with an upper or lower case vowel
For example, the following would be correct output:
>>> mlk = 'Our lives begin to end the day we become silent about things that matter'
>>> print(initialVowels(mlk))
['Our','about']
Question 14
The three words 'a', 'an' and 'the' are the only articles in the English language.
Write a function named countArticles(). Hint: Count both capitalized and lower case instances of articles.
Input: a string, named sentence
Return: the number of words in sentence that are articles
For example, the following would be correct output:
>>> theFlea = ['The flea is a mighty insect']
>>> print(articleCount(theFlea))
>>> 2
Question 15
Write a function named pluralCount() that takes two string parameters.
The first parameter is the name of an input file that exists before pluralCount() is called.
The second parameter is the name of an output file that pluralCount() creates and writes to.
You may assume that the input file is in the current working directory and you should write the output file to that directory as well.
For each line in the input file, the function pluralCount() should write to the output file the number of words in the line that end in the letter 's'.
For example, if the following is the content of the file foxInSocks.txt:
Look, sir. Look, sir. Mr. Knox, sir.
Let's do tricks with bricks and blocks, sir.
Let's do tricks with chicks and clocks, sir.
The following function call:
inF = 'foxInSocks.txt'
outF = 'foxRepLines.txt'
pluralCount(inF, outF)
should create the file ‘foxRepLines.txt’ with the content:
0
4
4
This class is for PLural count
package accountProject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Vowels{
private static final String FILENAME=
"C:\\Users\\cr\\Desktop\\input.txt";
private static final String FILENAME1 =
"C:\\Users\\cr\\Desktop\\output.txt";
static ArrayList<Integer> countlist;
public static ArrayList<Integer> countlist(File
file){
countlist=new
ArrayList<Integer>();
BufferedReader br = null;
FileReader fr = null;
FileWriter fw=null;
BufferedWriter bw=null;
try {
fr = new
FileReader(file);
br = new
BufferedReader(fr);
String
sCurrentLine;
br = new
BufferedReader(new FileReader(file));
while
((sCurrentLine = br.readLine()) != null) {
int count=0;
System.out.println(sCurrentLine);
String wordl[]=sCurrentLine.split(" ");
for(String word:wordl){
if(word.replace(",",
"").toLowerCase().endsWith("s")){
count++;
}
}
countlist.add(count);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println(countlist.toString());
return countlist;
}
public static void WriteToFile(File file){
BufferedWriter bw = null;
FileWriter fw = null;
try {
// if file
doesnt exists, then create it
if
(!file.exists()) {
file.createNewFile();
}
// true =
append file
fw = new
FileWriter(file.getAbsoluteFile(), true);
bw = new
BufferedWriter(fw);
File input=new
File(FILENAME);
File output=new
File(FILENAME1);
ArrayList<Integer>list=Vowels.countlist(input);
for(Integer
i:list){
bw.write(i.toString());
bw.newLine();
}
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
Vowels v=new Vowels();
File file=new File(FILENAME);
File file1=new
File(FILENAME1);
v.countlist(file);
v.WriteToFile(file1);
}
}
THis is main class
package accountProject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class VowelCount {
private static final String INPUT=
"C:\\Users\\cr\\Desktop\\input.txt";
private static final String OUTPUT =
"C:\\Users\\cr\\Desktop\\output.txt";
public static void PluralCount(File input,File
output){
ArrayList<Integer>countlist=Vowels.countlist(input);
Vowels.WriteToFile(output);
}
public static int countArtical(String word){
int count=0;
String [] list_words=word.split("
");
for(String s:list_words){
if(s.toLowerCase().startsWith("a") ||
s.toLowerCase().startsWith("the") ||
s.toLowerCase().startsWith("the") ){
count++;
}
}
return count;
}
public static ArrayList<String>
getVowelList(String s){
ArrayList<String> list=new
ArrayList<String>();
String [] list_words=s.split("
");
for(String word:list_words){
if(word.toLowerCase().startsWith("a") ||
word.toLowerCase().startsWith("e") ||
word.toLowerCase().startsWith("i") ||
word.toLowerCase().startsWith("o") ||
word.toLowerCase().startsWith("u")){
list.add(word);
}
}
return list;
}
public static void main(String[] args) throws
IOException {
VowelCount cw=new
VowelCount();
String mlk = "Our lives begin to
end the day we become silent about things that matter";
ArrayList<String>word=getVowelList(mlk);
Set<String >setarr=new
HashSet<String>();
setarr.addAll(word);
word.clear();
word.addAll(setarr);
System.out.println(word.toString());
String theFlea="The flea is a
mighty an insect , flea is the most beautiful insect you can ever
see the best one";
System.out.println(countArtical(theFlea));
File input=new File(INPUT);
File output=new File(OUTPUT);
PluralCount(input, output);
}
}