From nalimilan at club.fr  Wed Apr 10 20:43:27 2013
From: nalimilan at club.fr (Milan Bouchet-Valat)
Date: Wed, 10 Apr 2013 20:43:27 +0200
Subject: [Rd] [R] Question on Stopword Removal from a Cyrillic
 (Bulgarian)Text
In-Reply-To: <20130410111716.GA3687@scorpia>
References: <5163BEE2.4090104@gmail.com> <1365537342.2175.59.camel@milan>
	<516519CA.4020907@gmail.com> <1365582567.2175.68.camel@milan>
	<20130410111716.GA3687@scorpia>
Message-ID: <1365619407.29475.12.camel@milan>

Le mercredi 10 avril 2013 ? 13:17 +0200, Ingo Feinerer a ?crit :
> On Wed, Apr 10, 2013 at 10:29:27AM +0200, Milan Bouchet-Valat wrote:
> > Thanks for the reproducible example. Indeed, it does not work here
> > either (Linux with UTF-8 locale). The problem seems to be in the call to
> > gsub() in removeWords: the pattern "\\b" does not match anything when
> > perl=TRUE. With perl=FALSE, it works.
> 
> The \b versus perl versus UTF-8 issue seems to be known, and it is
> advised to use perl = TRUE with \b. See e.g. the warning in the gsub
> help page (?gsub):
> 
> ---8<--------------------------------------------------------------------------
> Warning:
> 
> POSIX 1003.2 mode of ?gsub? and ?gregexpr? does not work correctly with
> repeated word-boundaries (e.g. ?pattern = "\b"?).  Use ?perl = TRUE? for
> such matches (but that may not work as expected with non-ASCII inputs,
> as the meaning of ?word? is system-dependent).
> ---8<--------------------------------------------------------------------------
Thanks for the pointer. Indeed, this allowed me to discover the
existence of the PCRE_UCP (Unicode Character Properties) flag, which
changes matching behavior so that Unicode alphanumerics are not
considered as word boundaries.

This flag should probably be used by R when calling pcre_compile() in
gsub() and friends. At the moment, R's behavior is inconsistent across
platforms:
- on Fedora 18, R 2.15.3 :
gsub("\\bt\\b", "", "t?l?gramme", perl=TRUE)
[1] "?l?gramme"

- on Windows 2008, R 2.15.1 and 3.0.0 :
gsub("\\bt\\b", "", "t?l?gramme", perl=TRUE)
[1] "t?l?gramme"


Luckily, the bug can be fixed at tm's level by adding (*UCP) at the
beginning of the pattern. This works for our examples :

> gsub(sprintf("\\b(%s)\\b", "?????"), "", "?????", perl=TRUE)
[1] "?????"
> gsub(sprintf("(*UCP)\\b(%s)\\b", "?????"), "", "?????", perl=TRUE)
[1] ""

gsub("\\bt\\b", "", "t?l?gramme", perl=TRUE)
[1] "?l?gramme"
gsub("(*UCP)\\bt\\b", "", "t?l?gramme", perl=TRUE)
[1] "t?l?gramme"


Regards



[Rd] [R] Question on Stopword Removal from a Cyrillic (Bulgarian)Text
Milan Bouchet-Valat