Earley Parser Implementation: Java - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Earley Parser Implementation: Java

Description:

BufferedReader fin = EarleyFileRead.openFile(opts.getProperty('input' ... on position right-ward (so we use the tail of // the RHS array as the new RHS) ... – PowerPoint PPT presentation

Number of Views:163
Avg rating:3.0/5.0
Slides: 29
Provided by: emilyj2
Category:

less

Transcript and Presenter's Notes

Title: Earley Parser Implementation: Java


1
Earley Parser Implementation Java
  • Jeremy Morris Emily Jamison

2
MAIN public static void main(String args)
CmdOptions opts new CmdOptions(args) opts
.list(System.out) if (!opts.exists("agendaType")
) opts.setProperty("agendaType","stack")
try LexiconBase lex EarleyFileRead.readLex
icon(opts.getProperty("lexicon")) GrammarBase
gram EarleyFileRead.readGrammar(opts.getProperty
("grammar")) gram.dump(System.out) System.ou
t.println() lex.dump(System.out) System.out.
println() EarleyParser ep new
EarleyParser(gram,lex,opts.getProperty("agendaType
")) BufferedReader fin EarleyFileRead.openFil
e(opts.getProperty("input")) String lIn
fin.readLine().toLowerCase() while (lIn !
null) System.out.println("Recognize
"lIn"") boolean result
ep.recognize(lIn,"s") if (result) System.o
ut.println("Input "lIn" ....
RECOGNIZED!") else System.out.println(
"Input "lIn" .... NOT RECOGNIZED!") Syste
m.out.println() lIn fin.readLine()
catch (Exception e) System.err.println("Pro
blem with file i/o") e.printStackTrace()
Agenda Creation, Recognize / The core routine
of the object. This creates a new agenda,
places the various parts of speech for the input
string onto the agenda, then runs the agenda
processing. Agenda processing is very simple -
keep removing agenda items and entering them into
the chart with the enter_edge method until there
are no more agenda items left. Once the agenda
is empty, it checks to see if the end state is in
the chart at the expected location. If it is, it
returns true. Otherwise, it returns false.
/ public boolean recognize(String instring,
String startSym) boolean resultfalse Str
ing tokensinstring.split("\\s") if
(agendaType.toLowerCase().equals("stack")) agen
da new StackAgenda() else agenda new
QueueAgenda() String ss new
String1 ss0startSym chartnew
EarleyChart(tokens.length) for (int i0
ilttokens.length i) Vector lexItems
lexicon.lookup(tokensi) if (lexItems !
null) for (int cnt0 cntltlexItems.size()
cnt) EarleyChartState wordState new
EarleyChartState((String) lexItems.get(cnt),null)
agenda.add(wordState,i,i1)
EarleyChartState startState new
EarleyChartState("S",ss) agenda.add(startState,
0,0) while (!agenda.empty()) EarleyEdge
nextEdge agenda.next() enter_edge(nextEdge)
EarleyChartState endState new
EarleyChartState("S",null) resultchart.in(endS
tate,0,tokens.length) return result
1. 2.
4.
5. 6.
7.
8. 9. 10.
3.
3
Enter_edge Predict, Complete, Scan. (from
EarleyParser pg 2) public void
enter_edge(EarleyEdge e) EarleyChartState s
e.s int i e.i int j e.j chart.add(s,i,j)
// Predict // All edges returned by the predict
method are placed // into the
agenda. EarleyChartState preds predict(s) if
(preds ! null) System.out.println("PRED
"i"---"s"----"j) for (int cnt0
cntltpreds.length cnt) agenda.add(predscnt,j
,j) // Complete // The "completeSet" method
of the chart returns // only edges that fit the
completion criteria, // namely it returns all
edges that end in position 'i' // where the
current state (s) is the returned edge (s2) //
left sister (i.e. the LHS of (s) is the first
element // of the RHS of (s2). EarleyEdge
complete chart.completeSet(s,i) for (int
cnt0 cntltcomplete.length cnt) EarleyEdge
ne completecnt EarleyChartState s2
ne.s String newLHS s2.lhs String
newRHS s2.rhsTail() int k
ne.i EarleyChartState ns new
EarleyChartState(newLHS,newRHS) System.out.print
ln("COMPLETE "ne.i"---"s2"----"ne.j)
System.out.println("
"i"---"s"----"j) agenda.add(ns,k,j)
11.
// Scan // Again, the "scanSet" method of the
chart returns // only edges that fit the
scanning criteria EarleyEdge scan
chart.scanSet(s,j) for (int cnt0
cntltscan.length cnt) // Need to add our new
edge to the agenda // Transform the Edge into
a new edge // IOW - the LHS of the new state
is the same as // the current edge's LHS. The
RHS is the // same as the current RHS, but
with the dot advanced // on position
right-ward (so we use the tail of // the RHS
array as the new RHS). The new edge moves //
from the beginning of the current edge to the //
end of the edge in the scanSet. EarleyEdge ne
scancnt EarleyChartState s2 ne.s String
newLHS s.lhs String newRHS
s.rhsTail() int k ne.j EarleyChartState
ns new EarleyChartState(newLHS,newRHS) System.
out.println("SCAN "i"---"s"----"
j) System.out.println("
"ne.i"---"s2"----"ne.j) agenda.add(ns,i,k)

4
Enter_edge Predict, Complete, Scan. (from
EarleyParser pg 2) public void
enter_edge(EarleyEdge e) EarleyChartState s
e.s int i e.i int j e.j chart.add(s,i,j)
// Predict // All edges returned by the predict
method are placed // into the
agenda. EarleyChartState preds predict(s) if
(preds ! null) System.out.println("PRED
"i"---"s"----"j) for (int cnt0
cntltpreds.length cnt) agenda.add(predscnt,j
,j) // Complete // The "completeSet" method
of the chart returns // only edges that fit the
completion criteria, // namely it returns all
edges that end in position 'i' // where the
current state (s) is the returned edge (s2) //
left sister (i.e. the LHS of (s) is the first
element // of the RHS of (s2). EarleyEdge
complete chart.completeSet(s,i) for (int
cnt0 cntltcomplete.length cnt) EarleyEdge
ne completecnt EarleyChartState s2
ne.s String newLHS s2.lhs String
newRHS s2.rhsTail() int k
ne.i EarleyChartState ns new
EarleyChartState(newLHS,newRHS) System.out.print
ln("COMPLETE "ne.i"---"s2"----"ne.j)
System.out.println("
"i"---"s"----"j) agenda.add(ns,k,j)
// Scan // Again, the "scanSet" method of the
chart returns // only edges that fit the
scanning criteria EarleyEdge scan
chart.scanSet(s,j) for (int cnt0
cntltscan.length cnt) // Need to add our new
edge to the agenda // Transform the Edge into
a new edge // IOW - the LHS of the new state
is the same as // the current edge's LHS. The
RHS is the // same as the current RHS, but
with the dot advanced // on position
right-ward (so we use the tail of // the RHS
array as the new RHS). The new edge moves //
from the beginning of the current edge to the //
end of the edge in the scanSet. EarleyEdge ne
scancnt EarleyChartState s2 ne.s String
newLHS s.lhs String newRHS
s.rhsTail() int k ne.j EarleyChartState
ns new EarleyChartState(newLHS,newRHS) System.
out.println("SCAN "i"---"s"----"
j) System.out.println("
"ne.i"---"s2"----"ne.j) agenda.add(ns,i,k)

12.
5
Predict (from EarleyParser, pg 3) / A simple
implementation of the predictions method given
in the course notes. Makes use of the lookup
method of the grammarBase object to quickly
compile all possible predictions, then
converts them from Vector to array form for
easy processing. / public EarleyChartState
predict(EarleyChartState s) if (s.rhsnull)
return null String search s.rhs0 Vector
nVec grammar.lookup(search) if (nVec null)
return null EarleyChartState cs new
EarleyChartStatenVec.size() for (int i0
iltcs.length i) // Convert the grammar
rules returned by the grammar // database to
chart states GrammarRule gr (GrammarRule)
nVec.get(i) EarleyChartState ns new
EarleyChartState(gr.lhs,gr.rhs) csins r
eturn cs
13.
6
Enter_edge Predict, Complete, Scan. (from
EarleyParser pg 2) public void
enter_edge(EarleyEdge e) EarleyChartState s
e.s int i e.i int j e.j chart.add(s,i,j)
// Predict // All edges returned by the predict
method are placed // into the
agenda. EarleyChartState preds predict(s) if
(preds ! null) System.out.println("PRED
"i"---"s"----"j) for (int cnt0
cntltpreds.length cnt) agenda.add(predscnt,j
,j) // Complete // The "completeSet" method
of the chart returns // only edges that fit the
completion criteria, // namely it returns all
edges that end in position 'i' // where the
current state (s) is the returned edge (s2) //
left sister (i.e. the LHS of (s) is the first
element // of the RHS of (s2). EarleyEdge
complete chart.completeSet(s,i) for (int
cnt0 cntltcomplete.length cnt) EarleyEdge
ne completecnt EarleyChartState s2
ne.s String newLHS s2.lhs String
newRHS s2.rhsTail() int k
ne.i EarleyChartState ns new
EarleyChartState(newLHS,newRHS) System.out.print
ln("COMPLETE "ne.i"---"s2"----"ne.j)
System.out.println("
"i"---"s"----"j) agenda.add(ns,k,j)
// Scan // Again, the "scanSet" method of the
chart returns // only edges that fit the
scanning criteria EarleyEdge scan
chart.scanSet(s,j) for (int cnt0
cntltscan.length cnt) // Need to add our new
edge to the agenda // Transform the Edge into
a new edge // IOW - the LHS of the new state
is the same as // the current edge's LHS. The
RHS is the // same as the current RHS, but
with the dot advanced // on position
right-ward (so we use the tail of // the RHS
array as the new RHS). The new edge moves //
from the beginning of the current edge to the //
end of the edge in the scanSet. EarleyEdge ne
scancnt EarleyChartState s2 ne.s String
newLHS s.lhs String newRHS
s.rhsTail() int k ne.j EarleyChartState
ns new EarleyChartState(newLHS,newRHS) System.
out.println("SCAN "i"---"s"----"
j) System.out.println("
"ne.i"---"s2"----"ne.j) agenda.add(ns,i,k)

14. 15.
7
Earley Chart, Column 2/3 public boolean
in(EarleyChartState s, int i, int j) /
Returns true if the entry is in the chart,
false otherwise / for (int k0
kltchartij.size() k) if
(get(i,j,k).equals(s)) return true return
false public void add(EarleyChartState s, int
i, int j) / Check to see if the state is
already in the chart if not, add it /
System.out.println("CHART Enter
"i"---"s"----"j) if (!in(s,i,j))
chartij.add(s) public void add(EarleyEdge
e) add(e.s, e.i, e.j) public
EarleyChartState get(int i, int j, int idx) /
Easy get implementation to avoid a lot of
inline variable casting / return
(EarleyChartState) chartij.get(idx) public
EarleyEdge completeSet(EarleyChartState s, int
i) / Implements the query for items in the
chart that will complete state s, ending at
position i. It does this by searching all
items with end position i and using the
leftSister method of the chart item to find the
ones that can be used to complete the input
state s. / EarleyEdge retV Vector tmp
new Vector() for (int k0 kltcsize k)
for (int t0 tltchartki.size() t)
EarleyChartState s2 get(k,i,t) if
(s2.leftSister(s)) tmp.add(new EarleyEdge(s2,k,i))

Earley Chart Column 3/3 retV new
EarleyEdgetmp.size() for (int cnt0
cntlttmp.size() cnt) retVcnt(EarleyEdge)
tmp.get(cnt) return retV public
EarleyEdge scanSet(EarleyChartState s, int j)
/ This works just like "completeSet"
above, but for scanning. It uses the
rightSister method in the same way that the
above uses the leftSister method.
/ EarleyEdge retV Vector tmp new
Vector() for (int k0 kltcsize1 k)
for (int t0 tltchartjk.size() t)
EarleyChartState s2 get(j,k,t) if
(s2.rightSister(s)) tmp.add(new
EarleyEdge(s2,j,k)) retV new
EarleyEdgetmp.size() for (int cnt0
cntlttmp.size() cnt) retVcnt(EarleyEdge)
tmp.get(cnt) return retV public void
dump(PrintStream sout) / Simple chart
dump for debugging purposes / sout.println("Du
mping Chart...") for (int i0 iltcsize1 i)
sout.print(i) for (int j0 jltcsize1
j) sout.print(", "j" gt ") for (int
k0 kltchartij.size() k)
sout.print(" "get(i,j,k)",
") sout.println()
(14.)
8
Enter_edge Predict, Complete, Scan. (from
EarleyParser pg 2) public void
enter_edge(EarleyEdge e) EarleyChartState s
e.s int i e.i int j e.j chart.add(s,i,j)
// Predict // All edges returned by the predict
method are placed // into the
agenda. EarleyChartState preds predict(s) if
(preds ! null) System.out.println("PRED
"i"---"s"----"j) for (int cnt0
cntltpreds.length cnt) agenda.add(predscnt,j
,j) // Complete // The "completeSet" method
of the chart returns // only edges that fit the
completion criteria, // namely it returns all
edges that end in position 'i' // where the
current state (s) is the returned edge (s2) //
left sister (i.e. the LHS of (s) is the first
element // of the RHS of (s2). EarleyEdge
complete chart.completeSet(s,i) for (int
cnt0 cntltcomplete.length cnt) EarleyEdge
ne completecnt EarleyChartState s2
ne.s String newLHS s2.lhs String
newRHS s2.rhsTail() int k
ne.i EarleyChartState ns new
EarleyChartState(newLHS,newRHS) System.out.print
ln("COMPLETE "ne.i"---"s2"----"ne.j)
System.out.println("
"i"---"s"----"j) agenda.add(ns,k,j)
// Scan // Again, the "scanSet" method of the
chart returns // only edges that fit the
scanning criteria EarleyEdge scan
chart.scanSet(s,j) for (int cnt0
cntltscan.length cnt) // Need to add our new
edge to the agenda // Transform the Edge into
a new edge // IOW - the LHS of the new state
is the same as // the current edge's LHS. The
RHS is the // same as the current RHS, but
with the dot advanced // on position
right-ward (so we use the tail of // the RHS
array as the new RHS). The new edge moves //
from the beginning of the current edge to the //
end of the edge in the scanSet. EarleyEdge ne
scancnt EarleyChartState s2 ne.s String
newLHS s.lhs String newRHS
s.rhsTail() int k ne.j EarleyChartState
ns new EarleyChartState(newLHS,newRHS) System.
out.println("SCAN "i"---"s"----"
j) System.out.println("
"ne.i"---"s2"----"ne.j) agenda.add(ns,i,k)

16. 17.
9
Earley Chart, Column 2/3 public boolean
in(EarleyChartState s, int i, int j) /
Returns true if the entry is in the chart,
false otherwise / for (int k0
kltchartij.size() k) if
(get(i,j,k).equals(s)) return true return
false public void add(EarleyChartState s, int
i, int j) / Check to see if the
state is already in the chart if not, add
it / System.out.println("CHART Enter
"i"---"s"----"j) if (!in(s,i,j))
chartij.add(s) public void add(EarleyEdge
e) add(e.s, e.i, e.j) public
EarleyChartState get(int i, int j, int idx) /
Easy get implementation to avoid a lot of
inline variable casting / return
(EarleyChartState) chartij.get(idx) public
EarleyEdge completeSet(EarleyChartState s, int
i) / Implements the query for items in the
chart that will complete state s, ending at
position i. It does this by searching all
items with end position i and using the
leftSister method of the chart item to find the
ones that can be used to complete the input
state s. / EarleyEdge retV Vector tmp
new Vector() for (int k0 kltcsize k)
for (int t0 tltchartki.size() t)
EarleyChartState s2 get(k,i,t) if
(s2.leftSister(s)) tmp.add(new EarleyEdge(s2,k,i))

Earley Chart Column 3/3 retV new
EarleyEdgetmp.size() for (int cnt0
cntlttmp.size() cnt) retVcnt(EarleyEdge)
tmp.get(cnt) return retV public
EarleyEdge scanSet(EarleyChartState s, int j)
/ This works just like "completeSet"
above, but for scanning. It uses the
rightSister method in the same way that the
above uses the leftSister method.
/ EarleyEdge retV Vector tmp new
Vector() for (int k0 kltcsize1 k)
for (int t0 tltchartjk.size() t)
EarleyChartState s2 get(j,k,t) if
(s2.rightSister(s)) tmp.add(new
EarleyEdge(s2,j,k)) retV new
EarleyEdgetmp.size() for (int cnt0
cntlttmp.size() cnt) retVcnt(EarleyEdge)
tmp.get(cnt) return retV public void
dump(PrintStream sout) / Simple chart
dump for debugging purposes / sout.println("Du
mping Chart...") for (int i0 iltcsize1 i)
sout.print(i) for (int j0 jltcsize1
j) sout.print(", "j" gt ") for (int
k0 kltchartij.size() k)
sout.print(" "get(i,j,k)",
") sout.println()
(16.)
10
End Slideshow.
11
Slide Appendix
12
Output (slide 1/2) n ---gt adj n vp ---gt vt np vp
---gt vi vp ---gt vi pp s ---gt np vp np ---gt det
n np ---gt pron np ---gt pn pp ---gt prep
np Dumping Lexicon... vi ---gt ran n ---gt cave pn
---gt mary n ---gt man pron ---gt i vt ---gt saw n
---gt saw adj ---gt young vt ---gt fought prep ---gt
with prep ---gt in adj ---gt rusty n ---gt
dragon det ---gt the Recognize mary
ran AGENDA Add 0---state(pn,
)----1 AGENDA Add 1---state(vi,
)----2 AGENDA Add 0---state(S, s
)----0 AGENDA Remove 0---state(S, s
)----0 CHART Enter 0---state(S, s
)----0 PRED 0---state(S, s
)----0 AGENDA Add 0---state(s, np vp
)----0 AGENDA Remove 0---state(s, np vp
)----0 CHART Enter 0---state(s, np vp
)----0
PRED 0---state(s, np vp
)----0 AGENDA Add 0---state(np, det n
)----0 AGENDA Add 0---state(np, pron
)----0 AGENDA Add 0---state(np, pn
)----0 AGENDA Remove 0---state(np, pn
)----0 CHART Enter 0---state(np, pn
)----0 AGENDA Remove 0---state(np, pron
)----0 CHART Enter 0---state(np, pron
)----0 AGENDA Remove 0---state(np, det n
)----0 CHART Enter 0---state(np, det n
)----0 AGENDA Remove 1---state(vi,
)----2 CHART Enter 1---state(vi,
)----2 AGENDA Remove 0---state(pn,
)----1 CHART Enter 0---state(pn,
)----1 COMPLETE 0---state(np, pn
)----0 0---state(pn,
)----1 AGENDA Add 0---state(np,
)----1 AGENDA Remove 0---state(np,
)----1 CHART Enter 0---state(np,
)----1 COMPLETE 0---state(s, np vp
)----0 0---state(np,
)----1 AGENDA Add 0---state(s, vp
)----1 AGENDA Remove 0---state(s, vp
)----1 CHART Enter 0---state(s, vp
)----1 PRED 0---state(s, vp
)----1 AGENDA Add 1---state(vp, vt np
)----1 AGENDA Add 1---state(vp, vi
)----1 AGENDA Add 1---state(vp, vi pp
)----1 AGENDA Remove 1---state(vp, vi pp
)----1 CHART Enter 1---state(vp, vi pp
)----1 SCAN 1---state(vp, vi pp
)----1 1---state(vi,
)----2 AGENDA Add 1---state(vp, pp
)----2 AGENDA Remove 1---state(vp, pp
)----2 CHART Enter 1---state(vp, pp
)----2 PRED 1---state(vp, pp
)----2 AGENDA Add 2---state(pp, prep np
)----2
13
Output (slide 2/2) AGENDA Remove
2---state(pp, prep np )----2 CHART Enter
2---state(pp, prep np )----2 AGENDA Remove
1---state(vp, vi )----1 CHART Enter
1---state(vp, vi )----1 SCAN
1---state(vp, vi )----1
1---state(vi, )----2 AGENDA Add
1---state(vp, )----2 AGENDA Remove
1---state(vp, )----2 CHART Enter
1---state(vp, )----2 COMPLETE
0---state(s, vp )----1
1---state(vp, )----2 AGENDA Add
0---state(s, )----2 AGENDA Remove
0---state(s, )----2 CHART Enter
0---state(s, )----2 COMPLETE
0---state(S, s )----0
0---state(s, )----2 AGENDA Add
0---state(S, )----2 AGENDA Remove
0---state(S, )----2 CHART Enter
0---state(S, )----2 AGENDA Remove
1---state(vp, vt np )----1 CHART Enter
1---state(vp, vt np )----1 Input mary ran
.... RECOGNIZED!
14
lex.txt PN,mary VI,ran VT,saw PRON,i N,saw DET,th
e ADJ,young ADJ,rusty N,man N,dragon VT,fought PRE
P,with PREP,in N,cave
15
QueueAgenda (slide 1/1) import
java.util.Vector public class QueueAgenda
extends Vector implements Agenda /
Implements an Agenda as a simple Queue for
breadth-first searching. / int
current public QueueAgenda()
current0 public void
add(EarleyChartState s, int i, int j)
super.add(new EarleyEdge(s,i,j)) System.out
.println("AGENDA Add "i"---"s"----"j)
public EarleyEdge next() EarleyEdge p
(EarleyEdge) get(current) current System.o
ut.println("AGENDA Remove "p.i"---"p.s"---
-"p.j) return p public boolean empty()
return currentsize()
16
StackAgenda (slide 1/1) import
java.util. public class StackAgenda extends
Stack implements Agenda / Implements an
agenda as a stack, for depth-first searching
of the agenda. / public void
add(EarleyChartState s, int i, int j)
push(new EarleyEdge(s,i,j)) System.out.prin
tln("AGENDA Add "i"---"s"----"j)
public EarleyEdge next() EarleyEdge p
(EarleyEdge) pop() System.out.println("AGENDA
Remove "p.i"---"p.s"----"p.j) return
p
17
Earley Chart Column 1/3 package
jjm_ling684 import java.util. import
java.io. public class EarleyChart Vector
chart int csize public EarleyChart(int
length) / Chart is a length1 x
length1 chart of Vectors. Vectors are used
because they grow dynamically, and we may
not know how many chart entries it will
need to hold Technically, this doesn't need
to be a "full chart", but for readability,
we'll assume a full chart. / csizelengt
h chart new Vectorlength1length1 for
(int i0 iltlength1 i) for (int j0
jltlength1 j) chartijnew
Vector()
18
Earley Chart, Column 2/3 public boolean
in(EarleyChartState s, int i, int j) /
Returns true if the entry is in the chart,
false otherwise / for (int k0
kltchartij.size() k) if
(get(i,j,k).equals(s)) return true return
false public void add(EarleyChartState s, int
i, int j) / Check to see if the
state is already in the chart if not, add
it / System.out.println("CHART Enter
"i"---"s"----"j) if (!in(s,i,j))
chartij.add(s) public void add(EarleyEdge
e) add(e.s, e.i, e.j) public
EarleyChartState get(int i, int j, int idx) /
Easy get implementation to avoid a lot of
inline variable casting / return
(EarleyChartState) chartij.get(idx) public
EarleyEdge completeSet(EarleyChartState s, int
i) / Implements the query for items in the
chart that will complete state s, ending at
position i. It does this by searching all
items with end position i and using the
leftSister method of the chart item to find the
ones that can be used to complete the input
state s. / EarleyEdge retV Vector tmp
new Vector() for (int k0 kltcsize k)
for (int t0 tltchartki.size() t)
EarleyChartState s2 get(k,i,t) if
(s2.leftSister(s)) tmp.add(new EarleyEdge(s2,k,i))

Earley Chart Column 3/3 retV new
EarleyEdgetmp.size() for (int cnt0
cntlttmp.size() cnt) retVcnt(EarleyEdge)
tmp.get(cnt) return retV public
EarleyEdge scanSet(EarleyChartState s, int j)
/ This works just like "completeSet"
above, but for scanning. It uses the
rightSister method in the same way that the
above uses the leftSister method.
/ EarleyEdge retV Vector tmp new
Vector() for (int k0 kltcsize1 k)
for (int t0 tltchartjk.size() t)
EarleyChartState s2 get(j,k,t) if
(s2.rightSister(s)) tmp.add(new
EarleyEdge(s2,j,k)) retV new
EarleyEdgetmp.size() for (int cnt0
cntlttmp.size() cnt) retVcnt(EarleyEdge)
tmp.get(cnt) return retV public void
dump(PrintStream sout) / Simple chart
dump for debugging purposes / sout.println("Du
mping Chart...") for (int i0 iltcsize1 i)
sout.print(i) for (int j0 jltcsize1
j) sout.print(", "j" gt ") for (int
k0 kltchartij.size() k)
sout.print(" "get(i,j,k)",
") sout.println()
19
Earley Chart State Column 1/2 public class
EarleyChartState / Simple class for
holding information about a chart state. lhs
- holds the left-hand-side of the grammar rule
stored in the state rhs - holds the
right-hand-side of the grammar rule AFTER
the dot stored in the state complete - is
true if the state is complete, false otherwise
Note that the "dot" is not explicitly kept
in the structure. Instead, the dot is
presumed to be before the first element of
the rhs array. If the rhs array is null, the
state is complete. / String
lhs String rhs boolean complete public
EarleyChartState(String l, String r)
lhsl rhsr if (r null)
completetrue else completefalse public
boolean equals(EarleyChartState e) if
(!lhs.equals(e.lhs)) return false if (rhs
null e.rhs null) return true else if
(rhsnull e.rhs null) return false if
(rhs.length ! e.rhs.length) return false for
(int i0 iltrhs.length i) if
(!rhsi.equals(e.rhsi)) return
false return true public boolean
leftSister(EarleyChartState e) boolean
retfalse // System.out.println("Checking left
sister of "toString()) // System.out.println("
Against "e.toString()) if (e.complete rhs
! null rhs0.equals(e.lhs))
rettrue return ret
Earley Chart State Column 2/2 public boolean
rightSister(EarleyChartState e) boolean
retfalse // System.out.println("Checking right
sister of "toString()) // System.out.println("
Against "e.toString()) if (complete e.rhs
! null lhs.equals(e.rhs0))
rettrue return ret public String
rhsTail() if (rhs null (rhs.length lt1))
return null String ret new
Stringrhs.length-1 for (int i1
iltrhs.lengthi) reti-1rhsi return
ret public String rhsHead() if (rhs
null) return null return rhs0 public
String toString() String r " " if
(rhs!null) r " " for (int i0
iltrhs.length i) r rrhsi" " rr"
" else r" " return "state("lhs","r")
"
20
Agenda public interface Agenda / This is
the interface for all types of agendas.
Regardless of what kind of agenda is used,
these three things must be implemented.
add - enters a new state into the agenda,
along with its start and end
positions next - removes the next node from
the agenda and returns it to the
caller empty - returns true if there are no
more items on the agenda, false
otherwise / public void add(EarleyChartState
s, int i, int j) public EarleyEdge
next() public boolean empty()
21
CmdOptions import java.util. public class
CmdOptions extends Properties / This is
just a helper class for parsing the command-line
arguments and keeping them in a reasonable
form. Takes an array of strings at its
construction which can be accessed through
the "getProperty" method of the Properties class.
command-line arguments are in the
"keyvalue" format (or just "key" if boolean
args are needed). / public
CmdOptions(String argv) super() setProper
ties(argv) public void setProperties(String
argv) for (int i0 iltargv.length i)
String args argvi.split("") if
(args.lengthlt2) setProperty(args0,"true")
else setProperty(args0,args1)
public boolean exists(String ins)
if (getProperty(ins) ! null) return
true else return false
22
EarleyEdge public class EarleyEdge /
Simple class for holding information about an
edge. / EarleyChartState s int
i,j public EarleyEdge(EarleyChartState ns,
int ni, int nj) sns ini jnj pu
blic String toString() return s", "i",
"j
23
EarleyFileRead (slide 1/2) import
java.io. import java.util. public class
EarleyFileRead / Some simple file i/o
classes for making taking initial values for
our lexicon and grammar objects. / public
static LexiconBase readLexicon(String lexFile)
throws Exception // Lexicon is of the form
"CAT, WORD" for CAT ---gt WORD, each on its //
own line LexiconBase lexicon new
LexiconBase() BufferedReader hashIn
openFile(lexFile) String lInhashIn.readLine()
while (lIn ! null) String lStr
lIn.toLowerCase().split(",") if
(lStr.length!2) throw new
Exception("Invalid Lexicon File Format") else
lexicon.add(lStr0.trim(),lStr1.trim())
lInhashIn.readLine() hashIn.close()
return lexicon
24
EarleyFileRead(slide 2/2) public static
GrammarBase readGrammar(String grmFile) throws
Exception // Grammar is of the form
"CAT,SCAT1,SCAT2,..." for CAT ---gt SCAT1 SCAT2
..., // each on its own line // Since each
CAT can go to multiple rules, we'll store
possible rules as a vector, // indexed in the
hashmap. A grammar Rule class has been created
to store the logical // form of the grammar
rule for easy use, but the HashMap will maintain
them for quick // lookup. GrammarBase grammar
new GrammarBase() BufferedReader hashIn
openFile(grmFile) String lInhashIn.readLine()
while (lIn ! null) String lStr
lIn.toLowerCase().split(",") if
(lStr.lengthlt2) throw new Exception("Invalid
Grammar File Format") else GrammarRule
gr new GrammarRule(lStr) if
(grammar.get(gr.lhs()) ! null) ((Vector)
grammar.get(gr.lhs)).add(gr) else
Vector nVec new Vector() nVec.add(g
r) grammar.put(gr.lhs(),nVec)
lInhashIn.readLine() hashIn.close()
return grammar public static
BufferedReader openFile(String fname) throws
Exception BufferedReader finnull //System.
out.println(fname) FileInputStream in1 new
FileInputStream(new File(fname)) fin new
BufferedReader(new InputStreamReader(in1)) ret
urn fin
25
GrammarBase (slide 1/2) import
java.util. import java.io. public class
GrammarBase extends HashMap / Simple
grammar database object. Takes a
part-of-speech tag as a key and returns a
Vector of all of the possible expansions of
this part-of-speech in the grammar. (Must be
a Vector because multiple expansions can fit
one part of speech). / // HashMap
lcIndex public GrammarBase()
super() // lcIndexnew HashMap() publ
ic void addRule(GrammarRule gr) if
(get(gr.lhs()) ! null) ((Vector)
get(gr.lhs)).add(gr) else Vector nVec
new Vector() nVec.add(gr) put(gr.lhs(),nVe
c) / if (gr.rhs ! null) if
(lcIndex.get(gr.rhs0) ! null) ((Vector)
lcIndex.get(gr.rhs0)).add(gr) else
Vector nVec new Vector() nVec.add(gr)
lcIndex.put(gr.rhs0,nVec) /
26
GrammarBase (slide 2/2) public Vector
lookup(String lhs) if (get(lhs) ! null)
return (Vector) get(lhs) else return
null / public Vector leftCornerLookup(Strin
g lcRHS) if (lcIndex.get(lcRHS) !
null) return (Vector) get(lcRHS) else ret
urn null / public void dump(PrintStream
outs) outs.println("Dumping
Grammar...") Iterator iter
keySet().iterator() while (iter.hasNext())
String key (String) iter.next() Vector
tmpV (Vector) get(key) for (int i0
ilttmpV.size() i) outs.println(tmpV.get(i).t
oString())
27
public class GrammarRule / Simple class for
holding grammar rules. lhs - left-hand-side
of the grammar rule rhs - right-hand-side of
the grammar rule / protected String
lhs String rhs public GrammarRule(String
l, String r) // Rule of the form l --gt r1,
r2, ... lhs l rhs new
Stringr.length for (int i0 iltrhs.length
i) rhsi ri.trim() public
GrammarRule(String lStr) // Rule of the
form l,r1,r2,r3,... rhs new
StringlStr.length-1 lhs lStr0 for
(int i1 iltlStr.length i) rhsi-1lStri
.trim() public String expand(String l)
if (lhs.equals(l)) return
rhs else return null public String
rhs() return rhs public String lhs()
return lhs public String toString()
String ret lhs" ---gt" if (rhs null)
ret ret " " else for (int i0
iltrhs.length i) ret ret "
"rhsi return ret
GrammarRule (slide 1/1)
28
LexiconBase (slide 1/1) import
java.util. import java.io. public class
LexiconBase extends HashMap / Simple lexicon
map for lookup purposes. Takes a word items
as a key and returns a Vector containing all
possible parts of speech for that word (since
words may have multiple parts of speech).
/ public LexiconBase() super() public
void add(String lex, String word) if
(get(word) ! null) ((Vector)
get(word)).add(lex) else Vector nVec
new Vector() nVec.add(lex) put(word,nVec)
public Vector lookup(String word) if
(get(word) ! null) return (Vector)
get(word) else return null public void
dump(PrintStream outs) outs.println("Dumping
Lexicon...") Iterator iter
keySet().iterator() while (iter.hasNext())
String key (String) iter.next() Vector
tmpV (Vector) get(key) for (int i0
ilttmpV.size() i) outs.println((String)
tmpV.get(i)" ---gt "key)
Write a Comment
User Comments (0)
About PowerShow.com