siznax / wptools

Wikipedia tools (for Humans): easily extract data from Wikipedia, Wikidata, and other MediaWikis
MIT License
570 stars 79 forks source link

Only getting template value when Infobox contains template and other wikitext #109

Closed TheoPortmann closed 6 years ago

TheoPortmann commented 6 years ago

With some pages, french one, some fields of the infobox don't have the data shown in wikipedia but instead the {{,}}

In infobox part of page.data :

>>> import wptools
>>> page = wptools.page("Orléans-cléry",lang="fr")
>>> page.get_parse()
fr.wikipedia.org (parse) Orléans-cléry
...
>>> page.data["infobox"]
...
 'cépages': '{{,}}',
...

In parsetree :

>>> page.data["parsetree"]
...
cépages           </name><equals>=</equals><value> [[cabernet franc]] N<ext><name>ref</name><attr> group=&quot;Notes&quot; name=&quot;cep&quot;</attr><inner>Le code international d\'écriture des cépages mentionne la couleur du raisin de la manière suivante : B = blanc, N = noir, Rs = rose, G = gris.</inner><close>&lt;/ref&gt;</close></ext><template><title>,</title></template><ext><name>ref</name><attr> name=&quot;Légifrance2&quot;</attr><inner>{{pdf}} {{lien web|url=http://agriculture.gouv.fr/IMG/pdf/AOC_SOMM44.pdf |titre=Cahier des charges de l\'appellation |site=http://agriculture.gouv.fr/ }}, homologué par le {{article|titre={{Légifrance | base=JORF | numéro=AGRT1114927D | texte=décret {{n°}}2011-1399 du 26 octobre 2011 }} relatif à l\'appellation d\'origine contrôlée « Orléans-Cléry » |périodique=JORF |lien périodique=Journal officiel de la République française |numéro=0252 |jour=29 |mois=octobre |année=2011 |passage=18279}}.</inner><close>&lt;/ref&gt;</close></ext>\n </value></part>
...

I don't get the value [[cabernet franc]]

siznax commented 6 years ago

Thanks for trying wptools, @TheoPortmann!

I see what's going on here. That value also contains the wikitext template: {{,}}. Currently, if an infobox value contains a template, we return the wikitext for it, which is why you get "'cépages' = '{{,}}'".

Like #91, this French Infobox wikitext is unusually complex. French Wikipedia desperately needs Infobox parity in Wikidata. I wonder how much time people spend trying to parse wikitext like this? And, I guess wikitext like this is a major obstacle to getting the data it represents into Wikidata.

Anyway, I think we can give a better result.

siznax commented 6 years ago

I'm adding a new approach to extracting values from the parsetree called "iter" in the comparison below. I think it is an improvement over the previous approach called "find" here. This new approach (iter) has the benefit of picking up TAIL text, as well as getting MORE templates and links mixed with <ext> tags (references, etc.).

The difference between the approaches is that wptools.utils.template_to_dict_iter() uses etree.iter() to iterate over elements, accumulating tail text in order, while wptools.utils.template_to_dict_find() uses etree.xpath() to "lookup" or find specific elements.

The drawback to "iter" is that it does not preserve <ext> tags. I think excluding them is better because it focuses the result on the data.

You can still get the deprecated "find" result by calling template_to_dict(tree, find=True). That may help people wanting to use this tool to update Wikidata with references. We can expose it later as an option to wptools.page.parse() if someone wants it.

nom =
  find: Orléans-cléry
  iter: Orléans-cléry

charte =
  find: rouge
  iter: rouge

image =
  find: AOC Orléans-cléry vignoble.JPG
  iter: AOC Orléans-cléry vignoble.JPG

légende =
  find: Vignoble de l'AOC orléans-cléry à [[Mareau-aux-Prés]].
  iter: Vignoble de l'AOC orléans-cléry à [[Mareau-aux-Prés]].

appellations =
  find: orléans-cléry<ext><name>ref</name><attr> group="Notes" name="AO"</attr></ext>
  iter: orléans-cléry

typeappellation =
  find: [[appellation d'origine contrôlée|AOC]]-[[appellation d'origine protégée|AOP]]<ext><name>ref</name><attr> name="inao"</attr><inner>{{Lien web|url=http://www.inao.gouv.fr/public/produits/detailProduit.php?ID_PRODUIT=5301 |titre=Orl&#233;ans-Cl&#233;ry |site=[http://www.inao.gouv.fr www.inao.gouv.fr] |ann&#233;e=10 mars 2011 |&#233;diteur=[[Minist&#232;re de l'agriculture (France)|Minist&#232;re de l'agriculture]]. [[Institut national de l'origine et de la qualit&#233;]] |consult&#233; le=17 mars 2011 }}</inner><close>&lt;/ref&gt;</close></ext>
  iter: [[appellation d'origine contrôlée|AOC]]-[[appellation d'origine protégée|AOP]]

année =
  find: [[2006]]<ext><name>ref</name><attr> name="L&#233;gifrance1"</attr><inner>{{L&#233;gifrance | base=JORF | num&#233;ro=AGRP0602138D | texte=D&#233;cret du 23 novembre 2006 }} relatif &#224; l'appellation d'origine contr&#244;l&#233;e "Orl&#233;ans-Cl&#233;ry". {{Lien web|url=http://www.legifrance.gouv.fr |titre=www.legifrance.gouv.fr |ann&#233;e=2006 |&#233;diteur=Secr&#233;tariat g&#233;n&#233;ral du gouvernement fran&#231;ais |consult&#233; le=17 mars 2011 }}</inner><close>&lt;/ref&gt;</close></ext>
  iter: [[2006]]

pays =
  find: {{France}}
  iter: {{France}}

région-mère =
  find: [[Vignoble de la vallée de la Loire|vallée de la Loire]]
  iter: [[Vignoble de la vallée de la Loire|vallée de la Loire]]

similaire =
  find:
  iter:

sous-régions =
  find: [[Orléanais]]
  iter: [[Orléanais]]

localisation =
  find: [[Loiret (département)|Loiret]]
  iter: [[Loiret (département)|Loiret]]

saison =
  find:
  iter:

climat =
  find: [[Climat tempéré|tempéré]] [[Climat océanique|océanique]] dégradé
  iter: [[Climat tempéré|tempéré]] [[Climat océanique|océanique]] dégradé

jourssoleil =
  find: {{unité|1804|heures}}
  iter: {{unité|1804|heures}} /an

sol =
  find: [[argile|argilo]]-[[silice|siliceux]] et [[Grave (terrain)|gravelo]]-[[silice|siliceux]]
  iter: [[argile|argilo]]-[[silice|siliceux]] et [[Grave (terrain)|gravelo]]-[[silice|siliceux]]

superficietotale =
  find:
  iter:

superficieplantée =
  find: 28 [[hectare]]s<ext><name>ref</name><attr> name="Guide Hachette"</attr><inner>{{ouvrage|langue=fr|pr&#233;nom1=|nom1=Collectif|lien auteur1=|titre=Le guide Hachette des vins 2011 |sous-titre=des vins pour tous les go&#251;t, &#224; tous les prix|num&#233;ro d'&#233;dition=|&#233;diteur=Hachette livre|lien &#233;diteur=|lieu=Paris|mois=ao&#251;t|ann&#233;e=2010|pages totales=1402|passage=1050|isbn=978-2-01-237681-6|consult&#233; le=17 mars 2011}}</inner><close>&lt;/ref&gt;</close></ext>
  iter: 28 [[hectare]]s

exploitations =
  find: 23 viticulteurs et 6 vinificateurs<ext><name>ref</name><attr> name="inao"</attr></ext>
  iter: 23 viticulteurs et 6 vinificateurs

cépages =
  find: {{,}}
  iter: [[cabernet franc]] N {{,}}

vins =
  find: [[Vin rouge|rouges]]
  iter: [[Vin rouge|rouges]]

volproduction =
  find: {{unité|848|hectolitres}}
  iter: {{unité|848|hectolitres}} en [[2009]]

densité =
  find: {{unité|5000|pieds}}
  iter: minimum de {{unité|5000|pieds}} par hectare

rendement =
  find: {{unité|65|hectolitres}}
  iter: maximum 50 à {{unité|65|hectolitres}} par hectare
TheoPortmann commented 6 years ago

That's super nice, you really did a great job.

siznax commented 6 years ago

Thank you. I hope it helps! Adding test cases, then build and release.

siznax commented 6 years ago

This is now available in release v0.4.9 on pypi.

Thanks so much for helping us make this improvement @TheoPortmann!