+
    ÖÇÜhF:  ã                   ó  € ^ RI t ^ RIt^ RIt^ RIt^ RIHt ^ RIHtHtH	t	 ^ RI
HtHt ^ RIHtHt R tR tR tR	 tR
 tR tR tR tR tR tR tR tRR ltR tRR lt]! RR.4      t]! . RO4      t R t!]"R8X  d
   ]! 4        R# R# )é    N)Útreebank)ÚBrillTaggerTrainerÚRegexpTaggerÚUnigramTagger)ÚPosÚWord)ÚTemplateÚ
error_listc                 ó   € \        4        R# )zx
Run a demo with defaults. See source comments for details,
or docstrings of any of the more specific demo_* functions.
N©Úpostag© ó    Ú//usr/lib/python3/dist-packages/nltk/tbl/demo.pyÚdemor      s	   € ô
 †Hr   c                 ó   € \        RR7       R# )úF
Exemplify repr(Rule) (see also str(Rule) and Rule.format("verbose"))
Úrepr©Ú
ruleformatNr   r   r   r   Údemo_repr_rule_formatr      s   € ô �f×r   c                 ó   € \        RR7       R# )r   Ústrr   Nr   r   r   r   Údemo_str_rule_formatr   $   s   € ô �e×r   c                 ó   € \        RR7       R# )z"
Exemplify Rule.format("verbose")
Úverboser   Nr   r   r   r   Údemo_verbose_rule_formatr   +   s   € ô �i× r   c                 óH   € \        \        \        . RO4      4      .R7       R# )aš  
The feature/s of a template takes a list of positions
relative to the current word where the feature should be
looked for, conceptually joined by logical OR. For instance,
Pos([-1, 1]), given a value V, will hold whenever V is found
one step to the left and/or one step to the right.

For contiguous ranges, a 2-arg form giving inclusive end
points can also be used: Pos(-3, -1) is the same as the arg
below.
©Ú	templatesN)éýÿÿÿéþÿÿÿéÿÿÿÿ)r   r	   r   r   r   r   Údemo_multiposition_featurer$   2   s   € ô ”hœs¢<Ó0Ó1Ð2×3r   c            	     ó^   € \        \        \        ^ .4      \        RR.4      4      .R7       R# )z0
Templates can have more than a single feature.
r   Nr"   r#   )r   r	   r   r   r   r   r   Údemo_multifeature_templater&   A   s$   € ô ”hœt Q C›y¬#¨r°2¨h«-Ó8Ð9×:r   c                 ó    € \        RRR7       R# )aP  
Show aggregate statistics per template. Little used templates are
candidates for deletion, much used templates may possibly be refined.

Deleting unused templates is mostly about saving time and/or space:
training is basically O(T) in the number of templates T
(also in terms of memory usage, which often will be the limiting factor).
T)Úincremental_statsÚtemplate_statsNr   r   r   r   Údemo_template_statisticsr*   H   s   € ô ˜T°$×7r   c                 ó   € \         P                  ! . RO^^.RR7      p \        P                  ! . R	O^^.RR7      p\        \        P                  ! W.R
R7      4      p\        RP                  \        V4      4      4       \        VRRR7       R# )zñ
Template.expand and Feature.expand are class methods facilitating
generating large amounts of templates. See their documentation for
details.

Note: training with 500 templates can easily fill all available
even on relatively small corpora
F)ÚexcludezeroT)Úcombinationsz8Generated {} templates for transformation-based learning)r    r(   r)   N)r#   r   é   )r"   r#   r   r.   )r.   é   )	r   Úexpandr   Úlistr	   ÚprintÚformatÚlenr   )ÚwordtplsÚtagtplsr    s      r   Údemo_generated_templatesr7   T   ss   € ô �{Š{š:¨¨1 v¸5ÔA€HÜ�jŠjš¨!¨Q¨¸TÔB€GÜ”X—_’_ hÐ%8ÀvÔNÓO€IÜ	ØB×IÑIÜ�	‹Nó	
ôô
 �Y°$Àt×Lr   c                 ó"   € \        RRRR7       R# )zr
Plot a learning curve -- the contribution on tagging accuracy of
the individual rules.
Note: requires matplotlib
Tzlearningcurve.png)r(   Úseparate_baseline_dataÚlearning_curve_outputNr   r   r   r   Údemo_learning_curver;   h   s   € ô ØØ#Ø1÷r   c                 ó   € \        RR7       R# )zO
Writes a file with context for each erroneous word after tagging testing data
z
errors.txt)Úerror_outputNr   r   r   r   Údemo_error_analysisr>   u   s   € ô ˜×%r   c                 ó   € \        RR7       R# )za
Serializes the learned tagger to a file in pickle format; reloads it
and validates the process.
z
tagger.pcl)Úserialize_outputNr   r   r   r   Údemo_serialize_taggerrA   |   s   € ô
 ˜L×)r   c                 ó"   € \        RR^
R7       R# )zŒ
Discard rules with low accuracy. This may hurt performance a bit,
but will often produce rules which are more interesting read to a human.
i¸  g¸…ëQ¸î?)Ú	num_sentsÚmin_accÚ	min_scoreNr   r   r   r   Údemo_high_accuracy_rulesrF   „   s   € ô
 �T 4°2×6r   c           
     ó`	  € T;'       g    \         pV f   ^ RIHpHp V! 4       p \	        WW(V4      w  ppppV'       dÂ   \
        P                  P                  V4      '       g\   \        VVR7      p\        VR4      ;_uu_ 4       p\        P                  ! VV4       RRR4       \        RP                  V4      4       \        V4      ;_uu_ 4       p\        P                  ! V4      p\        RV 24       RRR4       M\        VVR7      p\        R4       V'       d*   \        RP                  XP                  V4      4      4       \         P                   ! 4       p\#        XWV	R	7      p\        R
4       VP%                  VW4V4      p\        R\         P                   ! 4       V,
          R R24       V'       d"   \        RVP                  V4      ,          4       V^8X  dQ   \        R4       \'        VP)                  4       ^4       F'  w  pp\        VR RVP                  V	4      R 24       K)  	  V
'       dŽ   \        R4       VP+                  VV4      w  p p!\        R4       V'       g   \        R4       VP-                  4       p"V'       d   VP/                  V!4       V'       d   \1        VV!V"VR7       \        RV 24       M4\        R4       VP3                  V4      p V'       d   VP/                  4        Ve„   \        VR4      ;_uu_ 4       p#V#P5                  RV,          4       V#P5                  RP7                  \9        VV 4      4      P;                  R4      R,           4       RRR4       \        RV 24       VeÆ   VP3                  V4      p \        VR4      ;_uu_ 4       p\        P                  ! VV4       RRR4       \        RV 24       \        V4      ;_uu_ 4       p\        P                  ! V4      p$RRR4       \        RV 24       VP3                  V4      p%V V%8X  d   \        R4       R# \        R4       R# R#   + '       g   i     EL»; i  + '       g   i     EL\; i  + '       g   i     EL; i  + '       g   i     LÎ; i  + '       g   i     L¡; i) aâ	  
Brill Tagger Demonstration
:param templates: how many sentences of training and testing data to use
:type templates: list of Template

:param tagged_data: maximum number of rule instances to create
:type tagged_data: C{int}

:param num_sents: how many sentences of training and testing data to use
:type num_sents: C{int}

:param max_rules: maximum number of rule instances to create
:type max_rules: C{int}

:param min_score: the minimum score for a rule in order for it to be considered
:type min_score: C{int}

:param min_acc: the minimum score for a rule in order for it to be considered
:type min_acc: C{float}

:param train: the fraction of the the corpus to be used for training (1=all)
:type train: C{float}

:param trace: the level of diagnostic tracing output to produce (0-4)
:type trace: C{int}

:param randomize: whether the training data should be a random subset of the corpus
:type randomize: C{bool}

:param ruleformat: rule output format, one of "str", "repr", "verbose"
:type ruleformat: C{str}

:param incremental_stats: if true, will tag incrementally and collect stats for each rule (rather slow)
:type incremental_stats: C{bool}

:param template_stats: if true, will print per-template statistics collected in training and (optionally) testing
:type template_stats: C{bool}

:param error_output: the file where errors will be saved
:type error_output: C{string}

:param serialize_output: the file where the learned tbl tagger will be saved
:type serialize_output: C{string}

:param learning_curve_output: filename of plot of learning curve(s) (train and also test, if available)
:type learning_curve_output: C{string}

:param learning_curve_take: how many rules plotted
:type learning_curve_take: C{int}

:param baseline_backoff_tagger: the file where rules will be saved
:type baseline_backoff_tagger: tagger

:param separate_baseline_data: use a fraction of the training data exclusively for training baseline
:type separate_baseline_data: C{bool}

:param cache_baseline_tagger: cache baseline tagger to this file (only interesting as a temporary workaround to get
                              deterministic output from the baseline unigram tagger between python versions)
:type cache_baseline_tagger: C{string}


Note on separate_baseline_data: if True, reuse training data both for baseline and rule learner. This
is fast and fine for a demo, but is likely to generalize worse on unseen data.
Also cannot be sensibly used for learning curves on training data (the baseline will be artificially high).
N)Úbrill24Údescribe_template_sets)ÚbackoffÚwz)Trained baseline tagger, pickled it to {}zReloaded pickled tagger from zTrained baseline taggerz!    Accuracy on test set: {:0.4f}r   zTraining tbl tagger...zTrained tbl tagger in z0.2fz secondsz    Accuracy on test set: %.4fz
Learned rules: Ú4dÚ ÚszJIncrementally tagging the test data, collecting individual rule statisticsz    Rule statistics collectedzbWARNING: train_stats asked for separate_baseline_data=True; the baseline will be artificially high)Útakez Wrote plot of learning curve to zTagging the test datazErrors for Brill Tagger %r

Ú
zutf-8z)Wrote tagger errors including context to zWrote pickled tagger to z4Reloaded tagger tried on test set, results identicalz;PROBLEM: Reloaded tagger gave different results on test set)ÚREGEXP_TAGGERÚnltk.tag.brillrH   rI   Ú_demo_prepare_dataÚosÚpathÚexistsr   ÚopenÚpickleÚdumpr2   r3   ÚloadÚaccuracyÚtimer   ÚtrainÚ	enumerateÚrulesÚbatch_tag_incrementalÚtrain_statsÚprint_template_statisticsÚ
_demo_plotÚ	tag_sentsÚwriteÚjoinr
   Úencode)&r    Útagged_datarC   Ú	max_rulesrE   rD   r]   ÚtraceÚ	randomizer   r(   r)   r=   r@   r:   Úlearning_curve_takeÚbaseline_backoff_taggerr9   Úcache_baseline_taggerrH   rI   Útraining_dataÚbaseline_dataÚ	gold_dataÚtesting_dataÚbaseline_taggerÚprint_rulesÚtbrillÚtrainerÚbrill_taggerÚrulenoÚruleÚ
taggedtestÚ	teststatsÚ
trainstatsÚfÚbrill_tagger_reloadedÚtaggedtest_reloadeds&   &&&&&&&&&&&&&&&&&&&                   r   r   r   Œ   sø  € ðp 6×FÐF¼ÐØÒßBñ
 “Iˆ	Ü>PØ˜IÐ2Hó?Ñ;€]�M 9¨l÷ Ü�w‰w�~‰~Ð3×4Ò4Ü+ØÐ'>ôˆOô Ð+¨S×1Ô1°[Ü—’˜O¨[Ô9÷ 2äØ;×BÑBØ)óôô
 Ð'×(Ô(¨KÜ$Ÿkšk¨+Ó6ˆOÜÐ1Ð2GÐ1HÐIÔJ÷ )Ð(ô (¨Ð?VÔWˆÜÐ'Ô(ßÜØ/×6Ñ6Ø×(Ñ(¨Ó3óô	
ô �YŠY‹[€FÜ Ø˜°jô€Gô 
Ð
"Ô#Ø—=‘= °	ÀgÓN€LÜ	Ð"¤4§9¢9£;°Õ#7¸Ð"=¸XÐ
FÔGßÜÐ.°×1FÑ1FÀyÓ1QÕQÔRð �„zÜÐ!Ô"Ü% l×&8Ñ&8Ó&:¸AÖ>‰LˆF�DÜ�V˜B�K˜q §¡¨ZÓ!8¸Ð ;Ð<Ö=ñ ?÷
 ÜØXô	
ð #/×"DÑ"DØ˜)ó#
Ñˆ�Yô 	Ð-Ô.ß%Üð,ôð "×-Ñ-Ó/ˆ
ßØ×2Ñ2°9Ô=ß ÜØ% y°*ÐCVõô Ð4Ð5JÐ4KÐLÔMøäÐ%Ô&Ø!×+Ñ+¨LÓ9ˆ
ßØ×2Ñ2Ô4ð ÒÜ�, ×$Ô$¨Ø�G‰GÐ4Ð7GÕGÔHØ�G‰G�D—I‘Iœj¨°JÓ?Ó@×GÑGÈÓPÐSWÕWÔX÷ %ô 	Ð9¸,¸ÐHÔIð Ò#Ø!×+Ñ+¨LÓ9ˆ
ÜÐ" C×(Ô(¨KÜ�KŠK˜ kÔ2÷ )äÐ(Ð)9Ð(:Ð;Ô<ÜÐ"×#Ô# {Ü$*§K¢K°Ó$<Ð!÷ $äÐ-Ð.>Ð-?Ð@ÔAØ*×4Ñ4°\ÓBÐØÐ,Ô,ÜÐHÖIäÐOÖPñ $÷U 2×1Ð1ú÷ )×(Ð(ú÷z %×$Ð$ú÷ )×(ú÷ $×#ús=   ÂQÃ%Q"ÌAQ6Î,R
Ï-RÑQ	Ñ"Q3	Ñ6R	Ò
R	ÒR-	c           	      óÜ  € V f!   \        R4       \        P                  ! 4       p Ve   \        V 4      V8:  d   \        V 4      pV'       d6   \        P
                  ! \        V 4      4       \        P                  ! V 4       \        W!,          4      pV R V pWV pV UU	u. uF  qˆ U	u. uF  q™^ ,          NK  	  up	NK  	  p
pp	V'       g   TpM\        V4      ^,          pVR V WlR  rk\        V4      w  rÞ\        V
4      w  pp\        V4      w  pp\        RVR RVR R24       \        RVR RVR R24       \        RP                  TTV'       d   RMR	4      4       WkWz3# u up	i u up	pi )
Nz%Loading tagged data from treebank... zRead testing data (Údz sents/z wds)zRead training data (z-Read baseline data ({:d} sents/{:d} wds) {:s}Ú z[reused the training set])
r2   r   Útagged_sentsr4   ÚrandomÚseedÚshuffleÚintÚcorpus_sizer3   )rh   r]   rC   rk   r9   Úcutoffro   rq   ÚsentÚtrr   rp   Ú	bl_cutoffÚ	trainseqsÚtraintokensÚtestseqsÚ
testtokensÚbltrainseqsÚbltraintokenss   &&&&&              r   rS   rS   Q  ss  € ð
 ÒÜÐ5Ô6Ü×+Ò+Ó-ˆØÒœC Ó,°	Ô9Ü˜Ó$ˆ	ßÜ�Š”C˜Ó$Ô%Ü�Š�{Ô#Ü�Õ"Ó#€FØ  Ð(€MØ 9Ð-€IÙ5>Ô?±Y¨T 4Ó(¡4˜a�q—T�T¡4Ô(±Y€LÑ?ß!Ø%‰ä˜Ó&¨!Õ+ˆ	à˜*˜9Ð%Ø˜*Ð%ð &ô  +¨=Ó9Ñ€YÜ(¨Ó6Ñ€XˆzÜ#.¨}Ó#=Ñ €[�-Ü	Ð ¨˜|¨7°:¸a°.ÀÐ
FÔGÜ	Ð  ¨1 ¨W°[À°OÀ5Ð
IÔJÜ	Ø7×>Ñ>ØØß(‰BÐ.Ió	
ôð ¨)ÐBÐBùò+ )ùÓ?s   Â E(Â(E#Â9E(Å#E(c                 óP  € VR ,          .pVR,           F"  pVP                  VR,          V,
          4       K$  	  VRV  Uu. uF  p^WaR,          ,          ,
          NK  	  ppVR ,          .pVR,           F"  pVP                  VR,          V,
          4       K$  	  VRV  Uu. uF  p^WbR,          ,          ,
          NK  	  pp^ RIHp \        \	        \        V4      4      4      p	VP                  W”W—4       VP                  . RO4       VP                  V 4       R# u upi u upi )ÚinitialerrorsÚ
rulescoresNÚ
tokencountr#   )NNNg      ð?)	ÚappendÚmatplotlib.pyplotÚpyplotr1   Úranger4   ÚplotÚaxisÚsavefig)
r:   r{   r|   rO   Ú	testcurveÚ	rulescoreÚxÚ
traincurveÚpltÚrs
   &&&&      r   rc   rc   y  s  € Ø˜?Õ+Ð,€IØ˜|×,Ð,ˆ	Ø×Ñ˜ 2�¨Õ2Ö3ñ -à:CÀEÀTÑ:JÓKÑ:J°Q��Q <Õ0Õ0×0Ð0Ñ:J€IÐKà˜_Õ-Ð.€JØ ×-Ð-ˆ	Ø×Ñ˜* R�.¨9Õ4Ö5ñ .à<FÀuÈÑ<MÓNÑ<M°q�!�a \Õ2Õ2×2Ð2Ñ<M€JÐNå#äŒU”3�y“>Ó"Ó#€AØ‡H�HˆQ˜1Ô)Ø‡H�HÒ$Ô%Ø‡K�KÐ%Ö&ùò Lùò
 Os   ÁDÂ"D#c                 ó<   € \        V 4      \        R  V  4       4      3# )c              3   ó8   "  € T F  p\        V4      x € K  	  R # 5i)N)r4   )Ú.0r    s   & r   Ú	<genexpr>Úcorpus_size.<locals>.<genexpr>ž  s   é € Ð0©4 aœ3˜qŸ6˜6«4ùs   ‚)r4   Úsum)Úseqss   &r   rˆ   rˆ   �  s   € Ü�‹I”sÑ0©4Ó0Ó0Ð1Ð1r   Ú__main__)NNiè  é,  r/   Ngš™™™™™é?r/   Fr   FFNNNr¬   NFN)NN©z^-?[0-9]+(\.[0-9]+)?$ÚCD©z.*ÚNN)	r­   )z(The|the|A|a|An|an)$ÚAT)z.*able$ÚJJ)z.*ness$r°   )z.*ly$ÚRB)z.*s$ÚNNS)z.*ing$ÚVBG)z.*ed$ÚVBDr¯   )#rT   rX   r„   r\   Únltk.corpusr   Únltk.tagr   r   r   rR   r   r   Únltk.tblr	   r
   r   r   r   r   r$   r&   r*   r7   r;   r>   rA   rF   r   rS   rc   ÚNN_CD_TAGGERrQ   rˆ   Ú__name__r   r   r   Ú<module>r¼      s®   ðó 
Û Û Û å  ß DÑ Dß $ß )òòòò!ò4ò;ò	8òMò(
ò&ò*ò7ôBQòJ%CôP'ñ& Ð=¸}ÐMÓN€áò
ó€ò2ð ˆzÔÙÖñ r   