<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://fabienpoulard.info/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Blog d'un jeune (chercheur) entrepreneur en TAL - Tag - nltk</title>
  <link>http://fabienpoulard.info/</link>
  <atom:link href="http://www.fabienpoulard.info/feed/tag/nltk/rss2" rel="self" type="application/rss+xml"/>
  <description></description>
  <language>fr</language>
  <pubDate>Mon, 17 Jun 2013 14:43:17 +0200</pubDate>
  <copyright>© Fabien Poulard</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Travaux Pratique TALN - Contexte syntaxique</title>
    <link>http://fabienpoulard.info/post/2011/11/21/Travaux-Pratique-TALN-Contexte-syntaxique</link>
    <guid isPermaLink="false">urn:md5:2d2b79c3877c8870ffdccd084274c72c</guid>
    <pubDate>Mon, 21 Nov 2011 23:42:00 +0100</pubDate>
    <dc:creator>Fabien Poulard</dc:creator>
        <category>Sciences &amp; Recherche</category>
        <category>corpus</category><category>enseignement</category><category>nltk</category><category>POS tagging</category><category>python</category><category>syntax</category>    
    <description>&lt;p&gt;Les séances précédentes ont été consacrées à l'analyse lexicale et morphologique. Il est temps de se détacher de la dimension lexicale des textes pour tendre vers la dimension syntaxique.&lt;/p&gt;    &lt;h2&gt;Les rôles grammaticaux&lt;/h2&gt;


&lt;p&gt;Tous les mots n'ont pas la même fonction dans la phrase, chaque mot a un rôle grammatical propre qui indique la façon dont il participe à la construction du sens. Ainsi les déterminants (&lt;em&gt;le&lt;/em&gt;, &lt;em&gt;la&lt;/em&gt;, &lt;em&gt;les&lt;/em&gt;, ...) ne jouent pas un rôle aussi crucial que les noms communs (&amp;quot;champion&amp;quot;, &amp;quot;médaille&amp;quot;, &amp;quot;concours&amp;quot;, ...). Ils font parti de l'outillage grammatical qui permet de faire &lt;em&gt;collaborer&lt;/em&gt; les mots afin de produire du sens.&lt;/p&gt;


&lt;p&gt;Dans le cadre de cette séance de TP, je propose de travailler sur les catégories grammaticales suivantes :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;l'article, le déterminant : &lt;em&gt;le&lt;/em&gt;, &lt;em&gt;la&lt;/em&gt;, &lt;em&gt;les&lt;/em&gt;, &lt;em&gt;l&lt;/em&gt;', &lt;em&gt;un&lt;/em&gt;, &lt;em&gt;une&lt;/em&gt;, &lt;em&gt;des&lt;/em&gt;, ...&lt;/li&gt;
&lt;li&gt;le nom : &lt;em&gt;médaille&lt;/em&gt;, &lt;em&gt;champion&lt;/em&gt;, &lt;em&gt;podium&lt;/em&gt;, &lt;em&gt;éléphant&lt;/em&gt;, ...&lt;/li&gt;
&lt;li&gt;l'adjectif : &lt;em&gt;grande&lt;/em&gt;, &lt;em&gt;téméraire&lt;/em&gt;, &lt;em&gt;puissant&lt;/em&gt;, &lt;em&gt;ordonnée&lt;/em&gt;, ...&lt;/li&gt;
&lt;li&gt;le pronom : &lt;em&gt;je&lt;/em&gt;, &lt;em&gt;me&lt;/em&gt;, &lt;em&gt;moi&lt;/em&gt;, &lt;em&gt;se&lt;/em&gt;, ...&lt;/li&gt;
&lt;li&gt;le verbe : &lt;em&gt;manger&lt;/em&gt;, &lt;em&gt;courir&lt;/em&gt;, &lt;em&gt;être&lt;/em&gt;, &lt;em&gt;penser&lt;/em&gt;, ...&lt;/li&gt;
&lt;li&gt;l'adverbe : &lt;em&gt;lentement&lt;/em&gt;, &lt;em&gt;doucement&lt;/em&gt;, &lt;em&gt;rapidement&lt;/em&gt;, ...&lt;/li&gt;
&lt;li&gt;la préposition : &lt;em&gt;à&lt;/em&gt;, &lt;em&gt;en&lt;/em&gt;, &lt;em&gt;sur&lt;/em&gt;, &lt;em&gt;sous&lt;/em&gt;, &lt;em&gt;sans&lt;/em&gt;, &lt;em&gt;avec&lt;/em&gt;, ...&lt;/li&gt;
&lt;li&gt;la conjonction : &lt;em&gt;mais&lt;/em&gt;, &lt;em&gt;ou&lt;/em&gt;, &lt;em&gt;et&lt;/em&gt;, &lt;em&gt;donc&lt;/em&gt;, ...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Les mots de chacune de ces catégories ont une fonction propre dans la phrase : qualifier le nom, faire référence à une personne ou un objet, décrire l'action ou l'état, coordonner des propositions... Si vous voulez en savoir plus, vous pouvez aller &lt;a href=&quot;http://www.scribd.com/doc/2260156/analyse-grammaticale&quot; hreflang=&quot;fr&quot;&gt;lire ceci&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Il est possible de lister de manière exhaustive (ou suffisamment exhaustive) les mots de certaines catégories grammaticales : les déterminants, les pronoms, les conjonctions et dans une certaine mesure les prépositions. Toutefois, il est très difficile de lister les noms, les verbes, les adjectifs ou encore les adverbes ; et ce pour deux raisons : les mots pouvant prendre ces rôles sont très nombreux et il est possible d'en générer une quasi-infinité de nouveaux, par composition notamment.&lt;/p&gt;


&lt;h2&gt;Lister les mots des catégories déterminant, pronom, conjonction et préposition&lt;/h2&gt;


&lt;p&gt;Même si l'ont peut donner une liste quasi-exhaustive des mots qui appartiennent aux catégories grammaticales déterminant, pronom, conjonction et préposition ; il peut être difficile de réaliser ces listes de tête.&lt;/p&gt;


&lt;p&gt;Encore une fois, le corpus peut-être d'un grand secours. En effet, du fait de la combinaison de leur nombre réduit (par comparaison aux noms et verbes par exemple) et de leur rôle primordial dans la phrase, ces mots sont sont parmi les mots les plus fréquents. Un simple comptage d'occurrence permet donc d'en découvrir un certain nombre :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;codecs&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Chargement du corpus prétraité (découpé en mots)&lt;/span&gt;
fh = &lt;span style=&quot;color: #dc143c;&quot;&gt;codecs&lt;/span&gt;.&lt;span style=&quot;color: #008000;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;Discours-Sarkozy-v20111025.preproc.txt&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;r&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
words   = fh.&lt;span style=&quot;color: black;&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;split&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
fh.&lt;span style=&quot;color: black;&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Comptage des mots&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;from&lt;/span&gt; nltk &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; FreqDist
fd = FreqDist&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; w &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; words:
	fd.&lt;span style=&quot;color: black;&quot;&gt;inc&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;w.&lt;span style=&quot;color: black;&quot;&gt;lower&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Affichage du Top 50 avec leur nombre d'occurrence&lt;/span&gt;
fd.&lt;span style=&quot;color: black;&quot;&gt;items&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;:&lt;span style=&quot;color: #ff4500;&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;
 
&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;','&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;111161&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'de'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;83656&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'.'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;76146&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'la'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;57608&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, 
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;l'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;41454&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'et'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;38519&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'le'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;37246&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'les'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;35018&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;,
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'que'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;33457&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\x&lt;/span&gt;e0'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;31767&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'est'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;29416&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'des'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;27233&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;,
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;d'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;26458&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'je'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;21483&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'qui'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;21432&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'en'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;21317&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;,
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'pas'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;20128&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'un'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;19782&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'pour'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;18374&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'il'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;17286&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;,
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'une'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;16512&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'nous'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;16031&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;c'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;15082&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'a'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;14891&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;,
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'dans'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;13862&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'on'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;13849&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'vous'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;13799&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'du'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;13605&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;,
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'ne'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;13502&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'ce'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;13231&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;qu'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;12629&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;n'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;12400&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;,
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'au'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;9723&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'plus'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;9494&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'y'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;7980&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'sur'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;7845&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, 
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'france'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;7687&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'mais'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;7638&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'avec'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;6869&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;':'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;6556&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;,
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;j'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;6408&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'se'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;5870&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;s'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;5809&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'sont'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;5791&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, 
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'par'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;5724&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'cela'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;5536&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'cette'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;5430&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'ont'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;5230&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;,
&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'aux'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;5145&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;, &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'si'&lt;/span&gt;, &lt;span style=&quot;color: #ff4500;&quot;&gt;5009&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Cette approche combinée à notre connaissance de la grammaire française nous permet de constituer assez rapidement la liste des mots de chacune des catégories déterminant, pronom, conjonction et préposition.&lt;/p&gt;


&lt;p&gt;Cette approche n'est toutefois pas satisfaisant pour les &amp;quot;puristes&amp;quot;, notamment car le corpus est loin d'être représentatif de la langue mais également car nous nous limitons ici aux mots et négligeons les locutions.&lt;/p&gt;


&lt;p&gt;Voici néanmoins une proposition de liste construite de cette manière :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Proposition de liste non exhaustive de mots pour les rôles grammaticaux&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# déterminants, pronoms, conjonctions et prépositions&lt;/span&gt;
dico = &lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# ARTICLES / DETERMINANTS&lt;/span&gt;
	&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;DET&amp;quot;&lt;/span&gt;: &lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;le&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;la&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;les&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;l'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;un&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;une&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;des&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;d'&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;du&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;de&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;au&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;aux&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ce&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;cet&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;cette&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ces&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;mon&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;son&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ma&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ta&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sa&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;mes&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ses&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;notre&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;votre&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;leur&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;nos&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;vos&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;leurs&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;aucun&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;aucune&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;aucuns&amp;quot;&lt;/span&gt;, 
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;tel&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;telle&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;tels&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;telles&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;tout&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;toute&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;tous&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;toutes&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;chaque&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;,
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# PRONOM&lt;/span&gt;
	&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;PRO&amp;quot;&lt;/span&gt;: &lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;je&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;tu&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;il&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;elle&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;on&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;nous&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;vous&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ils&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;elles&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;me&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;m'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;moi&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;te&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;t'&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;toi&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;se&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;y&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;le&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;lui&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;soi&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;leur&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;eux&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;lui&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;qui&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;que&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;quoi&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;dont&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;où&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;,
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# CONJONCTION&lt;/span&gt;
	&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;CONJ&amp;quot;&lt;/span&gt;: &lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;mais&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ou&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;et&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;donc&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;or&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ni&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;car&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;que&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;quand&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;comme&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;si&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;lorsque&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;quoique&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;puisque&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;,
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# PREPOSITION&lt;/span&gt;
	&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;PREP&amp;quot;&lt;/span&gt;: &lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;à&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;derrière&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;malgré&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sauf&amp;quot;&lt;/span&gt;,
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;selon&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;avant&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;devant&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sous&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;avec&amp;quot;&lt;/span&gt;, 
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;en&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;par&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sur&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;entre&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;parmi&amp;quot;&lt;/span&gt;, 
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;envers&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;pendant&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;vers&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;dans&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;pour&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;de&amp;quot;&lt;/span&gt;, 
		&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;près&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;depuis&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sans&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;
&lt;span style=&quot;color: black;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;


&lt;h2&gt;Le contexte syntaxique en renfort&lt;/h2&gt;


&lt;p&gt;Les différents mots extraits précédemment vont nous être utiles pour capturer les mots des autres catégories.&lt;/p&gt;


&lt;p&gt;Nous allons fouiller le contexte des mots listés précédemment afin d'identifier des règles qui nous permettraient de trouver automatiquement les catégories grammaticales des autres mots. Pour ce faire nous allons utiliser un outil bien connu des linguistes : le &lt;a href=&quot;http://fr.wikipedia.org/wiki/Concordancier&quot; hreflang=&quot;fr&quot;&gt;concordancier&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Nltk fournit un concordancier qui est certes un peu limité mais bien suffisant pour notre étude :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;t = nltk.&lt;span style=&quot;color: black;&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;words, name=&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;discours_sarkozy&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
t.&lt;span style=&quot;color: black;&quot;&gt;concordance&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;je&amp;quot;&lt;/span&gt;, width=&lt;span style=&quot;color: #ff4500;&quot;&gt;50&lt;/span&gt;, lines=&lt;span style=&quot;color: #ff4500;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;ce qui nous donne le résultat :&lt;/p&gt;

&lt;pre&gt;
Displaying 10 of 21483 matches:
re avec vous ce matin . Je sais qu' il y a plus de
onsulat dans le monde . Je salue tous ces Français
 , c' est sympathique ! Je suis donc venu vous voi
France-Syrie du Sénat . Je suis également venu ave
la Syrie et la France . Je n' oublie pas Bernard B
des parents d' élèves . Je voudrais vous féliciter
État sera à vos côtés . Je trouve cela formidable 
aissera pas tomber . Et je trouve que c' est merve
nt engagés à ce point . Je veux saluer également l
nsi que les autorités . Je salue ici la présence 
&lt;/pre&gt;


&lt;p&gt;L'utilisation du concordancier nous permet de lister un certain nombre de règles qui semblent généralisables :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;le &amp;quot;or&amp;quot; en début de phrase est une conjonction alors que le &amp;quot;or&amp;quot; précédé d'un déterminant est un nom ;&lt;/li&gt;
&lt;li&gt;les déterminants semblent précéder des noms ;&lt;/li&gt;
&lt;li&gt;les pronoms sont en général suivis d'un verbe ou d'un autre pronom&lt;/li&gt;
&lt;li&gt;les adjectifs sont en général précédés d'un verbe ou d'un nom&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Outre ces quelques règles auxquelles il est très certainement possible de trouver des contre-exemples, on peut surtout noter que les mots ne s'acoquinent pas avec n'importe qui. Chaque mot a des accointances privilégiées avec certaines catégories grammaticales (le déterminant avec le nom par exemple, ...). En d'autres termes, il semble exister une structure syntaxique propre à la langue... sa compréhension nous permettrait de faire un pas de plus dans la compréhension du langage par la machine.&lt;/p&gt;


&lt;h2&gt;Algorithme de Brill&lt;/h2&gt;


&lt;h3&gt;Intuition&lt;/h3&gt;


&lt;p&gt;Prenons une phrase du corpus :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'Je'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'sais'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;qu'&amp;quot;&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'il'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'y'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'a'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'plus'&lt;/span&gt;,
 u&lt;span style=&quot;color: #483d8b;&quot;&gt;'de'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'3 000'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'Fran&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\x&lt;/span&gt;e7ais'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'en'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'Syrie'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;','&lt;/span&gt;, 
 u&lt;span style=&quot;color: #483d8b;&quot;&gt;'&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\x&lt;/span&gt;e0'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'Damas'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;','&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\x&lt;/span&gt;e0'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'Alep'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;','&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'o&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\x&lt;/span&gt;f9'&lt;/span&gt;, 
 u&lt;span style=&quot;color: #483d8b;&quot;&gt;'nous'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'avons'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'notre'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'plus'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'ancien'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'consulat'&lt;/span&gt;, 
 u&lt;span style=&quot;color: #483d8b;&quot;&gt;'dans'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'le'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'monde'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'.'&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Un certain nombre des mots qui composent cette phrase sont connus car ils appartiennent aux catégories grammaticales déterminant, pronom, préposition ou conjonction. C'est le cas notamment de &lt;em&gt;Je&lt;/em&gt;, &lt;em&gt;qu&lt;/em&gt;', &lt;em&gt;il&lt;/em&gt;, &lt;em&gt;y&lt;/em&gt;, &lt;em&gt;de&lt;/em&gt;, &lt;em&gt;en&lt;/em&gt;, &lt;em&gt;à&lt;/em&gt;, &lt;em&gt;à&lt;/em&gt;, &lt;em&gt;où&lt;/em&gt;, &lt;em&gt;nous&lt;/em&gt;, &lt;em&gt;notre&lt;/em&gt;, &lt;em&gt;dans&lt;/em&gt; et &lt;em&gt;le&lt;/em&gt;. L'idée est donc d'affecter à ces mots leur étiquette la plus probable et aux autres l'étiquette &lt;em&gt;?&lt;/em&gt;, soit :&lt;/p&gt;

&lt;pre&gt;
[('Je', 'PRO'), ('sais', '?'), (&amp;quot;qu'&amp;quot;, 'PRO'), ('il', 'PRO'),
 ('y', 'PRO'),  ('a', '?'), ('plus', '?'), ('de', 'PREP'), 
 ('3 000', '?'), ('Fran\xe7ais', '?'), ('en', 'PREP'), 
 ('Syrie', '?'), (',', 'PUN'), ('\xe0', 'PREP'), ('Damas', '?'), 
 (',', 'PUN'), ('\xe0', 'PREP'), ('Alep', '?'), (',', 'PUN') , 
 ('o\xf9', 'PREP'), ('nous', 'PRO'), ('avons', '?'), 
 ('notre', 'DET'), ('plus', '?'), ('ancien', '?'), ('consulat', '?'), 
 ('dans', 'PREP'), ('le', 'DET'), ('monde', '?'), ('.', 'PUN')]
&lt;/pre&gt;


&lt;p&gt;L'application de règles simples naïves telles que :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Un pronom est suivi d'un pronom ou d'un verbe ;&lt;/li&gt;
&lt;li&gt;Un déterminant est suivi d'un adjectif ou d'un nom ;&lt;/li&gt;
&lt;li&gt;Une préposition est suivie d'un nom propre, d'un pronom ou d'un déterminant ;&lt;/li&gt;
&lt;li&gt;Un adjectif est suivi d'un adjectif, d'un nom ou d'une ponctuation ;&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;permettent de deviner les catégories grammaticales qui peuvent s'appliquer aux mots inconnus de nos dictionnaires. Nous pourrons ensuite compléter notre dictionnaire de ces nouveaux et ainsi enrichir notre connaissance itérativement.&lt;/p&gt;


&lt;p&gt;C'est &lt;em&gt;grosso modo&lt;/em&gt; le concept de l'&lt;a href=&quot;http://en.wikipedia.org/wiki/Brill_tagger&quot; hreflang=&quot;en&quot;&gt;algorithme de Brill&lt;/a&gt;...&lt;/p&gt;


&lt;h3&gt;Fonctionnement&lt;/h3&gt;


&lt;p&gt;L'algorithme de Brill parcourt les mots du texte. Si la forme textuelle est présente dans son dictionnaire, il lui associe la catégorie grammaticale la plus probable, c-à-d celle la plus communément rencontrée pour ce mot. Lorsque la forme textuelle n'est pas présente dans le dictionnaire, il lui associe l'étiquette &lt;em&gt;NOM&lt;/em&gt; ou &lt;em&gt;NOM PROPRE&lt;/em&gt; si elle commence par une majuscule. Une fois cette première passe opérée, l'algorithme applique des règles de transformation des étiquettes telles que vues précédemment.&lt;/p&gt;


&lt;p&gt;On dit que l'algorithme est guidé par l'erreur (&lt;em&gt;error-driven&lt;/em&gt;) car il fait appel à de l'apprentissage supervisé (sélection des règles).&lt;/p&gt;


&lt;p&gt;Pour en savoir plus, vous pouvez lire cet &lt;a href=&quot;http://dl.acm.org/citation.cfm?id=1075553&quot; hreflang=&quot;en&quot;&gt;article écrit par Brill lui-même&lt;/a&gt;.&lt;/p&gt;


&lt;h2&gt;Brill dans nltk&lt;/h2&gt;


&lt;p&gt;Nltk intègre &lt;a href=&quot;http://nltk.googlecode.com/svn/trunk/doc/api/nltk.tag.brill.BrillTagger-class.html&quot; hreflang=&quot;en&quot;&gt;une implémentation de l'algorithme de Brill&lt;/a&gt;, ainsi que l'&lt;a href=&quot;http://nltk.googlecode.com/svn/trunk/doc/api/nltk.tag.brill.BrillTaggerTrainer-class.html&quot; hreflang=&quot;en&quot;&gt;outillage nécessaire à l'entraînement d'un nouvel étiqueteur&lt;/a&gt;.&lt;/p&gt;</description>
    
    
    
      </item>
    
  <item>
    <title>Travaux Pratique TALN - Morphologie et contexte syntaxique</title>
    <link>http://fabienpoulard.info/post/2011/10/25/Travaux-Pratique-TALN-Le-lexique</link>
    <guid isPermaLink="false">urn:md5:d4fe421dfe957ecc07e14fdea0fa7a6c</guid>
    <pubDate>Mon, 14 Nov 2011 18:55:00 +0100</pubDate>
    <dc:creator>Fabien Poulard</dc:creator>
        <category>Sciences &amp; Recherche</category>
        <category>analyse lexicale</category><category>analyse morphologique</category><category>corpus</category><category>enseignement</category><category>nltk</category><category>POS tagging</category><category>python</category><category>regexp</category>    
    <description>&lt;p&gt;Dans &lt;a href=&quot;http://fabienpoulard.info/post/2011/10/24/Travaux-Pratique-TALN-Le-lexique&quot;&gt;le TP précédent&lt;/a&gt; nous nous sommes limité à l'analyse des mots en-dehors de tout contexte. Il s'est alors agi de découper un texte en mots puis de compter ces derniers.&lt;/p&gt;


&lt;p&gt;Dans ce second TP, nous allons nous intéresser à la morphologie des mots (leur forme textuelle). Nous explorerons notamment deux procédés de normalisation morphologique : la racinisation et la lemmatisation. Nous découvrirons ensuite que le contexte des mots, combiné à leur morphologie, peut nous apprendre bien des choses.&lt;/p&gt;    &lt;p&gt;Je considère comme acquis le découpage en phrases et en mots (si ce n'est pas le cas, vous pouvez vous rafraîchir la mémoire &lt;a href=&quot;http://fabienpoulard.info/post/2011/10/24/Travaux-Pratique-TALN-Le-lexique&quot;&gt;ici&lt;/a&gt;). Nous allons dans cette partie directement travailler sur &lt;a href=&quot;http://fabienpoulard.info/download/Recherche/Corpus/Discours-Sarkozy-v20111025.preproc.txt.gz&quot; hreflang=&quot;fr&quot; title=&quot;Corpus des discours de N. Sarkozy après découpage en unités linguistiques&quot;&gt;une version du corpus des discours de N. Sarkozy déjà découpée en phrases et en mots&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;La fonction ci-dessous permet de charger la liste des phrases du corpus et pour chaque phrase la liste des mots qui la compose :&lt;/p&gt;
&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; load_preprocessed_corpus&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;finput=&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;Discours-Sarkozy-v20111025.preproc.txt&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
	&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;
	Load the content of a preprocessed file as a list of sentences which happen
	to be a list of words.
	&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
	fh = &lt;span style=&quot;color: #dc143c;&quot;&gt;codecs&lt;/span&gt;.&lt;span style=&quot;color: #008000;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;finput, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;r&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	corpus = fh.&lt;span style=&quot;color: black;&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	fh.&lt;span style=&quot;color: black;&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	sents = &lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; sent &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; corpus.&lt;span style=&quot;color: black;&quot;&gt;split&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
		sents.&lt;span style=&quot;color: black;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt; sent.&lt;span style=&quot;color: black;&quot;&gt;split&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; sents&lt;/pre&gt;


&lt;p&gt;Exemple d'utilisation :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; sentences = load_preprocessed_corpus&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;Discours-Sarkozy-v20111025.preproc.txt&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;Nombre de phrases  : %d&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;sentences&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #483d8b;&quot;&gt;'Nombre de phrases  : 84325'&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;Nombre de mots : %d&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;w &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; s &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; sentences &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; w &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; s&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #483d8b;&quot;&gt;'Nombre de mots : 2017309'&lt;/span&gt;&lt;/pre&gt;


&lt;h2&gt;Morphologie : des expressions rationnelles à l'algorithme de Porter&lt;/h2&gt;


&lt;p&gt;Nous allons avoir besoin d'un outil puissant pour exprimer par intention les formes textuelles que nous allons manipuler : les &lt;a href=&quot;http://fr.wikipedia.org/wiki/Expression_rationnelle&quot; hreflang=&quot;fr&quot;&gt;expressions rationnelles&lt;/a&gt;. Celles-ci permettent de décrire un langage régulier pour lequel il sera possible de générer un automate acceptant. La plupart des langages informatiques récents offrent des bibliothèques permettant de manipuler  ces expressions et les automates en découlant. &lt;a href=&quot;http://docs.python.org/howto/regex.html#regex-howto&quot; hreflang=&quot;en&quot;&gt;Python ne fait pas exception à la règle&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Les expressions rationnelles sont gérés sous Python par le module &lt;em&gt;re&lt;/em&gt;. Il serait trop long de présenter l'utilisation des expressions rationnelles avec Python ici, et &lt;a href=&quot;http://diveintopython.adrahon.org/regular_expressions/index.html&quot; hreflang=&quot;fr&quot;&gt;d'autres le font beaucoup mieux que moi&lt;/a&gt;.&lt;/p&gt;


&lt;h3&gt;France, Français, Française, Françaises...&lt;/h3&gt;


&lt;p&gt;Le &lt;a href=&quot;http://fabienpoulard.info/public/2011/TP-TALN/nuage-mots-sarkozy.png&quot;&gt;nuage de mots&lt;/a&gt; généré &lt;a href=&quot;http://fabienpoulard.info/post/2011/10/24/Travaux-Pratique-TALN-Le-lexique&quot;&gt;précédemment&lt;/a&gt; contient des mots très similaires, c'est le cas notamment de &lt;em&gt;français&lt;/em&gt; et &lt;em&gt;française&lt;/em&gt;. Les expressions rationnelles peuvent nous permettre de les considérer ensemble :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;re&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;re&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;findall&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;bfrançais&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;b&amp;quot;&lt;/span&gt;, text&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #ff4500;&quot;&gt;1031&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;re&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;findall&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;bfrançaise&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;b&amp;quot;&lt;/span&gt;, text&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #ff4500;&quot;&gt;1286&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;re&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;findall&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;bfran[cç]ais&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;w&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;b&amp;quot;&lt;/span&gt;, text&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #ff4500;&quot;&gt;2661&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; tous_francais = &lt;span style=&quot;color: #008000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; m &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;re&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;finditer&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;bfran[cç]ais&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;w*&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\\&lt;/span&gt;b&amp;quot;&lt;/span&gt;, text&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
...     &lt;span style=&quot;color: black;&quot;&gt;tous_francais&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt; m.&lt;span style=&quot;color: black;&quot;&gt;group&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; tous_francais
&lt;span style=&quot;color: #008000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'fran&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\x&lt;/span&gt;e7aise'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'fran&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\x&lt;/span&gt;e7ais'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'fran&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\x&lt;/span&gt;e7aises'&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h3&gt;Généralisation : vers l'algorithme de Porter&lt;/h3&gt;


&lt;p&gt;L'exemple précédent nous montre que nous pourrions profiter d'une normalisation morphologique des mots collectés  afin de rendre mieux compte de la distribution lexicale. Après tout &lt;em&gt;français&lt;/em&gt;, &lt;em&gt;française&lt;/em&gt; et &lt;em&gt;françaises&lt;/em&gt; correspondent tous trois au même mot &lt;em&gt;français&lt;/em&gt; tel qu'on le trouve dans &lt;a href=&quot;http://atilf.atilf.fr/dendien/scripts/tlfiv5/visusel.exe?16;s=2523494280;r=1;nat=;sol=9;&quot; hreflang=&quot;fr&quot;&gt;le dictionnaire&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;En effet, &lt;em&gt;française&lt;/em&gt; et &lt;em&gt;françaises&lt;/em&gt; sont des flexions de l'adjectif &lt;em&gt;français&lt;/em&gt;. En linguistique, on oppose flexions et dérivations :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;La flexion consiste à ajouter un affixe au mot afin de refléter un changement au niveau grammatical, du genre, du nombre, de la personne, etc., sans que cette modification morphologique n'altère le sens du lexème d'origine ;&lt;/li&gt;
&lt;li&gt;La dérivation consiste à créer un nouveau lexème, c-à-d un nouveau mot avec un nouveau sens, par l'ajout d'un affixe.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Les règles flexionnelles en français semblent assez régulières : ajout d'un &amp;quot;e&amp;quot; pour marquer le féminin, d'un &amp;quot;s&amp;quot; pour le pluriel... Ne pourrait-on pas définir un algorithme capable de déconstruire ces flexions afin de retrouver le lexème d'origine, au masculin singulier ?&lt;/p&gt;


&lt;p&gt;Petit exercice :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Tentez de produire une liste exhaustive des suffixes utilisés pour marquer le genre et le nombre en français&lt;/li&gt;
&lt;li&gt;Recherchez toutes les formes textuelles qui utilisent ces suffixes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Proposition de corrigé :&lt;/p&gt;
&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;all_words = &lt;span style=&quot;color: #008000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;w.&lt;span style=&quot;color: black;&quot;&gt;lower&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; w &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; words&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
 
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; evaluation_suffix&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;suffix&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;global&lt;/span&gt; raw_text
	regexp = &lt;span style=&quot;color: #dc143c;&quot;&gt;re&lt;/span&gt;.&lt;span style=&quot;color: #008000;&quot;&gt;compile&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;r&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\b&lt;/span&gt;(&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\w&lt;/span&gt;+)%s&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\b&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt;suffix, &lt;span style=&quot;color: #dc143c;&quot;&gt;re&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;I&lt;/span&gt;|re.&lt;span style=&quot;color: black;&quot;&gt;U&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	&lt;span style=&quot;color: #008000;&quot;&gt;map&lt;/span&gt; = &lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;}&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Collecter les mots avec le suffixe&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; m &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; regexp.&lt;span style=&quot;color: black;&quot;&gt;finditer&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;raw_text&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
		avec_suffixe = m.&lt;span style=&quot;color: black;&quot;&gt;group&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
		sans_suffixe = m.&lt;span style=&quot;color: black;&quot;&gt;group&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
		&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;map&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;has_key&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;avec_suffixe&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
			&lt;span style=&quot;color: #008000;&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;avec_suffixe&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt; = &lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;
				&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sans_suffixe&amp;quot;&lt;/span&gt;: sans_suffixe,
				&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sans_suffixe_existe&amp;quot;&lt;/span&gt;: &lt;span style=&quot;color: #008000;&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;}&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Chercher les occurrences sans suffixe&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; k &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;map&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
		sans_suffixe = &lt;span style=&quot;color: #008000;&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;k&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sans_suffixe&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;lower&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
		&lt;span style=&quot;color: #008000;&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;k&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sans_suffixe_existe&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt; = &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;sans_suffixe &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; all_words&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Rapport&lt;/span&gt;
	nb_est_suffixe = &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;k &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; k &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;map&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;k&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sans_suffixe_existe&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	nb_est_pas_suffixe = &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt; - nb_est_suffixe
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; nb_est_suffixe &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&lt;/span&gt; nb_est_pas_suffixe:
		&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;%s est un suffixe (%d/%d)&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;suffix, nb_est_suffixe, nb_est_pas_suffixe&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;else&lt;/span&gt;:
		&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;%s n'est PAS un suffixe (%d/%d)&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;suffix, nb_est_suffixe, nb_est_pas_suffixe&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
 
suffixes = &lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ous&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;aux&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;s&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;e&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ais&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ives&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ent&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;es&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ai&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ons&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ez&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;t&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ait&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ions&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;iez&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;aient&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ant&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;le&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;les&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ne&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;nes&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;
 
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; suffix &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; suffixes:
	evaluation_suffix&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;suffix&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h3&gt;Algorithme de Porter&lt;/h3&gt;


&lt;p&gt;L'algorithme de Porter offre un cadre algorithmique pour la désuffixation des mots. Le fonctionnement de l'algorithme est décrit en de nombreux endroits sur le Web, notamment &lt;a href=&quot;http://fabienpoulard.info/post/2011/10/25/www-igm.univ-mlv.fr/~lecroq/cours/porter.pdf&quot; hreflang=&quot;fr&quot; title=&quot;Présentation de l'algorithme de Porter par Thierry Lecroq de l'Université de Rouen&quot;&gt;ici&lt;/a&gt; ou &lt;a href=&quot;http://fabienpoulard.info/post/2008/02/21/Lalgorithme-de-Porter&quot; hreflang=&quot;fr&quot; title=&quot;Présentation de l'algorithme de Porter par Fabien Poulard&quot;&gt;sur mon propre blog&lt;/a&gt;. S'il a été mis au point pour l'anglais, il est tout à fait envisageable de l'utiliser pour le français (&lt;a href=&quot;http://snowball.tartarus.org/&quot; hreflang=&quot;en&quot; title=&quot;Implémentation multilingue de l'algorithme de Porter&quot;&gt;d'ailleurs certains l'ont fait&lt;/a&gt;).&lt;/p&gt;


&lt;p&gt;L'idée de l'algorithme de Porter est de déconstruire les flexions pour retrouver la forme canonique des mots : le lemme. Par exemple : retirer le suffixe &amp;quot;aux&amp;quot; de &lt;em&gt;chevaux&lt;/em&gt; et le remplacer par &amp;quot;al&amp;quot; pour obtenir le singulier. Les règles de Porter sont déclenchées lorsque deux conditions sont réunies :&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;le mot se termine par un suffixe particulier&lt;/li&gt;
&lt;li&gt;le radical (mot privé dudit suffixe) respecte un motif particulier, le plus généralement un certain nombre de pseudo-syllabes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Le code python ci-dessous est une proposition d'implémentation de la règle de Porter pour normaliser &lt;em&gt;journaux&lt;/em&gt; et &lt;em&gt;chevaux&lt;/em&gt; en respectivement &lt;em&gt;journal&lt;/em&gt; et &lt;em&gt;cheval&lt;/em&gt; :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; compute_m&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;w&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
     pseudosyllabs = &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;
     &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; c &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; w:
             &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; c &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;'a'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'e'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'i'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'o'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'u'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'y'&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;:
                     &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;pseudosyllabs&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;==&lt;span style=&quot;color: #ff4500;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;or&lt;/span&gt; pseudosyllabs&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;-&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;==&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;C&amp;quot;&lt;/span&gt;:
                             pseudosyllabs += &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;V&amp;quot;&lt;/span&gt;
             &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;else&lt;/span&gt;:
                     &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;pseudosyllabs&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;==&lt;span style=&quot;color: #ff4500;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;or&lt;/span&gt; pseudosyllabs&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;-&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;==&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;V&amp;quot;&lt;/span&gt;:
                             pseudosyllabs += &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;C&amp;quot;&lt;/span&gt;
     &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; pseudosyllabs.&lt;span style=&quot;color: black;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;VC&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
 
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Règle de Porter : (m=1 &amp;amp;&amp;amp; *c) aux -&amp;gt; al&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Le mot se termine par aux, a un radical composé d'une pseudo-syllabe et qui se termine par une consomne&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; regle_porter_aux&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;w&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
     &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; w.&lt;span style=&quot;color: black;&quot;&gt;endswith&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;aux&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
          radical = w&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;:-&lt;span style=&quot;color: #ff4500;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;
          &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;not&lt;/span&gt; radical&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;-&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;'a'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'e'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'i'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'o'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'u'&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;'y'&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;:
               &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; radical + &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;al&amp;quot;&lt;/span&gt;
     &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; w
 
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; regle_porter_aux&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;journaux&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #483d8b;&quot;&gt;'journal'&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; regle_porter_aux&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;chevaux&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #483d8b;&quot;&gt;'cheval'&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; regle_porter_aux&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;agneaux&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #483d8b;&quot;&gt;'agneaux'&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Petit exercice :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Écrire d'autres règles pour le français dans le formalisme proposé par Porter&lt;/li&gt;
&lt;li&gt;À partir du &lt;a href=&quot;http://atoll.inria.fr/~sagot/lefff.html&quot; hreflang=&quot;fr&quot;&gt;travail de compilation des formes flexionnelles du français par B. Sagot&lt;/a&gt;, pouvez-vous inférer automatiquement toutes les règles de Porter pour le français ?&lt;/li&gt;
&lt;/ul&gt;</description>
    
    
    
      </item>
    
  <item>
    <title>Travaux Pratique TALN - Le lexique</title>
    <link>http://fabienpoulard.info/post/2011/10/24/Travaux-Pratique-TALN-Le-lexique</link>
    <guid isPermaLink="false">urn:md5:aa022e2b9c0d0f2ff31a5820a3b04ee4</guid>
    <pubDate>Mon, 24 Oct 2011 22:38:00 +0200</pubDate>
    <dc:creator>Fabien Poulard</dc:creator>
        <category>Sciences &amp; Recherche</category>
        <category>analyse lexicale</category><category>corpus</category><category>enseignement</category><category>nltk</category><category>python</category><category>scikit-learn</category>    
    <description>&lt;p&gt;Malgré &lt;a href=&quot;http://www.dictanova.com&quot; hreflang=&quot;fr&quot; title=&quot;Page de présentation de Dictanova&quot;&gt;notre projet de création d'entreprise&lt;/a&gt;, je tenais à continuer à enseigner le TALN à l'Université. Outre l'intérêt pragmatique du chef d'entreprise qui souhaite ainsi repérer les éléments prometteurs à recruter, l'enseignement est une des meilleures manières de prendre du recul sur un domaine.&lt;/p&gt;


&lt;p&gt;Cette année je compte mettre de côté UIMA pour me concentrer sur l'expérimentation. J'ai donc décidé de me tourner vers Python, mon langage de cœur, et les bibliothèques &lt;a href=&quot;http://www.nltk.org/&quot; hreflang=&quot;en&quot;&gt;NLTK&lt;/a&gt; et &lt;a href=&quot;http://scikit-learn.sourceforge.net/stable/&quot; hreflang=&quot;en&quot;&gt;scikit-learn&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Pour ce premier TP, je compte faire réfléchir les étudiants sur l'analyse lexicale : découpage d'un texte en mots, calculer une distribution sur un document, puis un corpus, filtrer les mots qui participent peu à l'expression du sens et visualiser un texte à partir de son lexique.&lt;/p&gt;    &lt;h2&gt;Constitution du corpus&lt;/h2&gt;


&lt;p&gt;Si la construction du corpus est toujours primordiale, celle-ci revêt un enjeu d'autant plus stratégique en enseignement que le corpus peut captiver ou repousser les étudiants.&lt;/p&gt;


&lt;p&gt;J'ai un temps été tenté par les messages de condoléances laissés par les fans d'Apple à l'occasion de la mort de Steve Jobs. Neil Kodner a réalisé &lt;a href=&quot;http://www.neilkodner.com/2011/10/an-analysis-of-steve-jobs-tribute-messages-displayed-by-apple/&quot; hreflang=&quot;en&quot;&gt;une superbe analyse sur le sujet&lt;/a&gt;. Toutefois, je préfère faire travailler mes étudiants sur le français qui a des propriétés particulières qu'on ne retrouve pas en anglais, notamment sa forte flexionnalité.&lt;/p&gt;


&lt;p&gt;À l'occasion du lancement par Google d'un concours de visualisation de données sur la thématique des élections présidentielles 2012, je me suis une nouvelle fois tourné vers les discours politiques. Ceux-ci sont souvent chargés de symboles qui s'expriment souvent par le lexique, d'où l'intérêt de les utiliser dans ce genre d'étude. Bien sûr l'objectif n'est pas de chercher à atteindre la qualité des études menées avec brillo par &lt;a href=&quot;http://blog.veronis.fr/&quot; hreflang=&quot;fr&quot;&gt;Jean Véronis&lt;/a&gt;, mais simplement se faire la main sur quelques textes contemporains d'intérêts.&lt;/p&gt;


&lt;p&gt;Pour la constitution du corpus, je me suis simplement tourné vers &lt;a href=&quot;http://www.elysee.fr/&quot; hreflang=&quot;fr&quot;&gt;le site de l'Élysée&lt;/a&gt;. J'avais commencé à &lt;a href=&quot;http://fabienpoulard.info/post/2010/03/13/Corpus-des-discours-de-Nicolas-Sarkozy-%28M%C3%80J%29&quot;&gt;collecter les différents discours de notre président&lt;/a&gt;, mais une mise-à-jour du site (fortement inspiré du &lt;a href=&quot;http://www.whitehouse.gov/&quot; hreflang=&quot;en&quot;&gt;site de la Maison Blanche&lt;/a&gt;) m'avait coupé dans mon élan. Le point positif est que ce nouveau site met l'accent sur l'accessibilité. J'ai écrit un petit script Python qui permet d'aller récupérer tous les discours. Il est un peu simpliste et ramène donc un peu de bruit, mais suffisamment peu pour que le résultat soit exploitable :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/env python&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# -*- coding: utf-8 -*-&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Scrap the speeches from M. President Sarkozy :)&lt;/span&gt;
 
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;codecs&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;urllib2&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;from&lt;/span&gt; BeautifulSoup &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; BeautifulSoup
 
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; did &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;12300&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
	&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;stdout&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;write&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;Dealing with page %d...&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt;did&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;try&lt;/span&gt;:
		&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Collect the page&lt;/span&gt;
		uh    = &lt;span style=&quot;color: #dc143c;&quot;&gt;urllib2&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;urlopen&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;http://www.elysee.fr/president/root/bank/print/%d.htm&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; did&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
		html  = uh.&lt;span style=&quot;color: black;&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
		uh.&lt;span style=&quot;color: black;&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
		&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Check it is a discourse (discourse subcategory)&lt;/span&gt;
		&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; html.&lt;span style=&quot;color: black;&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;/president/root/core/D00018&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&lt;/span&gt;= &lt;span style=&quot;color: #ff4500;&quot;&gt;0&lt;/span&gt;:
			&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Extract the text&lt;/span&gt;
			soup  = BeautifulSoup&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;html&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
			zone  = soup.&lt;span style=&quot;color: black;&quot;&gt;body&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;div&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #008000;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;zonePrincipale&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
			texts = &lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;t.&lt;span style=&quot;color: black;&quot;&gt;strip&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; t &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; zone.&lt;span style=&quot;color: black;&quot;&gt;findAll&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;text=&lt;span style=&quot;color: #008000;&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;t.&lt;span style=&quot;color: black;&quot;&gt;strip&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;
			&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Export it&lt;/span&gt;
			fh = &lt;span style=&quot;color: #dc143c;&quot;&gt;codecs&lt;/span&gt;.&lt;span style=&quot;color: #008000;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;%d.txt&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt;did, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;w&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
			&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; t &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; texts:
				fh.&lt;span style=&quot;color: black;&quot;&gt;write&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;t + &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
			fh.&lt;span style=&quot;color: black;&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
			&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;stdout&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;write&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;done&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
		&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;else&lt;/span&gt;:
			&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;stdout&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;write&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;ignored&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;except&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;Exception&lt;/span&gt;:
		&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;stdout&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;write&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;error&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;À l'heure de l'écriture de ce billet, le script permet de collecter 588 discours (ou pages identifiées comme tels) pour un total de 1943116 mots. L'export de l'extraction peut-être téléchargé &lt;a href=&quot;http://fabienpoulard.info/download/Recherche/Corpus/Discours-Sarkozy-v20111025.tar.gz&quot; hreflang=&quot;fr&quot;&gt;ici&lt;/a&gt;.&lt;/p&gt;


&lt;h2&gt;Découpage en mots&lt;/h2&gt;


&lt;p&gt;J'ai déjà discuté plusieurs fois du découpage en mots sur ce blog : avec &lt;a href=&quot;http://fabienpoulard.info/post/2008/03/05/Tokenisation-en-mots-avec-NLTK&quot;&gt;NLTK&lt;/a&gt; ou de &lt;a href=&quot;http://fabienpoulard.info/post/2010/09/08/Un-rapide-tokeniseur-en-mots-pour-le-fran%C3%A7ais&quot;&gt;manière plus théorique&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Je vois trois façons d'approcher le problème du découpage en mots :&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Rechercher les sous-chaînes qui constituent les mots ;&lt;/li&gt;
&lt;li&gt;Rechercher les sous-chaînes qui séparent les mots ;&lt;/li&gt;
&lt;li&gt;Identifier les frontières entre les mots et les non-mots.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;La première approche est celle qui a été auparavant discutée sur ce blog. L'idée générale est qu'un mot (ou plus précisément un &lt;a href=&quot;http://ldelafosse.pagesperso-orange.fr/Glossaire/L.htm#lexeme&quot; hreflang=&quot;fr&quot;&gt;lexème&lt;/a&gt;) est une séquence continue de lettres ou de chiffres. Les choses se compliquent lorsque l'on considère les cas particuliers : les articles et pronoms contractés (l', d', j', m', ...) ; les composés lexicaux à apostrophe (aujourd'hui, ...) ; les composés lexicaux à traits d'union (arc-en-ciel, peut-être, sauve-qui-peut, ...) ; les valeurs numériques (14 000, 14,18, 30 %, ...) ; les acronymes (ASSEDIC, ASCII, ...) ; les sigles (C-4, c-à-d, i.e., ...) ou encore les unités de mesure (A/m, km/h, ...).&lt;/p&gt;


&lt;p&gt;La seconde approche me semble biaisée par nature. Elle fonctionne suffisamment bien pour l'anglais où l'on peut envisager découper un texte en mots en coupant à l'endroit des espaces. Elle est toutefois inefficace pour le français étant donné le nombre de cas où les mots sont séparés par une chaîne vide (présence d'un apostrophe, ponctuation, ...).&lt;/p&gt;


&lt;p&gt;La dernière approche serait celle que je mettrais en œuvre si je devais implémenter un algorithme de découpage en mot par &lt;a href=&quot;http://fr.wikipedia.org/wiki/Apprentissage_automatique&quot; hreflang=&quot;fr&quot;&gt;apprentissage&lt;/a&gt;. L'idée serait alors de classer chaque caractère dans une des catégories : début de mot, fin de mot ou n'appartenant pas à un mot. Je ne connais pas l'état de l'art des techniques de tokenisation automatique, mais une approche par n-grammes caractères devrait assez bien fonctionner. Il faudrait toutefois étudier lequel du contexte gauche ou droit est le plus important (si l'un des deux est plus important que l'autre).&lt;/p&gt;


&lt;p&gt;Pour l'exercice nous utiliserons le &lt;a href=&quot;http://nltk.googlecode.com/svn/trunk/doc/api/nltk.tokenize.regexp.RegexpTokenizer-class.html&quot; hreflang=&quot;en&quot;&gt;RegexpTokenizer&lt;/a&gt; de nltk avec l'expression régulière suivante (à améliorer éventuellement) :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;re&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;from&lt;/span&gt; nltk.&lt;span style=&quot;color: #dc143c;&quot;&gt;tokenize&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;regexp&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; RegexpTokenizer
 
reg_words = r&lt;span style=&quot;color: #483d8b;&quot;&gt;''&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;'(?x)
          aujourd'&lt;/span&gt;hui    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# exception 1&lt;/span&gt;
        | prud&lt;span style=&quot;color: #483d8b;&quot;&gt;'hom&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\w&lt;/span&gt;+ # exception 2
        | &lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\d&lt;/span&gt;+(,&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\d&lt;/span&gt;+)?&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\s&lt;/span&gt;*[%€$] # les valeurs
        | &lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\d&lt;/span&gt;+                # les nombres
        | &lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\w&lt;/span&gt;'&lt;/span&gt;                 &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# les contractions d', l', j', t', s'&lt;/span&gt;
        | \w+&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;-\w+&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;+    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# les mots composés&lt;/span&gt;
        | &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;\d|\w&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;+         &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# les combinaisons alphanumériques&lt;/span&gt;
        | \w+               &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# les mots simples&lt;/span&gt;
        &lt;span style=&quot;color: #483d8b;&quot;&gt;''&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;'
tokenizer = RegexpTokenizer(reg_words, flags=re.UNICODE|re.IGNORECASE)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;L'utilisation du RegexpTokenizer est des plus simple, il suffit d'appeler la méthode &lt;em&gt;tokenize&lt;/em&gt; sur un texte pour obtenir en retour la liste des mots extraits dudit texte :&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; tokenizer.tokenize(text)

[u'Hommage', u'\xe0', u'M', u'Aim\xe9', u'C\xc9SAIRE', u'ALLOCUTION', u'DE', u'M', u'LE', u'PR\xc9SIDENT', u'DE', u'LA', 
u'R\xc9PUBLIQUE', u'FRAN\xc7AISE', u'A\xe9roport', u'de', u'Fort-de-France', u'Martinique', u'Dimanche', u'20', u'Avril', u'2008', u&amp;quot;C'&amp;quot;, 
u'est', u'avec', u'une', u'profonde', u'\xe9motion', u'que', u'je', u'viens', u&amp;quot;aujourd'hui&amp;quot;, u'rendre', u&amp;quot;l'&amp;quot;, u'hommage', u'de', u'la', 
u'Nation', u'\xe0', u'Aim\xe9', u'C\xc9SAIRE', u'qui', u'nous', u'a', u'quitt\xe9s', u'jeudi', u'dernier', u'Ma', u'place', u'ne', u'pouvait', 
u'\xeatre', u&amp;quot;aujourd'hui&amp;quot;, u'que', u'sur', u'cette', u'terre', u'de', u'Martinique', u'aux', u'c\xf4t\xe9s', u'de', u'ceux', u'qui', u'sont', 
u'dans', u'la', u'peine', u'Mes', u'premi\xe8res', u'pens\xe9es', u'vont', u'naturellement', u'\xe0', u'la', u'famille', u'endeuill\xe9e', 
u'qu', u'avec', u'les', u'ministres', u'je', u'rencontrerai', u'dans', ...
&lt;/pre&gt;


&lt;h2&gt;Distribution des mots&lt;/h2&gt;


&lt;p&gt;L'objet du découpage en mot est, outre de se confronter à la difficulté bien réelle de la tâche, de mesurer la distribution du lexique dans le corpus des discours. Plus simplement dit, il s'agit de compter le nombre d'occurrences de chacun des mots utilisés. Par mot, j'entends ici &lt;a href=&quot;http://ldelafosse.pagesperso-orange.fr/Glossaire/L.htm#lexie&quot; hreflang=&quot;fr&quot;&gt;lexie&lt;/a&gt; (bien sûr ^^), soit la forme textuelle qui fait référence au mot tel qu'on le trouve dans le dictionnaire.&lt;/p&gt;


&lt;p&gt;Pour le comptage des mots, il est possible d'utiliser un simple dictionnaire en gérant l'ajout d'une clé pour chaque nouveau mot rencontré. NLTK propose toutefois une classe qui permet de s'affranchir de cette déclaration et de se concentrer uniquement sur le comptage : la classe &lt;a href=&quot;http://nltk.googlecode.com/svn/trunk/doc/api/nltk.probability.FreqDist-class.html&quot; hreflang=&quot;en&quot;&gt;FreqDist&lt;/a&gt;.&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;from&lt;/span&gt; nltk.&lt;span style=&quot;color: black;&quot;&gt;probability&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; FreqDist
 
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Comptage des mots à la création&lt;/span&gt;
fdist = FreqDist&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;  tokenizer.&lt;span style=&quot;color: #dc143c;&quot;&gt;tokenize&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;text&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;  &lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
 
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# ... ou bien en parcourant la liste&lt;/span&gt;
fdist = FreqDist&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; word &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt;  tokenizer.&lt;span style=&quot;color: #dc143c;&quot;&gt;tokenize&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;text&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
     fdist.&lt;span style=&quot;color: black;&quot;&gt;inc&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt; word.&lt;span style=&quot;color: black;&quot;&gt;lower&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;En plus d'économiser la déclaration des mots, la classe ordonne automatiquement les mots par fréquence décroissante :&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; fdist.items()[:30]

[(u'de', 53), (u'la', 36), (u'qui', 23), (u'et', 19), (u&amp;quot;l'&amp;quot;, 18), (u'un', 17), (u'\xe0', 14), (u'des', 13),
 (u'que', 13), (u'les', 12), (u'le', 11), (u'a', 10), (u'est', 10), (u'homme', 10), (u'sa', 9),
 (u'aim\xe9', 8), (u'c\xe9saire', 8), (u'je', 8), (u&amp;quot;c'&amp;quot;, 7), (u'dans', 7), (u'du', 7), (u'cet', 6),
 (u'france', 6), (u'martinique', 6), (u'nous', 6), (u'ses', 6), (u'avec', 5), (u'ce', 5), (u&amp;quot;d'&amp;quot;, 5), (u'martiniquais', 5)]
&lt;/pre&gt;


&lt;h2&gt;Filtrage des mots non signifiants&lt;/h2&gt;


&lt;p&gt;Tous les mots d'un texte ne jouent pas le même rôle : certains participent à la construction du sens (je les appellerai &lt;em&gt;signifiants&lt;/em&gt;), d'autres ont un rôle d'assemblage (je les appellerai &lt;em&gt;outils&lt;/em&gt;). La catégorisation est volontairement extrêmement grossière. Toute la profondeur de l'analyse lexicale réside précisément dans le juste filtrage des mots qui n'apporte pas de sens dans le contexte de l'énonciation. Comment les sélectionner ?&lt;/p&gt;


&lt;p&gt;Une hypothèse simple, mais qui fonctionne assez bien, est de considérer que les mots les plus communs ne participent pas à l'élaboration du sens. L'hypothèse est globalement validé si l'on observe le top 50 des mots du corpus :&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; fdist.items()[:50]

[(u'de', 101164), (u'la', 67872), (u'le', 48749), (u'et', 42159), (u&amp;quot;l'&amp;quot;, 41465), (u'les', 36784), (u'que', 33999),
 (u'\xe0', 31771), (u'des', 30118), (u'est', 30097), (u&amp;quot;d'&amp;quot;, 26459), (u'en', 22888), (u'qui', 22072), (u'je', 21717),
 (u'un', 20687), (u'pas', 20162), (u'pour', 19479), (u'du', 18521), (u'il', 17603), (u'une', 17362), (u'a', 16495),
 (u'nous', 16219), (u&amp;quot;c'&amp;quot;, 15088), (u'dans', 14612), (u'vous', 14175), (u'on', 13874), (u'ce', 13645), (u'ne', 13540),
 (u'qu', 12816), (u&amp;quot;n'&amp;quot;, 12401), (u'au', 11463), (u'plus', 9716), (u'sur', 8973), (u'france', 8316), (u'avec', 8311), (u'y', 8047),
 (u'mais', 7662), (u'pr\xe9sident', 7540), (u&amp;quot;j'&amp;quot;, 6409), (u'par', 6384), (u'se', 6252), (u'sous', 6225), (u'r\xe9publique', 6179),
 (u'sont', 5905), (u&amp;quot;s'&amp;quot;, 5809), (u'aux', 5673), (u'publi\xe9', 5586), (u'class\xe9', 5576), (u'cette', 5564), (u'cela', 5555)]
&lt;/pre&gt;


&lt;p&gt;Dans les faits, il semble important de conserver des mots tels que &lt;em&gt;france&lt;/em&gt; ou &lt;em&gt;république&lt;/em&gt;. Au final, il est donc préférable d'opérer la sélection manuellement.&lt;/p&gt;


&lt;p&gt;Une première phase de filtrage consiste à supprimer toutes les entrées qui correspondent aux mots outils classiques (déterminants, pronoms, verbes être et avoir, ...). NLTK propose une telle liste de mots :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;from&lt;/span&gt; nltk.&lt;span style=&quot;color: black;&quot;&gt;corpus&lt;/span&gt; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; stopwords
 
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; sw &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; stopwords.&lt;span style=&quot;color: black;&quot;&gt;words&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;french&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
     &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; fdist.&lt;span style=&quot;color: black;&quot;&gt;has_key&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;sw&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;:
          fdist.&lt;span style=&quot;color: black;&quot;&gt;pop&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;sw&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Il faut ensuite la compléter en observant manuellement, parmi les mots les plus fréquents, ceux qui n'apportent pas d'information sur le contenu d'un discours politique. J'ai déposé une proposition d'anti-dictionnaire composé de 318 mots, encodés en UTF-8, dans &lt;a href=&quot;http://fabienpoulard.info/download/Recherche/Corpus/antidictionnaire&quot; hreflang=&quot;fr&quot;&gt;l'espace de téléchargement du corpus&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Le top 50 des mots du corpus une fois l'anti-dictionnaire appliqué est beaucoup plus parlant :&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; fdist.items()[:50]

[(u'france', 8316), (u'pr\xe9sident', 7540), (u'r\xe9publique', 6179), (u'monde', 4083), (u'fran\xe7ais', 3194),
 (u'europe', 3189), (u'travail', 2434), (u'ministre', 2331), (u'\xe9tat', 2175), (u'politique', 2130),
 (u'fran\xe7aise', 1812), (u'question', 1770), (u'crise', 1755), (u'avenir', 1515), (u'entreprises', 1480),
 (u'r\xe9forme', 1430), (u's\xe9curit\xe9', 1424), (u'vie', 1424), (u'besoin', 1420), (u'conseil', 1397),
 (u'international', 1319), (u'emploi', 1318), (u'recherche', 1314), (u'gouvernement', 1308), (u'\xe9conomie', 1253),
 (u'd\xe9veloppement', 1194), (u'etat', 1153), (u'palais', 1136), (u'paris', 1123), (u'droit', 1098),
 (u'histoire', 1079), (u'service', 1074), (u'\xe9conomique', 1054), (u'syst\xe8me', 1046), (u'enfants', 1006),
 (u'jeunes', 1000), (u'afrique', 985), (u'moyens', 954), (u'europ\xe9enne', 950), (u'plan', 941), (u'croissance', 925),
 (u'nationale', 903), (u'amis', 900), (u'loi', 888), (u'ministres', 885), (u'sant\xe9', 885), (u'projet', 873),
 (u'hommes', 842), (u'euros', 833), (u'culture', 829)]
&lt;/pre&gt;


&lt;h2&gt;Visualisation&lt;/h2&gt;


&lt;p&gt;La dernière étape du TP consiste à visualiser les données ainsi collectées. Il existe plusieurs méthodes de visualisation de ce type de données (voir notamment &lt;a href=&quot;http://blog.veronis.fr/2006/04/2007-larbre-des-thmes.html&quot; hreflang=&quot;fr&quot;&gt;ce billet de Jean Véronis&lt;/a&gt;), j'ai choisi la plus simple de toutes : le nuage de mots.&lt;/p&gt;


&lt;p&gt;J'utilise l'&lt;a href=&quot;http://www.wordle.net/&quot; hreflang=&quot;en&quot;&gt;outil Wordle&lt;/a&gt; pour générer mes nuages, et plus particulièrement &lt;a href=&quot;http://www.wordle.net/advanced&quot; hreflang=&quot;en&quot;&gt;l'interface avancée&lt;/a&gt; qui permet d'indiquer soit même les mots retenus et leur pondération.&lt;/p&gt;


&lt;p&gt;J'ai retenu pour le nuage les 500 mots les plus présents dans le corpus. Leur pondération est calculée à partir de leur fréquence multipliée par 10000 :&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; w &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; fdist.&lt;span style=&quot;color: black;&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;:&lt;span style=&quot;color: #ff4500;&quot;&gt;500&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]&lt;/span&gt;:
     &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;%s:%d&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;w, &lt;span style=&quot;color: #008000;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;fdist.&lt;span style=&quot;color: black;&quot;&gt;freq&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;w&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Le résultat est visible dans &lt;a href=&quot;http://www.wordle.net/show/wrdl/4298454/Discours_de_N._Sarkozy&quot; hreflang=&quot;fr&quot;&gt;la galerie publique de Wordle&lt;/a&gt;, ou ci-dessous :&lt;/p&gt;


&lt;p&gt;&lt;a href=&quot;http://fabienpoulard.info/public/2011/TP-TALN/nuage-mots-sarkozy.png&quot; title=&quot;Nuage de mots du corpus des discours publiques de N. Sarkozy en tant que président de la République (jusqu'au 24 octobre 2011).&quot;&gt;&lt;img src=&quot;http://fabienpoulard.info/public/2011/TP-TALN/.nuage-mots-sarkozy_m.jpg&quot; alt=&quot;Nuage de mots du corpus des discours publiques de N. Sarkozy en tant que président de la République (jusqu'au 24 octobre 2011).&quot; style=&quot;display:block; margin:0 auto;&quot; title=&quot;Nuage de mots du corpus des discours publiques de N. Sarkozy en tant que président de la République (jusqu'au 24 octobre 2011)., oct. 2011&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description>
    
    
    
      </item>
    
  <item>
    <title>Un cours de TALN parfait ?</title>
    <link>http://fabienpoulard.info/post/2011/01/03/Enseignement-du-TAL</link>
    <guid isPermaLink="false">urn:md5:b08304fc60d04e1dbe0602f48ad3efc3</guid>
    <pubDate>Mon, 03 Jan 2011 00:55:00 +0100</pubDate>
    <dc:creator>Fabien Poulard</dc:creator>
        <category>Sciences &amp; Recherche</category>
        <category>corpus</category><category>enseignement</category><category>exercice</category><category>nlp</category><category>nltk</category><category>python</category><category>éducation</category>    
    <description>&lt;p&gt;Depuis le début de mon doctorat j'ai eu la chance de pouvoir dispenser plusieurs enseignements autour du TALN. Malheureusement je n'ai jamais été responsable du module et je n'ai jamais tellement eu la liberté d'enseigner comme je le souhaitais. Personnellement je n'ai pas aimé la façon dont le TALN m'a été enseigné et par conséquent je n'aime pas l'enseigner de la même façon qu'il me l'a été.&lt;/p&gt;


&lt;p&gt;Quelle aurait été le cours de TALN que j'aurais aimé avoir ? Certainement quelque chose mieux ancré dans le TALN d'aujourd'hui et orienté vers les méthodes et les applications !&lt;/p&gt;    &lt;p&gt;Cette année, j'enseigne encore une fois le TALN à l'&lt;a href=&quot;http://www.polytech.univ-nantes.fr/&quot; hreflang=&quot;fr&quot;&gt;EPUN&lt;/a&gt; : 9h de TP destinés à des étudiants de 1ère année d'ingénieur. La différence c'est que cette fois j'ai plus ou moins quartier libre. Cette liberté est toute relative puisque je ne suis responsable que des TP et que ceux-ci devront coller aux cours (sur lesquels je n'ai aucune prise). Toutefois, cette année je ne suis pas tenu de donner un projet noté. En d'autres termes, je peux exploiter la totalité des heures pour faire des exercices : apprendre en faisant comme on dit.&lt;/p&gt;


&lt;p&gt;Habituellement, les travaux pratiques sont faits en &lt;a href=&quot;http://www.perl.org/&quot;&gt;Perl&lt;/a&gt;, un langage que je n'apprécie pas du tout alors qu'il est certainement le langage favoris de ma communauté. Sans disserter sur les raisons de notre désamour, je pense qu'il n'apporte rien d'autre qu'un bon moteur pour les &lt;a href=&quot;http://fr.wikipedia.org/wiki/Expression_rationnelle&quot;&gt;expressions rationnelles&lt;/a&gt;. S'il y a dix ans cette spécificité justifiait son choix, aujourd'hui tous les langages généralistes supportent aussi bien les expressions rationnelles. Le comble est qu'il n'existe pas (à ma connaissance) de plateforme (modules, extensions...) permettant de faire du TALN en Perl, alors qu'on en trouve pour &lt;a href=&quot;http://www.nltk.org&quot;&gt;Python&lt;/a&gt; et &lt;a href=&quot;http://uima.apache.org&quot;&gt;Java&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Plutôt que Perl, je pense que je vais faire travailler mes étudiants avec &lt;a href=&quot;http://www.python.org&quot;&gt;Python&lt;/a&gt;. Outre le fait qu'il me sera beaucoup plus aisé de corriger les projets (les programmes Perl des gens qui ne maîtrisent pas le langage peuvent être la cause de sérieuses crises de nerf), l'apprentissage du langage Python leur sera utile pour d'autres projets scientifiques (voir &lt;a href=&quot;http://ftp.traduc.org/doc-vf/gazette-linux/html/2005/114/lg114-A.html&quot; hreflang=&quot;fr&quot;&gt;ici&lt;/a&gt;, &lt;a href=&quot;http://ftp.traduc.org/doc-vf/gazette-linux/html/2005/115/lg115-A.html&quot; hreflang=&quot;fr&quot;&gt;ici&lt;/a&gt; ou encore &lt;a href=&quot;http://matthieu-brucher.developpez.com/livre/python/scientifiques/&quot; hreflang=&quot;fr&quot;&gt;là&lt;/a&gt;). De plus, le cadriciel &lt;a href=&quot;http://www.nltk.org&quot;&gt;nltk&lt;/a&gt; permet d'aborder rapidement les principales tâches du TALN sans avoir à tout réimplémenter. Le cadriciel est notamment &lt;a href=&quot;http://www.nltk.org/book&quot; hreflang=&quot;en&quot;&gt;très bien documenté&lt;/a&gt;, ce qui ne gâche rien !&lt;/p&gt;


&lt;p&gt;Au niveau corpus, je ne sais pas encore très bien sur quoi je vais les faire travailler car il y a malheureusement assez peu de corpus français intéressants pour l'enseignement. Remplacez &lt;em&gt;intéressants pour l'enseignement&lt;/em&gt; par &lt;em&gt;on se fout qu'il soit représentatif, on veut juste pouvoir faire des exercices marrants et observer des phénomènes linguistiques simples&lt;/em&gt;. J'hésite entre &lt;a href=&quot;http://fabienpoulard.info/post/2010/03/13/Corpus-des-discours-de-Nicolas-Sarkozy-%28M%C3%80J%29&quot;&gt;mon corpus sur les discours de Nicolas Sarkozy&lt;/a&gt; ou celui que j'ai constitué à partir de &lt;a href=&quot;http://fr.wikinews.org/&quot; hreflang=&quot;fr&quot;&gt;Wikinews&lt;/a&gt;. Je pourrais également utiliser le French TreeBank qui a l'avantage d'être arboré (découpage en mots et annotation des rôles grammaticaux) mais je ne suis pas sûr que sa licence le permette. Je pourrais également explorer certaines œuvres du &lt;a href=&quot;http://www.gutenberg.org/&quot;&gt;projet Guteberg&lt;/a&gt;. Il manque clairement un corpus libre tout plein d'annotations pour le français !&lt;/p&gt;


&lt;p&gt;Enfin au niveau du contenu il va falloir que je me creuse un petit peu la tête. Le cours présente pas mal (trop ?) de choses : &lt;a href=&quot;http://fr.wikipedia.org/wiki/Expression_rationnelle&quot; hreflang=&quot;fr&quot;&gt;expressions rationnelles&lt;/a&gt;, &lt;a href=&quot;http://fr.wikipedia.org/wiki/Morphologie_(linguistique)&quot; hreflang=&quot;fr&quot;&gt;analyse morphologique&lt;/a&gt; (&lt;a href=&quot;http://fabienpoulard.info/post/2008/02/21/Lalgorithme-de-Porter&quot;&gt;algorithme de Porter&lt;/a&gt; notamment), &lt;a href=&quot;http://fr.wikipedia.org/wiki/Syntaxe&quot; hreflang=&quot;fr&quot;&gt;analyse syntaxique&lt;/a&gt; mais également un peu de &lt;a href=&quot;http://fr.wikipedia.org/wiki/N-gramme&quot; hreflang=&quot;fr&quot;&gt;n-grammes&lt;/a&gt;, des &lt;a href=&quot;http://fr.wikipedia.org/wiki/Mod%C3%A8le_de_Markov_cach%C3%A9&quot;&gt;modèles de Markov cachés&lt;/a&gt; et j'en oublie certainement...&lt;/p&gt;


&lt;p&gt;Le premier cours est mardi prochain, il va falloir que je me casse la tête d'ici là. Je pense les faire travailler sur des choses simples :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;quelques exercices sur les expressions rationnelles&lt;/li&gt;
&lt;li&gt;prise en main de NLTK&lt;/li&gt;
&lt;li&gt;découpage en mots&lt;/li&gt;
&lt;li&gt;loi de Zipf&lt;/li&gt;
&lt;li&gt;une super application de l'utilisation du découpage en mots et des expressions rationnelles ???&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Si certains d'entre-vous ont des ressources, des supports de cours ou des idées qui pourraient m'inspirer... n'hésitez pas ! :)&lt;/p&gt;</description>
    
    
    
      </item>
    
  <item>
    <title>Calculer le recouvrement de deux textes avec NLTK</title>
    <link>http://fabienpoulard.info/post/2010/08/26/Calculer-le-recouvrement-de-deux-textes-avec-NLTK</link>
    <guid isPermaLink="false">urn:md5:c3b41b8dbcdfdc80f383c7f775d8833e</guid>
    <pubDate>Thu, 26 Aug 2010 00:11:00 +0200</pubDate>
    <dc:creator>Fabien Poulard</dc:creator>
        <category>Sciences &amp; Recherche</category>
        <category>nlp</category><category>nltk</category><category>python</category>    
    <description>&lt;p&gt;Mon travail de thèse, sobrement intitulée &lt;em&gt;Détection de dérivations de texte&lt;/em&gt;, consiste à évaluer la probabilité qu'un texte dérive d'un autre. Une des applications, que je déteste mais qui a l'avantage de parler à la plupart des gens, est la détection de plagiat.&lt;/p&gt;


&lt;p&gt;Une approche, naïve mais simple à mettre en œuvre, consiste à calculer le nombre de mots que deux textes partagent. Voici une proposition d'implémentation d'une telle technique tirant partie de la &lt;a href=&quot;http://www.nltk.org&quot; hreflang=&quot;en&quot;&gt;bibliothèque NLTK&lt;/a&gt;.&lt;/p&gt;    &lt;p&gt;Le calcul de la couverture en terme de mots de deux textes n'a rien de bien compliqué dans la mesure où&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;il nous est possible de découper le texte en mots&amp;nbsp;;&lt;/li&gt;
&lt;li&gt;nous savons compter le nombre de mots qu'ils ont en commun.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pour le découpage en mots, nous allons utiliser la &lt;a href=&quot;http://www.nltk.org&quot; hreflang=&quot;en&quot;&gt;bibliothèque NLTK&lt;/a&gt; (j'utilise la version bêta de la 2.0) qui propose une méthode &lt;em&gt;word_tokenize&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; nltk
nltk.&lt;span style=&quot;color: black;&quot;&gt;word_tokenize&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;A sentence to be tokenized&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;u&lt;span style=&quot;color: #483d8b;&quot;&gt;'A'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'sentence'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'to'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'be'&lt;/span&gt;, u&lt;span style=&quot;color: #483d8b;&quot;&gt;'tokenized'&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Le tokeniseur est loin d'être parfait, notamment pour les textes qui ne sont pas de l'anglais, mais il fonctionne suffisamment bien pour l'utilisation que nous en avons. Ce serait beaucoup plus problématique pour une tâche de recherche d'information par exemple.&lt;/p&gt;


&lt;p&gt;Nous passerons donc le contenu des fichiers à comparer à cette fonction qui nous retourne la liste des mots correspondant. Il nous faut alors calculer l'intersection des deux listes de mots.&lt;/p&gt;


&lt;p&gt;Les objets &lt;em&gt;set&lt;/em&gt; offrent une méthode &lt;em&gt;intersection&lt;/em&gt; qui ferait l'affaire. Cependant la conversion en un ensemble va faire disparaître les doublons, or nous aimerions avoir un recouvrement aussi précis que possible. Nous définissons donc une fonction qui va calculer l'intersection de deux listes. Elle peut certainement être largement améliorée (ne pas hésiter à proposer une correction), mais suffira pour l'exemple&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; intersect_list&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;lst1, lst2&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
	&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;
	Calcule l'intersection entre deux listes
	&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Intersection&lt;/span&gt;
	inter = &lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Calcul des éléments en commun&lt;/span&gt;
	common = &lt;span style=&quot;color: #008000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;lst1&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;intersection&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;lst2&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Construction de la liste commune&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; e &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; common:
		nbe = &lt;span style=&quot;color: #008000;&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;lst1.&lt;span style=&quot;color: black;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;e&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;, lst2.&lt;span style=&quot;color: black;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;e&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
		&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;nbe&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
			inter.&lt;span style=&quot;color: black;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;e&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; inter&lt;/pre&gt;


&lt;p&gt;Nous avons désormais en main tous les éléments nécessaire pour écrire un petit script qui calculera le taux de recouvrement en mots entre deux fichiers textes.&lt;/p&gt;


&lt;p&gt;Voici le script complet pour ceux que ça intéresse&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;python python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#! /usr/bin/python&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# -*- coding: utf-8 -*-&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Script permettant de calculer le taux de recouvrement en mots entre les&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# deux textes passés en paramètre.&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; nltk
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;os&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;path&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;codecs&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; loadTxt&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;fic&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
	&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;
	Charge le contenu textuel du fichier en paramètre
	&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
	fh  = &lt;span style=&quot;color: #dc143c;&quot;&gt;codecs&lt;/span&gt;.&lt;span style=&quot;color: #008000;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;fic, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;r&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;, errors=&lt;span style=&quot;color: #483d8b;&quot;&gt;'ignore'&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	txt = fh.&lt;span style=&quot;color: black;&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	fh.&lt;span style=&quot;color: black;&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; txt
&amp;nbsp;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; intersect_list&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;lst1, lst2&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
	&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;
	Calcule l'intersection entre deux listes
	&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Intersection&lt;/span&gt;
	inter = &lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Calcul des éléments en commun&lt;/span&gt;
	common = &lt;span style=&quot;color: #008000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;lst1&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;intersection&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;lst2&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Construction de la liste commune&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; e &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; common:
		nbe = &lt;span style=&quot;color: #008000;&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;lst1.&lt;span style=&quot;color: black;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;e&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;, lst2.&lt;span style=&quot;color: black;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;e&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
		&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;nbe&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
			inter.&lt;span style=&quot;color: black;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;e&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; inter
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Vérifie qu'il y a bien deux fichiers passés en paramètre du script&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt; == &lt;span style=&quot;color: #ff4500;&quot;&gt;3&lt;/span&gt;:
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Charge les textes des fichiers&lt;/span&gt;
	txt1 = loadTxt&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	txt2 = loadTxt&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Tokenisation en mots&lt;/span&gt;
	txt1toks = nltk.&lt;span style=&quot;color: black;&quot;&gt;word_tokenize&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;txt1&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	txt2toks = nltk.&lt;span style=&quot;color: black;&quot;&gt;word_tokenize&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;txt2&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Calcul de la couverture&lt;/span&gt;
	incommon = intersect_list&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;txt1toks, txt2toks&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Affichage&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;Couverture de %s par rapport à %s&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\t&lt;/span&gt;%f&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;incommon&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;/&lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;txt1toks&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;Couverture de %s par rapport à %s&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\t&lt;/span&gt;%f&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;incommon&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;/&lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;txt2toks&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;else&lt;/span&gt;:
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;Utilisation :&amp;quot;&lt;/span&gt;
	&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\t&lt;/span&gt;%s &amp;lt;fichier 1&amp;gt; &amp;lt;fichier 2&amp;gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Bien entendu, le taux de recouvrement en mots n'est pas très performant pour évaluer le degré de dérivation entre deux textes. Une manière de l'améliorer serait de calculer le taux de recouvrement de n-grammes plutôt. J'attends vos propositions :)&lt;/p&gt;</description>
    
    
    
      </item>
    
  <item>
    <title>Tokenisation en mots avec NLTK</title>
    <link>http://fabienpoulard.info/post/2008/03/05/Tokenisation-en-mots-avec-NLTK</link>
    <guid isPermaLink="false">urn:md5:27f95599f6dadc4a9910b093a9bf9625</guid>
    <pubDate>Wed, 05 Mar 2008 14:39:00 +0100</pubDate>
    <dc:creator>Fabien Poulard</dc:creator>
        <category>Sciences &amp; Recherche</category>
        <category>nlp</category><category>nltk</category><category>python</category><category>tokenization</category>    
    <description>&lt;p&gt;Le toolkit NLTK intègre plusieurs algorithmes permettant de découper un texte en mots. Ce billet présente plusieurs d'entre eux.&lt;/p&gt;    &lt;h2&gt;Un point sur la tokenisation&lt;/h2&gt;


&lt;p&gt;La tokenisation est l'opération de segmenter un acte langagier en unités &quot;atomiques&quot;&amp;nbsp;: les tokens. Les tokenisations les plus courantes sont le découpage en mots ou bien en phrases. Ainsi la tokenisation en mots de la phrase &lt;q&gt;Tout le monde attendait une véritable furia rouge.&lt;/q&gt; nous donnerait les composants mots&amp;nbsp;: &lt;q&gt;Tout&lt;/q&gt;, &lt;q&gt;le&lt;/q&gt;, &lt;q&gt;monde&lt;/q&gt;, &lt;q&gt;attendait&lt;/q&gt;, &lt;q&gt;une&lt;/q&gt;, &lt;q&gt;véritable&lt;/q&gt;, &lt;q&gt;furia&lt;/q&gt;, &lt;q&gt;rouge&lt;/q&gt;, &lt;q&gt;.&lt;/q&gt;.&lt;/p&gt;


&lt;p&gt;En traitement automatique des langues (&lt;a href=&quot;http://fr.wikipedia.org/wiki/Traitement_automatique_des_langues&quot; hreflang=&quot;fr&quot;&gt;TAL&lt;/a&gt;), la tokenisation est surtout utilisée lors des prétraitements afin d'identifier des unités de &quot;haut niveau&quot; sur lesquelles porteront l'analyse. De plus, le terme &quot;tokenisation&quot; est couramment associé à l'idée de &quot;découpage en mots&quot;, soit une analyse morphologique des actes langagiers.&lt;/p&gt;


&lt;p&gt;Nous nous intéressons par la suite aux outils disponibles au sein de &lt;a href=&quot;http://nltk.sourceforge.net&quot; hreflang=&quot;fr&quot;&gt;NLTK&lt;/a&gt; pour réaliser une segmentation en mots.&lt;/p&gt;


&lt;h2&gt;Tokenisation avec NLTK&lt;/h2&gt;


&lt;p&gt;Nous allons utiliser le langage &lt;a href=&quot;http://www.python.org&quot; hreflang=&quot;fr&quot;&gt;Python&lt;/a&gt;, et notamment le package &lt;a href=&quot;http://nltk.sourceforge.net&quot; hreflang=&quot;fr&quot;&gt;NLTK&lt;/a&gt;, pour tokeniser en mots. Avant toute chose, il est nécessaire d'ouvrir une console python et d'importer nltk à l'aide de la commande &lt;em&gt;import nltk&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;

&lt;pre&gt;
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32) 
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type &amp;quot;help&amp;quot;, &amp;quot;copyright&amp;quot;, &amp;quot;credits&amp;quot; or &amp;quot;license&amp;quot; for more information.
&amp;gt;&amp;gt;&amp;gt; import nltk
&lt;/pre&gt;


&lt;p&gt;Si vous obtenez un message d'erreur comme ci-dessous, c'est que le paquet nltk n'est pas installé ou bien que Python n'arrive pas à le trouver. &lt;a href=&quot;http://nltk.sourceforge.net/index.php/Installation&quot; hreflang=&quot;fr&quot;&gt;Cette page&lt;/a&gt; devrait vous permettre de résoudre le problème.&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; import nltk
Traceback (most recent call last):
  File &amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;
ImportError: No module named nltk
&lt;/pre&gt;


&lt;p&gt;Nous allons travailler sur deux textes, le premier en anglais et le second en français. Les textes en anglais posent assez peu de problèmes alors que ceux en français nous confrontent à certaines subtilités. Ces deux textes sont issus de wikipédia et peuvent donc être utilisés, diffusés ou modifiés librement en accord avec la licence &lt;a href=&quot;http://www.gnu.org/copyleft/fdl.html&quot; hreflang=&quot;en&quot;&gt;GFDL&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Stockons le texte en français dans la variable &lt;em&gt;txt_fr&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;
&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; txt_fr = u&amp;quot;&amp;quot;&amp;quot;
Rapidement, ils ont été fondus avec un alliage de plomb (80 %), d’antimoine (5 %) 
et d'étain (15 %) dans des matrices. L’ouvrier typographe se servait d’un composteur 
sur lequel il alignait les caractères, lus à l’envers, de gauche à droite, piochés dans 
une boîte appelée « casse ». Les caractères du haut de la casse étaient appelés les 
capitales (majuscules) et ceux du bas — les minuscules — les bas-de-casse.&amp;quot;&amp;quot;&amp;quot;
&lt;/pre&gt;


&lt;p&gt;... et celui en anglais dans la variable &lt;em&gt;txt_en&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;
&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; txt_en = u&amp;quot;&amp;quot;&amp;quot;
Monty Python, or The Pythons, is the collective name of the creators of Monty Python's 
Flying Circus, a British television comedy sketch show that first aired on the BBC on 5 
October 1969. A total of 45 episodes were made over four series. The Python phenomenon 
developed from the original television series into something much larger in scope and impact.&amp;quot;&amp;quot;&amp;quot;
&lt;/pre&gt;


&lt;h3&gt;Découpage naïf avec &lt;em&gt;split&lt;/em&gt;&lt;/h3&gt;


&lt;p&gt;Dans le formalisme Python, les deux chaînes unicodes précédentes sont en réalité des instances de la classe &lt;em&gt;unicode&lt;/em&gt;. Cette classe possède une méthode &lt;em&gt;split()&lt;/em&gt; qui permet d'éclater la chaîne en une liste de composants à partir d'un caractère séparateur passé en paramètre. Si jamais aucun séparateur n'est spécifié, les caractères correspondant à des espaces sont utilisés comme séparateurs.&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; txt_fr.split()
[u'Rapidement,', u'ils', u'ont', ...]

&amp;gt;&amp;gt;&amp;gt; txt_en.split()
[u'Monty', u'Python,', u'or', u'The', ...]
&lt;/pre&gt;


&lt;p&gt;Cette approche naïve de découpage à l'aide des séparateurs &lt;em&gt;espaces&lt;/em&gt; fonctionne globalement plutôt bien, excepté lorsque des éléments ponctuatifs sont accolés aux mots. Pour la suite, il est nécessaire de définir des mesures d'évaluation de la qualité de la tokenisation en mots. Nous utilisons des mesures régulièrement en TALN&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;la précision&amp;nbsp;: proportion, parmi les mots identifiés, de mots corrects&amp;nbsp;;&lt;/li&gt;
&lt;li&gt;le rappel&amp;nbsp;: proportion, parmi les mots attendus, de mots identifiés.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Étant donné que nous manipulons des listes, nous pouvons adopter une approche &lt;em&gt;ensembliste&lt;/em&gt; dans la définition des mesures de précision et de rappel. Ainsi, avec W le résultat attendu de la segmentation en mots et T le résultat calculé, on pose&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Précision(T, W) = |T ⋂ W| / |T|&lt;/li&gt;
&lt;li&gt;Rappel(T, W) = |T ⋂ W| / |W|&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ce qui nous donne, en tenant compte du fait que nous manipulons des listes et non des ensembles, l'implémentation ci-dessous. Notez toutefois que le calcul des mesures est approximative, mais suffisamment précise pour nos besoins. Une version exacte devrait être utilisé pour une évaluation scientifiquement correcte et précise.&lt;/p&gt;

&lt;pre&gt;
def metrics(lstcomp, lstref) :
    card_intersec = 0.0 # force à utiliser la division non entière
    for t in set(lstcomp) :
        card_intersec += min(lstref.count(t), lstcomp.count(t))
    precision = card_intersec/len(lstcomp)
    rappel = card_intersec/len(lstref)
    return (precision, rappel)
&lt;/pre&gt;


&lt;p&gt;Le calcul de ces mesures nécessite d'avoir une tokenisation de référence. Afin de vous éviter le fastidieux travail de tokenisation à la main, voici ci-dessous les deux tokenisations de référence que j'ai utilisé par la suite&amp;nbsp;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pour le texte en français&amp;nbsp;:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; ref_toks_fr = [u'Rapidement', u',', u'ils', u'ont', u'\xe9t\xe9', u'fondus', u'avec', u'un', u'alliage', u'de', 
u'plomb', u'(', u'80 %', u')', u',', u'd\u2019', u'antimoine', u'(', u'5 %', u')', u'et', u'd\'', u'\xe9tain', u'(', u'15 %', u')',
u'dans', u'des', u'matrices', u'.', u'L\u2019', u'ouvrier', u'typographe', u'se', u'servait', u'd\u2019', u'un',
u'composteur', u'sur', u'lequel', u'il', u'alignait', u'les', u'caract\xe8res', u',', u'lus', u'\xe0', u'l\u2019', 
u'envers', u',', u'de', u'gauche', u'\xe0', u'droite', u',', u'pioch\xe9s', u'dans', u'une', u'bo\xeete', u'appel\xe9e',
u'\xab', u'casse', u'\xbb', u'.', u'Les', u'caract\xe8res', u'du', u'haut', u'de', u'la', u'casse', u'\xe9taient', 
u'appel\xe9s', u'les', u'capitales', u'(', u'majuscules', u')', u'et', u'ceux', u'du', u'bas', u'\u2014', u'les', 
u'minuscules', u'\u2014', u'les', u'bas', u'-', u'de', u'-', u'casse', u'.']
&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;... et pour le texte en anglais&amp;nbsp;:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; ref_toks_en = [u'Monty', u'Python', u',', u'or', u'The', u'Pythons', u',', u'is', u'the', u'collective', u'name',
u'of', u'the', u'creators', u'of', u'Monty', u'Python', u'\'s', u'Flying', u'Circus', u',', u'a', u'British', u'television',
u'comedy', u'sketch', u'show', u'that', u'first', u'aired', u'on', u'the', u'BBC', u'on', u'5', u'October', u'1969',
u'.', u'A', u'total', u'of', u'45', u'episodes', u'were', u'made', u'over', u'four', u'series', u'.', u'The', u'Python', 
u'phenomenon', u'developed', u'from', u'the', u'original', u'television', u'series', u'into', u'something', 
u'much', u'larger', u'in', u'scope', u'and', u'impact', u'.']
&lt;/pre&gt;


&lt;p&gt;L'application de nos mesures montre clairement que l'utilisation de &lt;em&gt;split&lt;/em&gt; est plus efficace sur l'anglais que sur le français, la différence entre les deux tokenisations étant importantes autant en terme de précision (74% pour le français, contre 88% pour l'anglais), que de rappel (57% pour le français, contre 79% pour l'anglais). Notons toutefois que le texte en français est un peu plus compliqué que celui en anglais. L'utilisation d'un corpus parallèle nous permettrait une comparaison plus exacte des résultats.&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; metrics(txt_fr.split(), ref_toks_fr)
(0.74647887323943662, 0.56989247311827962)

&amp;gt;&amp;gt;&amp;gt; metrics(txt_en.split(), ref_toks_en)
(0.8833333333333333, 0.79104477611940294)
&lt;/pre&gt;


&lt;p&gt;Les résultats de l'algorithme naïf, bien qu'encourageant, peuvent être améliorés, notamment en employant des automates pour repérer des structures particulières telles que les données numériques, les sigles, ...&lt;/p&gt;


&lt;h3&gt;Utilisation d'expressions régulières&lt;/h3&gt;


&lt;p&gt;Si les langues naturelles ne sont pas des langages réguliers, les mots les composant peuvent l'être. NLTK offre une classe &lt;em&gt;RegexpTokenizer&lt;/em&gt; qui permet d'effectuer une tokenisation à partir d'une description des tokens par le biais d'une expression régulière. Dans la &lt;a href=&quot;http://fr.wikipedia.org/wiki/Théorie_des_automates&quot; hreflang=&quot;fr&quot;&gt;théorie des automates&lt;/a&gt;, les expressions régulières sont capables de reconnaître les langages réguliers, tout comme les automates déterministe à états finis.&lt;/p&gt;


&lt;p&gt;À partir de l'expérimentation précédente, nous allons utiliser un expression régulière qui définit plusieurs types de &lt;em&gt;mots&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;les mots composés uniquement de lettres&amp;nbsp;;&lt;/li&gt;
&lt;li&gt;les contractions des mots outils (l', d', ...)&amp;nbsp;;&lt;/li&gt;
&lt;li&gt;les pourcentages&amp;nbsp;;&lt;/li&gt;
&lt;li&gt;les nombres&amp;nbsp;;&lt;/li&gt;
&lt;li&gt;les éléments ponctuatifs et signes diacritiques (tout ce qui n'est ni espace, ni alphanumérique).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cette catégorisation dépend bien entendu de la langue, et n'est pas exhaustive (prise en compte des monnaies, sigles, ...).&lt;/p&gt;


&lt;p&gt;Nous utilisons la possibilité dans Python de structurer les expressions régulières de manière plus compréhensible à l'aide de l'instruction &lt;em&gt;(?x)&lt;/em&gt;. Pour en savoir plus sur les expressions régulières dans Python, se référer à la &lt;a href=&quot;http://docs.python.org/lib/re-syntax.html&quot; hreflang=&quot;en&quot;&gt;documentation de référence&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; tok2 = nltk.RegexpTokenizer(r'''(?x)
          \d+(\.\d+)?\s*%   # les pourcentages
        | \w'               # les contractions d', l', ...
        | \w+               # les mots pleins
        | [^\w\s]           # les ponctuations
        ''')
&lt;/pre&gt;


&lt;p&gt;La simple utilisation d'une typologie des mots et l'écriture de règles en conséquence nous permet d'augmenter significativement les performances, notamment en terme de rappel, pour chacune des langues. Ainsi la précision est de 92% pour le français (97% pour l'anglais) et la précision de 96% (99% pour l'anglais).&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; metrics(tok2.tokenize(txt_fr), ref_toks_fr)
(0.91752577319587625, 0.956989247311828)

&amp;gt;&amp;gt;&amp;gt; metrics(tok2.tokenize(txt_en), ref_toks_en)
(0.97058823529411764, 0.9850746268656716)
&lt;/pre&gt;


&lt;p&gt;Intéressons nous aux cas problématiques, i.e. les mots qui ne sont pas correctement identifiés. Pour l'anglais il s'agit des marques d'appartenance en &lt;em&gt;'s&lt;/em&gt;. En effet, ces dernières sont spécifiques à l'anglais et n'ont pas été prises en compte dans la mise au point de notre expression régulière plus particulièrement adaptée au français. Pour le français, le problème provient des apostrophes unicodes.&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; set(ref_toks_en).difference(tok2.tokenize(txt_en))
set([u&amp;quot;'s&amp;quot;])

&amp;gt;&amp;gt;&amp;gt; set(ref_toks_fr).difference(tok2.tokenize(txt_fr))
set([u'd\u2019', u'L\u2019', u'l\u2019'])
&lt;/pre&gt;


&lt;p&gt;Le tokeniseur ci-dessous prend en compte les résultats de notre expérimentation précédente. L'introduction de caractères unicodes complique largement l'écriture de l'expression régulière.&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; reg_words = r'''(?x)
          \d+(\.\d+)?\s*%   # les pourcentages
        | 's                # l'appartenance anglaise 's
        | \w'               # les contractions d', l', j', t', s'
        '''
&amp;gt;&amp;gt;&amp;gt; reg_words += u&amp;quot;| \w\u2019&amp;quot;      # version unicode
&amp;gt;&amp;gt;&amp;gt; reg_words += u&amp;quot;|\w+|[^\w\s]&amp;quot; # avec les antislashs
&amp;gt;&amp;gt;&amp;gt; tok3 = nltk.RegexpTokenizer(reg_words)

&amp;gt;&amp;gt;&amp;gt; metrics(tok3.tokenize(txt_en), ref_toks_en)
(1.0, 1.0)

&amp;gt;&amp;gt;&amp;gt; metrics(tok3.tokenize(txt_fr), ref_toks_fr)
(1.0, 1.0)
&lt;/pre&gt;


&lt;p&gt;Nous obtenons ainsi une tokénisation &lt;em&gt;parfaite&lt;/em&gt;. Bien entendu, cette performance est à relativiser par rapport à la taille de notre échantillon. Si vous souhaitez obtenir une évaluation plus intéressante - et que vous avez une quelques cycles calculs à dépenser - vous pouvez tester le nouveau tokenizer sur le &lt;em&gt;Brown Corpus&lt;/em&gt; qui est intégré à NLTK&amp;nbsp;:&lt;/p&gt;

&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; metrics(tok3.tokenize(nltk.corpus.brown.raw()),
            nltk.corpus.brown.words())
&lt;/pre&gt;</description>
    
    
    
      </item>
    
  <item>
    <title>NLTK et la loi de Zipf (Traduction et Reprise de ACM Crossroads)</title>
    <link>http://fabienpoulard.info/post/2008/02/10/NLTK-et-la-loi-de-Zipf-Traduction-et-Reprise-de-ACM-Crossroads</link>
    <guid isPermaLink="false">urn:md5:4987ca460967ee4c1b30364668ef9b2a</guid>
    <pubDate>Sun, 10 Feb 2008 18:36:00 +0100</pubDate>
    <dc:creator>Fabien Poulard</dc:creator>
        <category>Sciences &amp; Recherche</category>
        <category>nlp</category><category>nltk</category><category>python</category>    
    <description>&lt;p&gt;Ce billet reprend partiellement l'&lt;a href=&quot;http://www.acm.org/crossroads/xrds13-4/natural_language.html&quot;&gt;article du journal ACM Crossroads&lt;/a&gt; sur NLTK. Il illustre le potentiel du toolkit &lt;a href=&quot;http://nltk.sourceforge.net/&quot;&gt;NLTK (Natural Language Toolkit)&lt;/a&gt; pour Python en montrant l'application de la loi de Zipf sur le corpus Gutenberg.&lt;/p&gt;    &lt;h2&gt;Loi de Zipf&lt;/h2&gt;
&lt;p&gt;La &lt;a href=&quot;http://fr.wikipedia.org/wiki/Loi_de_Zipf&quot;&gt;loi de Zipf&lt;/a&gt;, du nom de George Kingsley Zipf, correspond à l'observation empirique de la distribution de la fréquence des mots dans un texte.&lt;/p&gt;
&lt;p&gt;L'observation de G. K. Zipf est que les textes sont constitués d'un grand nombre de mots qui apparaissent peut de fois et au contraire d'un petit nombre de mots très fréquents dans le texte. En d'autres termes, la fréquence (&lt;em&gt;f&lt;/em&gt;) d'un mot dans un texte est corrélée à son rang (&lt;em&gt;f(n)&lt;/em&gt;) par une loi du type : &lt;em&gt;f(rang) x rang = K&lt;/em&gt; avec K une constante.&lt;/p&gt;
&lt;p&gt;Nous allons tenter de vérifier l'application de cette loi sur le corpus Gutenber, un des corpus distribués avec NLTK.&lt;/p&gt;
&lt;h2&gt;Script Python utilisant NLTK&lt;/h2&gt;
&lt;p&gt;La version de NLTK utilisée au sein lors de la rédaction de l'article contenu dans le &lt;a href=&quot;http://www.acm.org/crossroads/xrds13-4/natural_language.html&quot;&gt;ACM Crossroad&lt;/a&gt; semble un petit peu ancien. Voici donc une version actualisée qui devrait fonctionner avec les dernières versions du Toolkit :&lt;/p&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; from nltk.corpus import gutenberg&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; from nltk.probability import FreqDist&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; from nltk.draw.plot import Plot&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; fd = FreqDist()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for word in gutenberg.words():&lt;br /&gt;...        fd.inc(word)&lt;br /&gt;...&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; ss = fd.sorted()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; points = []&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for index,word in enumerate(ss):&lt;br /&gt;...     points.append( (index+1, fd[word]) ) &lt;br /&gt;...&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; Plot(points, scale='log').mainloop()&lt;/pre&gt;
&lt;img src=&quot;http://fabienpoulard.info/fabienpoulard.info/public/billet_4/zipf_plot.png&quot; alt=&quot;Visualisation de la courbe f=&quot;x.r&quot; liée à la loi de Zipf&quot; style=&quot;margin: 0 auto; display: block;&quot; /&gt;
&lt;p&gt;La première partie du script permet simplement de permettre au script d'accéder à quelques éléments du toolkit tel que :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;le corpus Gutenberg ;&lt;/li&gt;
&lt;li&gt;la classe &lt;em&gt;FreqDist&lt;/em&gt; permettant de comptabiliser des entités et extraire des informations de distribution de ces entités ;&lt;/li&gt;
&lt;li&gt;l'objet &lt;em&gt;Plot&lt;/em&gt; permettant de tracer un graphique à partir des données que nous aurons extraites ;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; from nltk.corpus import gutenberg&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; from nltk.probability import FreqDist&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; from nltk.draw.plot import Plot&lt;/pre&gt;
&lt;p&gt;La seconde partie du code permet dans un premier temps d'instancier la classe &lt;em&gt;FreqDist&lt;/em&gt;.&lt;/p&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; fd = FreqDist()&lt;/pre&gt;
&lt;p&gt;Nous utilisons l'objet obtenu (&lt;em&gt;fd&lt;/em&gt;) pour comptabiliser chacun des mots du corpus. Ce corpus est composé de plusieurs textes. Nous pouvons accéder aux identifiants de ces textes par le biais de la méthode &lt;em&gt;items&lt;/em&gt; :&lt;/p&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; gutenberg.items&lt;br /&gt;('austen-emma', 'austen-persuasion', 'austen-sense', 'bible-kjv', 'blake-poems', 'blake-songs', 'chesterton-ball', 'chesterton-brown',&lt;br /&gt; 'chesterton-thursday', 'milton-paradise', 'shakespeare-caesar', 'shakespeare-hamlet', 'shakespeare-macbeth', 'whitman-leaves')&lt;/pre&gt;
&lt;p&gt;L'article original parcourait les textes un par un en utilisant la méthode &lt;em&gt;raw&lt;/em&gt;. Ceci est incorrect car &lt;em&gt;raw&lt;/em&gt; donne accès au contenu du texte comme une chaîne de caractères. Nous choisissons d'utiliser la méthode &lt;em&gt;words&lt;/em&gt; qui retourne la liste des mots du corpus dans leur ordre chronologique d'apparition.&lt;/p&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; for word in gutenberg.words():&lt;br /&gt;...       fd.inc(word)&lt;/pre&gt;
&lt;p&gt;La classe &lt;em&gt;FreqDist&lt;/em&gt; est extrêmement pratique pour calculer une distribution des éléments d'un texte. Les méthodes de la classe permettent d'obtenir rapidement des informations sur cette distribution :&lt;/p&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; fd.max()&lt;br /&gt;','&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; fd[',']&lt;br /&gt;146772&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; fd.Nr(146772)&lt;br /&gt;1&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; fd['.']&lt;br /&gt;57046&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; fd.freq('.')&lt;br /&gt;0.027755950431840863&lt;/pre&gt;
&lt;p&gt;Ainsi, pour connaître les dix mots les plus présents dans le corpus et leur fréquence respective :&lt;/p&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; for w in fd.sorted()[:10]:&lt;br /&gt;...     print &quot;%3s : %0.2f%%&quot; % (w, fd.freq(w)*100)&lt;br /&gt;... &lt;br /&gt;  , : 7.14%&lt;br /&gt;the : 4.87%&lt;br /&gt;and : 3.17%&lt;br /&gt; of : 2.85%&lt;br /&gt;  . : 2.78%&lt;br /&gt;  : : 2.28%&lt;br /&gt; to : 1.69%&lt;br /&gt; in : 1.21%&lt;br /&gt;  I : 1.14%&lt;br /&gt;  a : 1.13%&lt;/pre&gt;
&lt;p&gt;La dernière partie du script compile les informations de la distribution des mots sous la forme de coordonnées (x,y) avec :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;x&lt;/em&gt; le rang du mot&lt;/li&gt;
&lt;li&gt;&lt;em&gt;y&lt;/em&gt; la fréquence du mot dans le corpus&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Pour calculer le rang du mot, on extrait une version classée par fréquence décroissante de l'objet &lt;em&gt;fd&lt;/em&gt; que l'on transforme en &lt;em&gt;enumerable&lt;/em&gt; grâce à la fonction &lt;em&gt;enumerate&lt;/em&gt; de Python. Un énumérable de la séquence &lt;em&gt;seq&lt;/em&gt; est une liste de tuples du type : &lt;em&gt;(0, seq[0]) (1, seq[1]) ...&lt;/em&gt;. Il suffit donc d'ajouter 1 au premier éléments des tuples pour obtenir le rang :&lt;/p&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; ss = fd.sorted()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; points = []&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for index,word in enumerate(ss):&lt;br /&gt;...     points.append( (index+1, fd.freq(word)) ) &lt;br /&gt;...&lt;/pre&gt;
&lt;p&gt;La dernière étape consiste à tracer un graphe à partir de cette liste de coordonnées grâce à la classe &lt;em&gt;Plot&lt;/em&gt; :&lt;/p&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; Plot(points, scale='log').mainloop()&lt;br /&gt;&lt;br /&gt;&lt;img style=&quot;margin: 0 auto; display: block;&quot; alt=&quot;&quot; src=&quot;http://fabienpoulard.info/dotclear/public/billet_4/zipf_plot.png&quot; /&gt;&lt;/pre&gt;</description>
    
    
    
      </item>
    
</channel>
</rss>