ó
    ´OÂfF:  ã                   ó6  • S SK r S SKrS SKrS SKrS SKJr  S SKJrJrJ	r	  S SK
JrJr  S SKJrJr  S rS rS rS	 rS
 rS rS rS rS rS rS rS r                   SS jrS rSS jr\" SS/5      r\" / SQ5      r S r!\"S:X  a  \" 5         gg)é    N)Útreebank)ÚBrillTaggerTrainerÚRegexpTaggerÚUnigramTagger)ÚPosÚWord)ÚTemplateÚ
error_listc                  ó   • [        5         g)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                  ó   • [        SS9  g)ú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                  ó   • [        SS9  g)r   Ústrr   Nr   r   r   r   Údemo_str_rule_formatr   $   s   € ô �eÓr   c                  ó   • [        SS9  g)z"
Exemplify Rule.format("verbose")
Úverboser   Nr   r   r   r   Údemo_verbose_rule_formatr   +   s   € ô �iÓ r   c                  ó@   • [        [        [        / SQ5      5      /S9  g)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            	      óV   • [        [        [        S/5      [        SS/5      5      /S9  g)z0
Templates can have more than a single feature.
r   r    r!   r"   N)r   r	   r   r   r   r   r   Údemo_multifeature_templater&   A   s$   € ô ”hœt Q C›y¬#¨r°2¨h«-Ó8Ð9Ó:r   c                  ó   • [        SSS9  g)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                  ó  • [         R                  " / SQSS/SS9n [        R                  " / SQSS/SS9n[        [        R                  " X/SS	95      n[        S
R                  [        U5      5      5        [        USSS9  g)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
)r!   r   é   r,   é   F)Úexcludezero)r    r!   r   r,   T)r,   é   )Úcombinationsz8Generated {} templates for transformation-based learning)r#   r(   r)   N)	r   Úexpandr   Úlistr	   ÚprintÚformatÚlenr   )ÚwordtplsÚtagtplsr#   s      r   Údemo_generated_templatesr8   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                  ó   • [        SSSS9  g)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                  ó   • [        SS9  g)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                  ó   • [        SS9  g)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_taggerrB   |   s   € ô
 ˜LÓ)r   c                  ó   • [        SSSS9  g)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_rulesrH   „   s   € ô
 �T 4°2Ó6r   c           	      óž  • U=(       d    [         nU c  SSKJnJn  U" 5       n [	        XX(U5      u  nnnnU(       a­  [
        R                  R                  U5      (       dP  [        UUS9n[        US5       n[        R                  " UU5        SSS5        [        SR                  U5      5        [        U5       n[        R                  " U5      n[        SU 35        SSS5        O[        UUS9n[        S5        U(       a)  [        S	R                  WR                  U5      5      5        [         R                   " 5       n[#        WXU	S
9n[        S5        UR%                  UX4U5      n[        S[         R                   " 5       U-
  S S35        U(       a  [        SUR                  U5      -  5        US:X  aP  [        S5        ['        UR)                  5       S5       H'  u  nn[        US SUR                  U	5      S 35        M)     U
(       a‡  [        S5        UR+                  UU5      u  n n![        S5        U(       d  [        S5        UR-                  5       n"U(       a  UR/                  U!5        U(       a  [1        UU!U"US9  [        SU 35        O3[        S5        UR3                  U5      n U(       a  UR/                  5         Ubs  [        US5       n#U#R5                  SU-  5        U#R5                  SR7                  [9        UU 5      5      R;                  S5      S-   5        SSS5        [        SU 35        Ub²  UR3                  U5      n [        US5       n[        R                  " UU5        SSS5        [        SU 35        [        U5       n[        R                  " U5      n$SSS5        [        SU 35        UR3                  U5      n%U U%:X  a  [        S 5        g[        S!5        gg! , (       d  f       GNr= f! , (       d  f       GN= f! , (       d  f       Nö= f! , (       d  f       N¹= f! , (       d  f       N’= f)"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).
Nr   )Ú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: %.4fr,   z
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.brillrJ   rK   Ú_demo_prepare_dataÚosÚpathÚexistsr   ÚopenÚpickleÚdumpr3   r4   ÚloadÚaccuracyÚtimer   ÚtrainÚ	enumerateÚrulesÚbatch_tag_incrementalÚtrain_statsÚprint_template_statisticsÚ
_demo_plotÚ	tag_sentsÚwriteÚjoinr
   Úencode)&r#   Útagged_datarE   Ú	max_rulesrG   rF   r_   ÚtraceÚ	randomizer   r(   r)   r>   rA   r;   Úlearning_curve_takeÚbaseline_backoff_taggerr:   Úcache_baseline_taggerrJ   rK   Ú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¼ÐØÑßBñ
 “Iˆ	Ü>PØ˜IÐ2Hó?Ñ;€]�M 9¨lö Ü�w‰w�~‰~Ð3×4Ñ4Ü+ØÐ'>ñˆOô Ð+¨SÔ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ú÷ )Ö(ú÷z %Õ$ú÷ )Õ(ú÷ $Õ#ús=   Á5O8Â:%P
ËAPÍ"P-ÎP>Ï8
PÐ

PÐ
P*Ð-
P;Ð>
Qc           	      ó¼  • U c   [        S5        [        R                  " 5       n Ub  [        U 5      U::  a  [        U 5      nU(       a5  [        R
                  " [        U 5      5        [        R                  " U 5        [        X!-  5      nU S U nXU nU VV	s/ s H  oˆ V	s/ s H  o™S   PM	     sn	PM     n
nn	U(       d  UnO[        U5      S-  nUS U XlS  pk[        U5      u  pÞ[        U
5      u  nn[        U5      u  nn[        SUS SUS S35        [        SUS SUS S35        [        S	R                  UUU(       a  S
OS5      5        XkXz4$ s  sn	f s  sn	nf )Nz%Loading tagged data from treebank... r   r/   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])
r3   r   Útagged_sentsr5   ÚrandomÚseedÚshuffleÚintÚcorpus_sizer4   )rj   r_   rE   rm   r:   Úcutoffrq   rs   ÚsentÚtrt   rr   Ú	bl_cutoffÚ	trainseqsÚtraintokensÚtestseqsÚ
testtokensÚbltrainseqsÚbltraintokenss                      r   rU   rU   Q  sq  € ð
 ÑÜÐ5Ô6Ü×+Ò+Ó-ˆØÑœC Ó,°	Ó9Ü˜Ó$ˆ	ÞÜ�Š”C˜Ó$Ô%Ü�Š�{Ô#Ü�Ñ"Ó#€FØ  Ð(€MØ 9Ð-€IÙ5>Ô?²Y¨T 4Ó(¢4˜a�q”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Â/EÅEc                 óâ  • US   /nUS    H  nUR                  US   U-
  5        M     US U  Vs/ s H  nSXaS   -  -
  PM     nnUS   /nUS    H  nUR                  US   U-
  5        M     US U  Vs/ s H  nSXbS   -  -
  PM     nnSS KJn  [        [	        [        U5      5      5      n	UR                  X”X—5        UR                  / SQ5        UR                  U 5        g s  snf s  snf )NÚinitialerrorsÚ
rulescoresr!   r,   Ú
tokencountr   )NNNg      ð?)	ÚappendÚmatplotlib.pyplotÚpyplotr2   Úranger5   ÚplotÚaxisÚsavefig)
r;   r}   r~   rQ   Ú	testcurveÚ	rulescoreÚxÚ
traincurveÚpltÚrs
             r   re   re   y  s  € Ø˜?Ñ+Ð,€IØ˜|Ô,ˆ	Ø×Ñ˜ 2™¨Ñ2Ö3ñ -à:CÀEÀTÑ:JÓKÒ:J°Q��Q <Ñ0Ñ0Ô0Ñ:J€IÐKà˜_Ñ-Ð.€JØ Ô-ˆ	Ø×Ñ˜* R™.¨9Ñ4Ö5ñ .à<FÀuÈÑ<MÓNÒ<M°q�!�a \Ñ2Ñ2Ô2Ñ<M€JÐNå#äŒU”3�y“>Ó"Ó#€AØ‡H�HˆQ˜1Ô)Ø‡H�HÒ$Ô%Ø‡K�KÐ%Õ&ùò Lùò
 Os   ±C'Á7C,©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¨   c                 ó<   • [        U 5      [        S U  5       5      4$ )Nc              3   ó8   #   • U  H  n[        U5      v •  M     g 7f)N)r5   )Ú.0r¢   s     r   Ú	<genexpr>Úcorpus_size.<locals>.<genexpr>ž  s   é € Ð0ª4 aœ3˜qŸ6˜6ª4ùs   ‚)r5   Úsum)Úseqss    r   rŠ   rŠ   �  s   € Ü�‹I”sÑ0©4Ó0Ó0Ð1Ð1r   Ú__main__)NNiè  é,  r/   Ngš™™™™™é?r/   Fr   FFNNNr¸   NFN)NN)#rV   rZ   r†   r^   Únltk.corpusr   Únltk.tagr   r   r   rT   r   r   Únltk.tblr	   r
   r   r   r   r   r$   r&   r*   r8   r<   r?   rB   rH   r   rU   re   ÚNN_CD_TAGGERrS   rŠ   Ú__name__r   r   r   Ú<module>r¾      sé   ðó 
Û Û Û å  ß DÑ Dß $ß )òòòò!ò4ò;ò	8òMò(
ò&ò*ò7ð ØØØØØØ
Ø
ØØØØØØØØØ Ø Øô'BQòJ%CôP'ñ& Ð=¸}ÐMÓN€áò
ó€ò2ð ˆzÓÙÕð r   