2012-03-09 53 views
23

テキスト解析を行うためにRのtmパッケージを使用しようとしています。私は以下を結びつけました:'utf8towcs'に無効な入力があります

require(tm) 
dataSet <- Corpus(DirSource('tmp/')) 
dataSet <- tm_map(dataSet, tolower) 
Error in FUN(X[[6L]], ...) : invalid input 'RT @noXforU Erneut riesiger (Alt-)�lteppich im Golf von Mexiko (#pics vom Freitag) http://bit.ly/bw1hvU http://bit.ly/9R7JCf #oilspill #bp' in 'utf8towcs' 

いくつかの文字は無効です。私は、無効な文字をR内から、または処理のためにファイルをインポートする前に、分析から除外したいと考えています。私はすべてのファイルがUTF-8に変換してそれに変換できないものを除外するためのiconvを使用してみました

は次のとおりです。

find . -type f -exec iconv -t utf-8 "{}" -c -o tmpConverted/"{}" \; 

ここで指摘したようにBatch convert latin-1 files to utf-8 using iconv

しかし、私はまだ取得同じエラー。

ご協力いただきありがとうございます。

答えて

2

これはtmパッケージ(1,2,3)の一般的な問題です。それを修正するために

つの非R方法が見つけて、すべての空想の文字(すなわち。発音区別符号を持つもの)Rにそれをロードする前に、あなたのテキスト内を置き換えるために、テキストエディタを使用する(またはRgsubを使用)することです。たとえば、Öl-TeppichのO-ウムラウトのすべてのインスタンスを検索して置き換えます。 Othersはこれで成功しましたが(私もありますが)、何千もの個別のテキストファイルがあれば、これはうまくいきません。

# I put your example text in a file and tested it with both ANSI and 
# UTF-8 encodings, both enabled me to reproduce your problem 
# 
tmp <- Corpus(DirSource('C:\\...\\tmp/')) 
tmp <- tm_map(dataSet, tolower) 
Error in FUN(X[[1L]], ...) : 
    invalid input 'RT @noXforU Erneut riesiger (Alt-)Öl–teppich im Golf von Mexiko (#pics vom Freitag) http://bit.ly/bw1hvU http://bit.ly/9R7JCf #oilspill #bp' in 'utf8towcs' 
# quite similar error to what you got, both from ANSI and UTF-8 encodings 
# 
# Now try VectorSource instead of DirSource 
tmp <- readLines('C:\\...\\tmp.txt') 
tmp 
[1] "RT @noXforU Erneut riesiger (Alt-)Öl–teppich im Golf von Mexiko (#pics vom Freitag) http://bit.ly/bw1hvU http://bit.ly/9R7JCf #oilspill #bp" 
# looks ok so far 
tmp <- Corpus(VectorSource(tmp)) 
tmp <- tm_map(tmp, tolower) 
tmp[[1]] 
rt @noxforu erneut riesiger (alt-)öl–teppich im golf von mexiko (#pics vom freitag) http://bit.ly/bw1hvu http://bit.ly/9r7jcf #oilspill #bp 
# seems like it's worked just fine. It worked for best for ANSI encoding. 
# There was no error with UTF-8 encoding, but the Ö was returned 
# as ã– which is not good 

しかし、これは幸運偶然のビットのように思える:R解決のために

は、私の代わりにDirSourceVectorSourceを使用しても問題が解決しているようだということがわかりました。それについてもっと直接的な方法が必要です。何があなたのために働くのか教えてください!

+1

返信いただきありがとうございます。何らかの理由で、私にとって失敗した同じコード行が今すぐ動作します。私はこれが別の幸運な偶然であるかどうかわからない:)私は何も変えなかった、ただそれを再実行し、今度は何の問題もなく動作する。 – maiaini

+0

うれしいことを聞いてうれしい! – Ben

1

無効な入力を無視しても問題ない場合は、Rのエラー処理を使用できます。例えば:

dataSet <- Corpus(DirSource('tmp/')) 
    dataSet <- tm_map(dataSet, function(data) { 
    #ERROR HANDLING 
    possibleError <- tryCatch(
     tolower(data), 
     error=function(e) e 
    ) 

    # if(!inherits(possibleError, "error")){ 
    # REAL WORK. Could do more work on your data here, 
    # because you know the input is valid. 
    # useful(data); fun(data); good(data); 
    # } 
    }) 

ここでの追加の例があります:http://gastonsanchez.wordpress.com/2012/05/29/catching-errors-when-using-tolower/

19

これは、TMよくあるご質問からである:

、それは彼らの16進コードを示す文字列 とyourCorpus非コンバーチブルバイトを交換します。

私はこれが役に立ちます。

tm_map(yourCorpus, function(x) iconv(enc2utf8(x), sub = "byte")) 

http://tm.r-forge.r-project.org/faq.html

1

次の手順を使用します。

# First you change your document in .txt format with encoding UFT-8 
library(tm) 
# Set Your directoryExample ("F:/tmp"). 
dataSet <- Corpus(DirSource ("/tmp"), readerControl=list(language="english)) # "/tmp" is your directory. You can use any language in place of English whichever allowed by R. 
dataSet <- tm_map(dataSet, tolower) 

Inspect(dataSet) 
8

私はちょうどこの問題の抵触実行しました。 OSXを実行しているマシンを使用していますか?私はと文字の定義に問題をトレースしているように見える、私はよくある質問から

tm_map(yourCorpus, function(x) iconv(enc2utf8(x), sub = "byte")) 
をソリューションを使用していることを見ていた何

https://stat.ethz.ch/pipermail/r-sig-mac/2012-July/009374.htmlを参照)Rは、このオペレーティングシステム上でコンパイルされていることを設定します

は私にこの警告を与えていた。

Warning message: 
it is not known that wchar_t is Unicode on this platform 

この私がenc2utf8機能にさかのぼります。悪いニュースは、これはだからここR.

私の基盤となるOSとしていない問題であるということです、私は周りの仕事としてやったことです:

tm_map(yourCorpus, function(x) iconv(x, to='UTF-8-MAC', sub='byte')) 

この力は、Macintosh上で、UTF8エンコーディングを使用するiconvがと再コンパイルする必要はありません。

1

公式FAQは私の状況で動作していないことのようです:

tm_map(yourCorpus, function(x) iconv(x, to='UTF-8-MAC', sub='byte')) 

は、最後に私はそれが&エンコーディング機能のために使用して作られた:

for (i in 1:length(dataSet)) 
{ 
    Encoding(corpus[[i]])="UTF-8" 
} 
corpus <- tm_map(dataSet, tolower) 
4

私はMac上とにこれを実行してきました私の欲求不満、私は解決するために(これらはつぶやきだった)ファウルレコードを特定しなければならなかった。上記で示唆したように、次の時間ので、レコードが同じであることの保証はありません、私は次の関数

tm_map(yourCorpus, function(x) iconv(x, to='UTF-8-MAC', sub='byte')) 

を使用しました。

これは、上記の回答の

39

なし私のために働いていない魅力のように働きました。この問題を回避する唯一の方法は、すべての非グラフィカル文字(http://stat.ethz.ch/R-manual/R-patched/library/base/html/regex.html)を削除することでした。

コードは、この単純な

usableText=str_replace_all(tweets$text,"[^[:graph:]]", " ") 
+1

解決策としてマークする必要があります。それは動作し、それは長年にわたって普及してきましたが、OPはそれが正しいとマークするために回りませんでした。 –

0

チャドのソリューションは、私のために働いていなかったです。私はこれを関数に埋め込み、iconvの入力にベクトルが必要であるというエラーを出していました。そこで、私はコーパスを作成する前に変換を行うことにしました。

myCleanedText <- sapply(myText, function(x) iconv(enc2utf8(x), sub = "byte")) 
9

は、私は問題は、TOLOWERが私のために動作しませんでした

#to remove emojis 
dataSet <- iconv(dataSet, 'UTF-8', 'ASCII') 
2

かつての提案を理解することができない絵文字であることを今では明らかだと思います。私はより多くを調査し、コード

corpus <- tm_map(corpus, PlainTextDocument)

感謝の行を使用して、プレーンテキスト形式に戻ってデータを変換することにより、以下のhttps://eight2late.wordpress.com/2015/05/27/a-gentle-introduction-to-text-mining-using-r/

#Create the toSpace content transformer 
toSpace <- content_transformer(function(x, pattern) {return (gsub(pattern," ", 
x))}) 
# Apply it for substituting the regular expression given in one of the former answers by " " 
your_corpus<- tm_map(your_corpus,toSpace,"[^[:graph:]]") 

# the tolower transformation worked! 
your_corpus <- tm_map(your_corpus, content_transformer(tolower)) 
関連する問題