[Corpusreader-svn] SF.net SVN: corpusreader:[210] trunk/corpusreader/src
Status: Alpha
Brought to you by:
sylvainloiseau
|
From: <syl...@us...> - 2008-12-21 11:13:53
|
Revision: 210
http://corpusreader.svn.sourceforge.net/corpusreader/?rev=210&view=rev
Author: sylvainloiseau
Date: 2008-12-21 11:13:51 +0000 (Sun, 21 Dec 2008)
Log Message:
-----------
bug
Modified Paths:
--------------
trunk/corpusreader/src/main/java/tei/cr/Version.java
trunk/corpusreader/src/main/java/tei/cr/filters/MarkBalanced.java
trunk/corpusreader/src/main/java/tei/cr/saxConverterImpl/Codegram.java
trunk/corpusreader/src/main/java/tei/cr/utils/syntex/SyntexBuffer.java
trunk/corpusreader/src/test/java/tei/cr/utils/syntex/TestSyntexBuffer.java
Modified: trunk/corpusreader/src/main/java/tei/cr/Version.java
===================================================================
--- trunk/corpusreader/src/main/java/tei/cr/Version.java 2008-12-20 21:23:44 UTC (rev 209)
+++ trunk/corpusreader/src/main/java/tei/cr/Version.java 2008-12-21 11:13:51 UTC (rev 210)
@@ -11,7 +11,7 @@
// don't change this lines; regexp are used during build for updating the fields.
private static final String VERSION = "0.1"; // "8.2";
- private static final String RELEASE_DATE = "20081219-1918";
+ private static final String RELEASE_DATE = "20081221-1138";
private static final String PRODUCT_NAME = "CorpusReader";
private Version() {
Modified: trunk/corpusreader/src/main/java/tei/cr/filters/MarkBalanced.java
===================================================================
--- trunk/corpusreader/src/main/java/tei/cr/filters/MarkBalanced.java 2008-12-20 21:23:44 UTC (rev 209)
+++ trunk/corpusreader/src/main/java/tei/cr/filters/MarkBalanced.java 2008-12-21 11:13:51 UTC (rev 210)
@@ -25,7 +25,7 @@
// ************************************** //
private Stack openedElement = new Stack();
- private char[] codePoint = { '(', ')', '', '' };
+ private char[] codePoint = { '(', ')', '«', '»' };
private boolean[] isOpening = { true, false, true, false };
private String[] markup = { "parenthesis", "PRE laquo POST raquo" };
Modified: trunk/corpusreader/src/main/java/tei/cr/saxConverterImpl/Codegram.java
===================================================================
--- trunk/corpusreader/src/main/java/tei/cr/saxConverterImpl/Codegram.java 2008-12-20 21:23:44 UTC (rev 209)
+++ trunk/corpusreader/src/main/java/tei/cr/saxConverterImpl/Codegram.java 2008-12-21 11:13:51 UTC (rev 210)
@@ -22,15 +22,15 @@
/*-------------------------------------------------------------------------------------------*/
/*
Accepter redondance :
- interrogatif dans "assertivit\xE9" et dans "type de d\xE9terminant"
+ interrogatif dans "assertivit" et dans "type de dterminant"
fini dans "finitude" et dans "type"
nbr dans "type" et dans "nbr"
Accepter perte d'information :
- on ne sait plus lequel est le nombre du possesseur et lequel du poss\xE9d\xE9.
+ on ne sait plus lequel est le nombre du possesseur et lequel du possd.
*/
/* --------------------------------------------------------------------------------- */
- /* cat\xE9gorie partie du discours */
+ /* catgorie partie du discours */
public static final String POS_ADJECTIF = "pos.adj";
public static final String POS_CONJONCTION = "pos.c";
public static final String POS_DETERMINANT = "pos.d";
@@ -44,7 +44,7 @@
public static final String POS_PONCTUATION = "pos.ponct";
/* --------------------------------------------------------------------------------- */
- /* type : sous-cat\xE9gorisation de POS */
+ /* type : sous-catgorisation de POS */
// Adjectif :
public static final String TYPE_QUALIFICATIF = "ty.qual";
@@ -54,14 +54,14 @@
public static final String TYPE_COORDINATION = "ty.coo"; // Cc
public static final String TYPE_SUBORDINATION = "ty.sub"; // Cs
- // d\xE9terminant
+ // dterminant
public static final String TYPE_ARTICLE = "ty.art";
- // d\xE9terminant et pronom
+ // dterminant et pronom
public static final String TYPE_DEMONSTRATIF = "ty.dem";
public static final String TYPE_INTERROGATIF = "ty.int";
- // adjectif d\xE9terminant-adjectif et pronom
+ // adjectif dterminant-adjectif et pronom
public static final String TYPE_POSSESSIF = "ty.poss";
public static final String TYPE_INDEFINI = "ty.ind";
@@ -89,7 +89,7 @@
public static final String COMPARATIF = "comp.comp";
/* --------------------------------------------------------------------------------- */
- /* num\xE9raux */
+ /* numraux */
// Adjectif ordinal:
public static final String NUMERIQUE_ORDINAL = "num.ord";
@@ -110,12 +110,12 @@
public static final String PERSONNEL_PROPRE = "pers.nom";
/* --------------------------------------------------------------------------------- */
- /* r\xE9fl\xE9chis */
+ /* rflchis */
public static final String REFLECHI = "refl.oui";
public static final String NON_REFLECHI = "refl.non";
/* --------------------------------------------------------------------------------- */
- /* cat\xE9gorie syntaxique */
+ /* catgorie syntaxique */
public static final String CAT_SUJET = "cat.s";
public static final String CAT_COD = "cat.cod";
public static final String CAT_COI = "cat.coi";
@@ -126,11 +126,11 @@
- personne :
1, 2, 3
pour pronom : personne du possesseur
- d\xE9terminant possessif : id.
+ dterminant possessif : id.
adj ??
- - nmb_possesseur (ou \xE0 mettre dans nombre seul ?, avec deux traits nombre ?)
+ - nmb_possesseur (ou mettre dans nombre seul ?, avec deux traits nombre ?)
adjectif : possessif
- d\xE9terminant : possessif
+ dterminant : possessif
*/
public static final String PERS_1 = "p.1";
public static final String PERS_2 = "p.2";
@@ -139,21 +139,21 @@
/* --------------------------------------------------------------------------------- */
/* Fini */
/*
- d\xE9fini : d\xE9terminant : article d\xE9fini
- ind\xE9fini :
- d\xE9terminant : article ind\xE9fini,
- d\xE9terminant : adjectif ind\xE9fini (?)
- adjectif : ind\xE9fini (diff\xE9rence avec ci-dessus ???)
- pronom : ind\xE9fini
+ dfini : dterminant : article dfini
+ indfini :
+ dterminant : article indfini,
+ dterminant : adjectif indfini (?)
+ adjectif : indfini (diffrence avec ci-dessus ???)
+ pronom : indfini
*/
- // adj, art, det-adj, et pronom (redouble tout les types "ind\xE9finis"
- // plus les d\xE9terminant-articles "ind\xE9fini ou d\xE9fini"
+ // adj, art, det-adj, et pronom (redouble tout les types "indfinis"
+ // plus les dterminant-articles "indfini ou dfini"
public static final String FIN_DEFINI = "fin.d";
public static final String FIN_INDEFINI = "fin.i";
// adj indef = //tout, quelque, chaque, tel, aucune
- // adj, det et pronom, ajouter det art indefini ? cat n\xE9cessaire ?
+ // adj, det et pronom, ajouter det art indefini ? cat ncessaire ?
/* --------------------------------------------------------------------------------- */
/* Nombre */
@@ -166,21 +166,21 @@
public static final String GENRE_FEMININ = "g.f";
/* --------------------------------------------------------------------------------- */
- /* Assertivit\xE9 */
+ /* Assertivit */
/* assertif :
- d\xE9terminant : d\xE9monstratif,
- pronom : d\xE9monstratif
- interrogativit\xE9
- d\xE9terminant : interrogatif
+ dterminant : dmonstratif,
+ pronom : dmonstratif
+ interrogativit
+ dterminant : interrogatif
pronom : interrogatif
- n\xE9gation
- adv : n\xE9gation*/
+ ngation
+ adv : ngation*/
public static final String ASS_INTERROGATIF = "ass.i";
public static final String ASS_DEMONSTRATIF = "ass.d";
- // penser \xE0 encoder aussi qu'est adj.
+ // penser encoder aussi qu'est adj.
// en commun avec ADJ_POSSESSIF et pronom possessif ?
- // ??? quelle cat\xE9gorie : type ? != ass
+ // ??? quelle catgorie : type ? != ass
public static final String ASS_POSSESSIF = "ass.poss";
// adverbe
@@ -200,7 +200,7 @@
public static final String MODE_IMPERATIF = "m.imp";
public static final String MODE_PARTICIPE = "m.partpres";
- // = participe pass\xE9 ???
+ // = participe pass ???
/* --------------------------------------------------------------------------------- */
/* Temps */
@@ -321,7 +321,7 @@
case 'S':
- /* S p pour une pr\xE9position */
+ /* S p pour une prposition */
addIDREF(POS_PREPOSITION);
break;
@@ -354,29 +354,29 @@
}
/* pour adjectif
- A f c/p <genre> <nombre> pour un adjectif qualificatif (\x93 c \x94 si comparatif, \x93 p \x94 sinon)
- A i - <genre> <nombre> pour un adjectif ind\xE9fini
+ A f c/p <genre> <nombre> pour un adjectif qualificatif ( c 铔 si comparatif, p sinon)
+ A i - <genre> <nombre> pour un adjectif indfini
A o - <genre> <nombre> pour un adjectif ordinal
A s - <genre> <nombre> pour un adjectif possessif
*/
/*
- //QUALIFICATIF : Afpms "sens commun" ; Afpm. "c'est faux du commencement \xE0 la fin.
+ //QUALIFICATIF : Afpms "sens commun" ; Afpm. "c'est faux du commencement la fin.
- Ind\xE9fini :
- ajouter ind\xE9fini : cf. d\xE9terminant, adj ind\xE9fini ???
+ Indfini :
+ ajouter indfini : cf. dterminant, adj indfini ???
"quel" Ai-ms-
quelle (blessure n'est pas de guerre) Ai-fs- => quasi toujours interrogatif ? == beaucoup d'erreur.
- (mais) quelle (\xE9trange tension presque insupportable, cette \xE9treinte [...]) Ai-fs-
- (on peut encore se demander) quel (genre de folie repr\xE9sentent ...) Ai-ms-
+ (mais) quelle (trange tension presque insupportable, cette treinte [...]) Ai-fs-
+ (on peut encore se demander) quel (genre de folie reprsentent ...) Ai-ms-
(ou) quel (est le sujet du discours philosophique ?) Ai-ms-
- (on ne sait plus du tout de) quel (c\xF4t\xE9 est le maximum d'ironie) Ai-ms-
+ (on ne sait plus du tout de) quel (ct est le maximum d'ironie) Ai-ms-
// Ordinal
- // Ao-.. : jamais de genre ou nombre indiqu\xE9 ???
+ // Ao-.. : jamais de genre ou nombre indiqu ???
// adjectif cardinal ??
// possessif :
- // As-mp "je les fasse miens", As-fs "tu es mienne" As-ms "reconna\xEEtra comme sien"
+ // As-mp "je les fasse miens", As-fs "tu es mienne" As-ms "reconnatra comme sien"
// ? et pronom possessif ? et "ma fille" = ?
// + personne, cf. verbe
// + genre, cf. nom
@@ -559,7 +559,7 @@
}
/*
- (o\xF9 <cat> est 'n' pour le sujet, 'a' pour le COD, 'd' pour le COI)
+ (o <cat> est 'n' pour le sujet, 'a' pour le COD, 'd' pour le COI)
!!! ou aussi '-' !!!
*/
public void doCat(int index) {
@@ -604,8 +604,8 @@
/*
R g c pour un adverbe comparatif
- R g p pour un adverbe non comparatif et non de n\xE9gation
- R p n pour l\x92adverbe de n\xE9gation \x93 ne \x94 ou \x93 n\x92 \x94
+ R g p pour un adverbe non comparatif et non de ngation
+ R p n pour ladverbe de ngation ne 铔 ou n
*/
private void doAdverbe() {
if (codechar.length < 3) {
@@ -642,18 +642,18 @@
}
/*
- D a - <genre> <nombre> d pour un article d\xE9fini
+ D a - <genre> <nombre> d pour un article dfini
le Da-ms-d
la Da-fs-d
les Da-.p-d
du Da-ms-d
!!! plus '-' !!!
- D a - <genre> <nombre> i pour un article ind\xE9fini
- (celle) des (choses et celle du monde) Da-.p-i == erreur, ici d\xE9fini !!!
+ D a - <genre> <nombre> i pour un article indfini
+ (celle) des (choses et celle du monde) Da-.p-i == erreur, ici dfini !!!
du (monde) Da-ms-d
- le pays des (f\xE9es) Da-.p-i
+ le pays des (fes) Da-.p-i
- D d - <genre> <nombre> pour un adjectif d\xE9monstratif
+ D d - <genre> <nombre> pour un adjectif dmonstratif
cette Dd-fs-
Cet Dd-ms-
ces termes Dd-.p-
@@ -664,27 +664,27 @@
AUCUN ds LS ou MP !!!
D s 1/2/3 <nombre> <nb poss.> pour un adjectif possessif (1 ou 2 ou 3 selon la personne),
- <nb poss.> est \xE9gal \xE0 'p' si le possesseur est pluriel, \xE0 's' s'il est singulier
+ <nb poss.> est gal 'p' si le possesseur est pluriel, 's' s'il est singulier
!!! ++ genre !!!
- (le langage atteint \xE0) sa (plus haute puissance) Ds3fss
- diff\xE9rent du n\xF4tre Ps1.sa.
+ (le langage atteint ) sa (plus haute puissance) Ds3fss
+ diffrent du ntre Ps1.sa.
qui ne sont plus la sienne Ps3fs-s
tellement semblable aux siennes Ps2fpas
- sup\xE9rieur \xE0 la n\xF4tre Ps1.sd.
+ suprieur la ntre Ps1.sd.
il y a toujours un autre souffle dans le mien Ps1msds
- une autre pens\xE9e dans la mienne Ps1fsds
+ une autre pense dans la mienne Ps1fsds
aucun nom propre ne se substitue au souffle hyperbolique du mien Ps1msds
- qui ne se laisse pas substituer \xE0 la mienne Ps1fsns
- analogue au n\xF4tre Ps1.sa.
- le v\xF4tre Ps2.s-.
+ qui ne se laisse pas substituer la mienne Ps1fsns
+ analogue au ntre Ps1.sa.
+ le vtre Ps2.s-.
- D t - <genre> <nombre> pour un adjectif ind\xE9fini
- (qui se d\xE9robe \xE0) toute (identification) Dt-fs-
- (o\xF9) chaque (syst\xE8me individuel se d\xE9fait) Dt-.s-
- (qui repporte la qualit\xE9 \xE0 ) quelque (chose de fixe) Dt-.s-
- (de) telle (mani\xE8re que) aucune Dt-fs- Dt-fs-
- (pourquoi \xE0) tel (moment plut\xF4t qu'\xE0 tel autre ?) Dt-ms-
+ D t - <genre> <nombre> pour un adjectif indfini
+ (qui se drobe ) toute (identification) Dt-fs-
+ (o) chaque (systme individuel se dfait) Dt-.s-
+ (qui repporte la qualit ) quelque (chose de fixe) Dt-.s-
+ (de) telle (manire que) aucune Dt-fs- Dt-fs-
+ (pourquoi ) tel (moment plutt qu' tel autre ?) Dt-ms-
(qui n'autorise) aucun (amalgame) Dt-ms-
*/
private void doDeterminant() {
@@ -785,9 +785,9 @@
}
/*
- M c <genre> <nombre> pour un num\xE9ral
- (o\xF9 <genre> est cod\xE9 par \x93 m \x94 pour masculin, \x93 f \x94 pour f\xE9minin,
- et o\xF9 <nombre> est cod\xE9 \x93 s \x94 pour singulier, \x93 p \x94 pour pluriel)
+ M c <genre> <nombre> pour un numral
+ (o <genre> est cod par m 铔 pour masculin, f pour fminin,
+ et o <nombre> est cod s 铔 pour singulier, p pour pluriel)
N c <genre> <nombre> pour un nom commun
N p <genre> <nombre> pour un nom propre
*/
@@ -824,27 +824,27 @@
/*
- // d\xE9monstratif, ind\xE9fini, personnel, non r\xE9fl\xE9chi, relatif, possessif, interrogatif,
- // personnel non-r\xE9fl\xE9chis :
+ // dmonstratif, indfini, personnel, non rflchi, relatif, possessif, interrogatif,
+ // personnel non-rflchis :
- // utiliser les autres cat\xE9gories. Distinguer "ind\xE9fini" comme cat\xE9gorie (commune \xE0 det ?)
- // et comme type d'article ? possessif : \xE0 noter avec adj et det possessifs.
- // personnel : en faire un type commun (avec d\xE9monstratif, etc.)
- // avec une sous-cat\xE9gorie r\xE9fl\xE9chi ou non)
+ // utiliser les autres catgories. Distinguer "indfini" comme catgorie (commune det ?)
+ // et comme type d'article ? possessif : noter avec adj et det possessifs.
+ // personnel : en faire un type commun (avec dmonstratif, etc.)
+ // avec une sous-catgorie rflchi ou non)
- P d - <genre> <nombre> <cat> pour un pronom d\xE9monstratif
- (o\xF9 <cat> est 'n' pour le sujet, 'a' pour le COD, 'd' pour le COI)
+ P d - <genre> <nombre> <cat> pour un pronom dmonstratif
+ (o <cat> est 'n' pour le sujet, 'a' pour le COD, 'd' pour le COI)
!!! ou aussi '-' !!!
- P i - <genre> <nombre> <cat> pour un pronom ind\xE9fini
+ P i - <genre> <nombre> <cat> pour un pronom indfini
// = ???
- P p 1/2/3 <nombre> <cat> pour un pronom personnel non r\xE9fl\xE9chi
+ P p 1/2/3 <nombre> <cat> pour un pronom personnel non rflchi
P r -<genre> <nombre> <cat> pour un pronom relatif
Pr-ms-
P s - 1/2/3 <nombre> <cat> pour un pronom possessif
!!! + genre !!!
!!! pas de tiret !!!
P t - <genre> <nombre> <cat> pour un pronom interrogatif
- P x 1/2/3 <nombre> pour un pronom personnel r\xE9fl\xE9chi
+ P x 1/2/3 <nombre> pour un pronom personnel rflchi
!!! + genre !!!
!!! + cat !!!
@@ -926,19 +926,19 @@
}
/*
- V a/m n/i/s/c/f/p p/i/s/f/r/m/c/\xE9/a 1/2/3 s/p (ou <genre> <nombre> si participe)
+ V a/m n/i/s/c/f/p p/i/s/f/r/m/c//a 1/2/3 s/p (ou <genre> <nombre> si participe)
!!! c'est l'inverse : <nbr><genre> si participe !
- o\xF9 a/m correspond \xE0 \x93 auxiliaire \x94 ou \x93 principal \x94
- o\xF9 n/i/s/c/f/p correspond au mode
- (\x93 n \x94=infinitif, \x93 i \x94=indicatif, \x93 s \x94=subjonctif, \x93 c \x94=conditionnel,
- \x93 f \x94=imp\xE9ratif, \x93 p \x94=participe)
- o\xF9 p/i/s/f/r/m/c/\xE9/a correspond au temps
- (\x93 p \x94=pr\xE9sent, \x93 i \x94=imparfait, \x93 s \x94=pass\xE9, \x93 f \x94=futur,
- \x93 r \x94=subjonctif pr\xE9sent, \x93 m \x94=subjonctif imparfait,
- \x93 c \x94=conditionnel, \x93 \xE9 \x94=imp\xE9ratif, \x93 a \x94= participe pass\xE9
- o\xF9 1/2/3 est \xE9gal \xE0 1 ou 2 ou 3 selon la personne
- o\xF9 s/p correspond \xE0 \x93 singulier \x94 ou \x93 pluriel \x94 pour la personne
+ o a/m correspond auxiliaire Ӕ ou principal
+ o n/i/s/c/f/p correspond au mode
+ ( n ?=infinitif, ? i =indicatif, s =subjonctif, c =conditionnel,
+ f =impratif, p 铔=participe)
+ o p/i/s/f/r/m/c//a correspond au temps
+ ( p 铔=prsent, i 铔=imparfait, s =pass, f 铔=futur,
+ r =subjonctif prsent, m 铔=subjonctif imparfait,
+ c =conditionnel, =impratif, a 铔= participe pass
+ o 1/2/3 est gal 1 ou 2 ou 3 selon la personne
+ o s/p correspond singulier Ӕ ou pluriel pour la personne
*/
private void doVerbe() {
boolean isParticipe = false;
@@ -983,7 +983,7 @@
return; // infinitif: do not process further.
- // ??? s\xFBr de ne rien perdre ?
+ // ??? sr de ne rien perdre ?
case 'i':
addIDREF(MODE_INDICATIF);
@@ -1068,11 +1068,11 @@
break;
- case '\xE9':
- addIDREF(TEMPS_IMPERATIF);
+// case '':
+// addIDREF(TEMPS_IMPERATIF);
+//
+// break;
- break;
-
case 'a':
addIDREF(TEMPS_PARTICIPE_PASSE);
@@ -1101,8 +1101,8 @@
/*
Y p w ponctuation finale pour une fin de phrase
Y p s ponctuation de pause pour une virgule ou une ponctuation de pause
- Y p o ponctuation d\x92insertion pour une parenth\xE8se ouverte ou signe voisin
- Y p c ponctuation de fin d\x92insertion pour une parenth\xE8se ferm\xE9e ou signe voisin
+ Y p o ponctuation dinsertion pour une parenthse ouverte ou signe voisin
+ Y p c ponctuation de fin dinsertion pour une parenthse ferme ou signe voisin
Y s s ponctuation pour un autre type de ponctuation
*/
private void doPonctuation() {
Modified: trunk/corpusreader/src/main/java/tei/cr/utils/syntex/SyntexBuffer.java
===================================================================
--- trunk/corpusreader/src/main/java/tei/cr/utils/syntex/SyntexBuffer.java 2008-12-20 21:23:44 UTC (rev 209)
+++ trunk/corpusreader/src/main/java/tei/cr/utils/syntex/SyntexBuffer.java 2008-12-21 11:13:51 UTC (rev 210)
@@ -1,9 +1,15 @@
package tei.cr.utils.syntex;
+import java.io.BufferedReader;
import java.io.FileNotFoundException;
+import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
+
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
@@ -11,39 +17,66 @@
import tei.cr.utils.stax.StAXBuffer;
import tei.cr.utils.stax.StAXBufferImpl;
+/**
+ * Get an iterator over sentences of a Syntex file.
+ *
+ * @author sloiseau
+ *
+ */
public class SyntexBuffer {
- private final StAXBuffer syntex;
+ private XMLEventReader reader;
public SyntexBuffer(String uri) throws FileNotFoundException {
- syntex = new StAXBufferImpl(uri);
+ XMLInputFactory f = XMLInputFactory.newInstance();
+ f.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
+ f.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
+ f.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
+ try {
+ reader = f.createXMLEventReader(new BufferedReader(new FileReader(uri)));
+ } catch (XMLStreamException xsE) {
+ IllegalStateException iSE = new IllegalStateException("Unable to create reader: " +
+ xsE.getMessage());
+ iSE.initCause(xsE);
+ throw iSE;
+ }
}
public SyntexSentenceIterator getSentenceIterator() {
- return new SyntexSentenceIterator(syntex);
+ return new SyntexSentenceIterator(reader);
}
public class SyntexSentenceIterator {
-
- private int currentEventIndex = 0;
- private List<XMLEvent> events;
- public SyntexSentenceIterator(StAXBuffer syntex) {
- events = syntex.getXMLEvents();
+ private List<List<XMLEvent>> buffer;
+ private XMLEventReader reader;
+ /** does the XMLReader iterator has next element */
+ private boolean hasNextElement = true;
+ /** does the Sentence iterator has next element */
+ private boolean hasNextSentence = true;
+ private final static int BUFFER_SIZE = 100;
+
+ public SyntexSentenceIterator(XMLEventReader reader) {
+ if (reader == null) {
+ throw new IllegalArgumentException("");
+ }
+ this.reader = reader;
+ buffer = new ArrayList<List<XMLEvent>>();
+ goToNextSentence();
}
/**
- * Return the complete (<code>qualified</code>) id of the next sentence.
+ * Return the complete (qualified) id of the next sentence.
*
* @return the complete id.
+ *
* @throws NoSuchElementException if there is no more sentence.
*/
- public String getNextSentenceQualifiedId() {
- int index = getNextSentenceIndex();
- if (index == -1) {
+ public String peekNextSentenceQualifiedId() {
+ if (!hasNextSentence()) {
throw new NoSuchElementException("No more sentence");
}
- Attribute a = events.get(index).asStartElement().getAttributeByName(SyntexVocabulary.SEQ_ELEMENT_ID_ATTRIBUTE);
+ Attribute a = buffer.get(0).get(0).asStartElement().getAttributeByName(SyntexVocabulary.SEQ_ELEMENT_ID_ATTRIBUTE);
return a.getValue();
}
@@ -53,11 +86,11 @@
* @return the complete id.
* @throws NoSuchElementException if there is no more sentence.
*/
- public String getNextSentenceDocumentId() {
- String qualifiedId = getNextSentenceQualifiedId();
+ public String peekNextSentenceDocumentId() {
+ String qualifiedId = peekNextSentenceQualifiedId();
return getDocumentPart(qualifiedId);
}
-
+
/**
* Return the next sentence
*
@@ -66,64 +99,139 @@
* @throws NoSuchElementException if there is no more sentence.
*/
public List<XMLEvent> getNextSentence() {
- List<XMLEvent> sentence = new ArrayList<XMLEvent>();
- int start = getNextSentenceIndex();
- if (start == -1) {
+ if (!hasNextSentence()) {
throw new NoSuchElementException("No more sentence");
}
- int end = getNextSentenceEndIndex(start);
- if (end == -1) {
- throw new IllegalStateException("No EndElement for this sentence. Bug somewhere.");
- }
- List<XMLEvent> subList = events.subList(start, end);
- sentence.addAll(subList);
- currentEventIndex = end;
- return sentence;
+ return buffer.remove(0);
}
+ // -----------------------
+
+ /**
+ * Fill the buffer and test the wrapped reader. Must be called by all
+ * methods needing to test for availability of sentences.
+ */
public boolean hasNextSentence() {
- int index = getNextSentenceIndex();
- return index != -1;
+ if (!buffer.isEmpty()) {
+ return true;
+ } else {
+ fillBuffer();
+ return hasNextSentence;
+ }
}
+ // protected
+
+ protected String getDocumentPart(String qualifiedId) {
+ return qualifiedId.substring(0, qualifiedId.lastIndexOf("_"));
+ }
+
/**
- * Get the index of the StartElement of the next sentence.
- * @return
+ * This method set the <code>hasNextSentence</code> flag.
*/
- protected int getNextSentenceIndex() {
- for (int i = currentEventIndex; i+1 < events.size(); i++) {
- XMLEvent e = events.get(i);
- if (e.isStartElement()) {
- StartElement se = e.asStartElement();
- if (se.getName().getLocalPart().equals(SyntexVocabulary.SENTENCE)) {
- return i;
- }
+ protected void fillBuffer() {
+ while (buffer.size() < BUFFER_SIZE && hasNextElement) {
+ List<XMLEvent> sentence = getOneSentence();
+ if (sentence != null) {
+ buffer.add(sentence);
+ } else {
+ break;
}
}
- return -1;
+ if (!hasNextElement && buffer.size() == 0) {
+ hasNextSentence = false;
+ }
}
- /**
- * Get the index of the EndElement of the next sentence.
- * @return
- */
- protected int getNextSentenceEndIndex(int nextSentenceStartIndex) {
- int end = nextSentenceStartIndex + 1;
- for (; end < events.size(); end++) {
- XMLEvent e = events.get(end);
- if (e.isEndElement()) {
- EndElement se = e.asEndElement();
- if (se.getName().getLocalPart().equals(SyntexVocabulary.SENTENCE)) {
- return end;
+ // the for methods below access the reader, deal with XMLStreamException, and set hasNextElement.
+
+ protected List<XMLEvent> getOneSentence() {
+ // skip the blank char between two sentences.
+ goToNextSentence();
+
+ if (!hasNextElement) {
+ return null;
+ }
+// XMLEvent ev = null;
+// try {
+// ev = (XMLEvent) reader.peek();
+// } catch (XMLStreamException e1) {
+// // TODO Auto-generated catch block
+// e1.printStackTrace();
+// }
+// System.out.println("---" + ev + "----");
+ if (!isOnSentenceStart()) {
+ try {
+ if (reader.peek() == null) {
+ hasNextElement = false;
+ return null;
+ } else if (reader.peek().isEndElement() && reader.peek().asEndElement().getName().getLocalPart().equals("syntex")) {
+ hasNextElement = false;
+ return null;
}
+ } catch (XMLStreamException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
+ try {
+ throw new IllegalStateException("Next element is not a start sentence: " + reader.peek());
+ } catch (XMLStreamException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
}
- return -1;
+
+ List<XMLEvent> sentence = new ArrayList<XMLEvent>();
+ while (!isOnSentenceEnd()) {
+ sentence.add((XMLEvent)reader.next());
+ }
+ XMLEvent last = (XMLEvent)reader.next();
+// System.out.println("last: " + last);
+ sentence.add(last);
+ return sentence;
}
- protected String getDocumentPart(String qualifiedId) {
- return qualifiedId.substring(0, qualifiedId.lastIndexOf("_"));
+ protected boolean isOnSentenceStart() {
+ try {
+ if (reader.peek() == null) {
+ hasNextElement = false;
+ return false;
+ }
+// System.out.println(reader.peek());
+ return
+ reader.peek().isStartElement()
+ &&
+ reader.peek().asStartElement().getName().getLocalPart().equals(SyntexVocabulary.SENTENCE);
+ } catch (XMLStreamException e) {
+ throw new IllegalStateException(e);
+ }
}
+ protected boolean isOnSentenceEnd() {
+ try {
+ if (reader.peek() == null) {
+ hasNextElement = false;
+ return false;
+ }
+ return
+ reader.peek().isEndElement()
+ &&
+ reader.peek().asEndElement().getName().getLocalPart().equals(SyntexVocabulary.SENTENCE);
+ } catch (XMLStreamException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ /** used for positionning the reader on the first sentence. */
+ protected void goToNextSentence() {
+ while (!isOnSentenceStart()) {
+ if (!reader.hasNext()) {
+ hasNextElement = false;
+ return;
+ }
+ XMLEvent e = (XMLEvent)reader.next();
+// System.out.println(">" + e + "<");
+ }
+ }
}
}
Modified: trunk/corpusreader/src/test/java/tei/cr/utils/syntex/TestSyntexBuffer.java
===================================================================
--- trunk/corpusreader/src/test/java/tei/cr/utils/syntex/TestSyntexBuffer.java 2008-12-20 21:23:44 UTC (rev 209)
+++ trunk/corpusreader/src/test/java/tei/cr/utils/syntex/TestSyntexBuffer.java 2008-12-21 11:13:51 UTC (rev 210)
@@ -14,6 +14,7 @@
protected void setUp() {
try {
+ // the test file must have 2 sentences in order for the tests to work.
syntexBuffer = new SyntexBuffer(System.getProperty("user.dir") + "/src/test/resources/syntex.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
@@ -24,38 +25,51 @@
syntexBuffer = null;
}
- public void testGetSentenceIterator() {
- syntexBuffer.getSentenceIterator();
- }
-
- public void testNextSentenceIndex() {
+ public void testGoToNextSentence() {
SyntexSentenceIterator it = syntexBuffer.getSentenceIterator();
- assertEquals(3, it.getNextSentenceIndex());
+ it.goToNextSentence();
+ assertTrue(it.isOnSentenceStart());
}
- public void testNextSentenceEndIndex() {
+// public void testIsOnSentenceStart() {
+// SyntexSentenceIterator it = syntexBuffer.getSentenceIterator()
+// it.goToNextSentence();
+// List<XMLEvent> sentence = it.getNextSentence();
+//
+// }
+
+ public void testGetOneSentence() {
SyntexSentenceIterator it = syntexBuffer.getSentenceIterator();
- int start = it.getNextSentenceIndex();
- assertEquals(464, it.getNextSentenceEndIndex(start));
+ it.goToNextSentence();
+
+ // since there is two sentences in the test file.
+ List<XMLEvent> sentence = it.getOneSentence();
+ assertTrue(sentence != null);
+
+ sentence = it.getOneSentence();
+ assertTrue(sentence != null);
+
+ sentence = it.getOneSentence();
+ assertTrue(sentence == null);
}
- public void testNextSentenceDocumentId() {
+ public void testPeekNextSentenceDocumentId() {
SyntexSentenceIterator it = syntexBuffer.getSentenceIterator();
- assertEquals("300001_1782665", it.getNextSentenceDocumentId());
+ assertEquals("300001_1782665", it.peekNextSentenceDocumentId());
}
- public void testNextSentenceQualifiedId() {
+ public void testPeekNextSentenceQualifiedId() {
SyntexSentenceIterator it = syntexBuffer.getSentenceIterator();
- assertEquals("300001_1782665_1", it.getNextSentenceQualifiedId());
+ assertEquals("300001_1782665_1", it.peekNextSentenceQualifiedId());
}
public void testHasSentence() {
SyntexSentenceIterator it = syntexBuffer.getSentenceIterator();
assertTrue(it.hasNextSentence());
-// List<XMLEvent> sent1 = it.getNextSentence();
+ List<XMLEvent> sent1 = it.getNextSentence();
// System.out.println(sent1);
assertTrue(it.hasNextSentence());
-// List<XMLEvent> sent2 = it.getNextSentence();
+ List<XMLEvent> sent2 = it.getNextSentence();
// System.out.println(sent2);
assertFalse(it.hasNextSentence());
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|