Introduction to Programming With Java
Download
Report
Transcript Introduction to Programming With Java
“Introduction to Programming
With Java”
Lecture - 11
UMESH PATIL ([email protected])
[email protected]
Contents for Today’s Lecture
• Programming practice: Program for morphological
analysis of English nouns.
[email protected]
Problem
Write a Java program that finds out the root & suffix of the given noun
plural in English. Also print the morphology rule applied for forming
the plural.
Sample input & output –
Input: boys
Output:
root = boy
suffix = s
Morphology rule = [[W]] [[W][s]]
Input: indices
Output:
root = index
suffix = ices
Morphology rule = [[W][ex]] [[W][ices]]
[email protected]
Plurals in English
Example
Morphology Rule
•
boy boys
•
W Ws
•
bus buses
•
W Wes
•
lady ladies
•
Wy Wies
•
leaf leaves
•
Wf Wves
•
datum data
•
Wum Wa
•
formula formulae
•
W We
•
index indices
•
Wex Wices
•
matrix matrices
•
Wx Wces
•
radius radii
•
Wus Wi
•
cherub cherubim
•
W Wim
[email protected]
Algorithm
1. Read the input word
2. Find the suffix
3. Remove suffix & get the root
4. If needed, prepare the correct root
eg. indices ind ind + ex = index
5. Print the output
[email protected]
Required Methods of String class
1. boolean endsWith(String suffix)
Returns true if the string ends with the specified suffix;
false otherwise.
Parameters: the String suffix.
eg. If s = “indices” then
s.endsWith(“ices”) will return true
s.endsWith(“s”) will return true
s.endsWith(“”) will return true
s.endsWith(“indices”) will return true
But
s.endsWith(“ice”) will return false
[email protected]
Required Methods of String class… continued
2.
String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of the current string. The
substring begins at the specified beginIndex and extends to the
character at index endIndex - 1. It will give error (throw exception) if
the beginIndex is negative, or endIndex is larger than the length the
string, or beginIndex is larger than endIndex.
Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
eg. If s = “mississippi” then
s.substring(0,4) will return “miss”
s.substring(6,9) will return “sip”
s.substring(0,11) will return “mississippi”
s.substring(0,0) will return “”
[email protected]
Required Methods of String class… continued
3. int lastIndexOf(String str)
Returns the index within the current string of the
rightmost occurrence of the specified substring. If str does
not occur as a substring, -1 is returned.
Parameters: str - the substring to search for.
eg. If s = “mississippi” then
s.lastIndexOf(“is”) will return 4
s.lastIndexOf(“i”) will return 10
s.lastIndexOf(“”) will return 11
s.lastIndexOf(“mississippi”) will return 0
And
s.lastIndexOf(“abc”) will return -1
[email protected]
Required Methods of String class… continued
4. String concat(String str)
Returns a string after concatenating the specified string, ie.
str, to the end of the current string.
Parameters:
str - the string that is concatenated to the end of the current
string.
eg. if s = “man” then
s.concat(“go”) will return “mango”
s.concat(“”) will return “man”
Detailed explanation of the String class.
[email protected]
Core Part of the Program
Suppose input word = “indices”;
if (word.endsWith("ices")) {
// word = “indices”
suffix = "ices";
i = word.lastIndexOf("ices"); // i = 3
root = word.substring(0,i);
// root = “ind”
root = root.concat("ex");
// root = “index”
morphRule = "[[W][ex]] [[W][ices]]";
}
[email protected]
Some Testing
1. matrix matrices
2. life lives
3. analysis analyses
4. criterion criteria
Does the program work for these examples?
If yes, how?
If no, why?
[email protected]
Assignment
1. Do the morphological analysis of the following inflected verbs
in English and write a Java program that finds out the root &
suffix of the given inflected verb. Also print the morphology
rule that is applied.
fetch fetches, fetching, fetched
carry carries, carrying, carried
enjoy enjoys, enjoying, enjoyed
burn burns, burning, burnt
beat beats, beating, beaten
forbid forbids, forbidding, forbided, forbidden
blow blows, blowing, blowed, blown
save saves, saving, saved
[email protected]
Assignment … continued
say says, saying, said
drop drops, dropping, dropped
admit admits, admitting, admitted
repel repels, repelling, repelled
dog dogs, dogging, dogged
can cans, canning, canned
spur spurs, spurring, spurred
stem stems, stemming, stemmed
stab stabs, stabbing, stabbed
kid kids, kidding, kidded
mimic mimics, mimicking, mimicked
[email protected]
To Remind
The best way to learn programming
is writing a lot of programs on your
own.
[email protected]
End
Thank you
[email protected]