2014-10-02 39 views
3

介意,我在F#和R(更何況)一個初學者,所以可能指向我RTFM或以其他方式;-)F#,R提供商,R包TM和(幾乎)奧維例如

我已經開始用R來研究一些文本數據挖掘,以及包tm。

我在R中有以下腳本,它的方式非常類似於Ovid分析的示例(用「txt」替換實際示例中的「lgtext」,language =「lat」替換語言=「no」使用Ovid示例運行它):

library(tm) 
library(SnowballC) 

txt <- system.file("texts", "lgtextfull", package = "tm") 
(lgorg <- VCorpus(DirSource(txt, encoding = "UTF-8"), 
      readerControl = list(language = "no"))) 

lg <- tm_map(lgorg , stripWhitespace) 

因此,作爲初學者,我使用了F#,R,Deedle和RPRovider。 Deedle我還沒有使用的又是那麼這可以被忽略不計......

我試着再寫出下面的F#:

#I "../packages/RProvider.1.0.17/" 

#load "RProvider.fsx" 

open RProvider 
open RDotNet 

open RProvider.``base`` 
open RProvider.tm 
open RProvider.openNLP 
open RProvider.SnowballC 

let txt = R.system_file("texts", "lgtextfull", package = "tm", lib_loc = null, mustWork=true) 
let lang = dict [("language", "no":>obj)] 
let readerControl = R.list(lang) 
let dirsource = R.DirSource(txt, encoding = "UTF-8") 

let lgorg = R.VCorpus(dirsource, readerControl) 

let lg = R.tm__map(lgorg, R.stripWhitespace) 

原因「擴展」將R劇本與其說是對我的理解和讓它起作用。

一些來回後,上面會運行和apperently它的工作原理和報告REPL相同R,到最後一行的exeption:

let lg = R.tm__map(lgorg, R.stripWhitespace) 

這給出了一個錯誤,如:

System.Exception: No converter registered for type [email protected] or any of its base types 
    at [email protected](String message) in c:\Tomas\Public\FSharp.RProvider\src\RProvider\RInterop.fs:line 164 
    at [email protected][b,c,d](String fmt, Int32 len, FSharpFunc`2 outputChar, FSharpFunc`2 outa, b os, FSharpFunc`2 finalize, FSharpList`1 args, Int32 i) 
    at [email protected][b,c,d](FSharpFunc`2 initialize, String fmt, Int32 len, FSharpList`1 args) 
    at [email protected][b,c,d](FSharpFunc`2 initialize, String fmt, Int32 len, FSharpList`1 args, Type ty, Int32 i) 
    at <StartupCode$FSharp-Core>[email protected](T1 inp) 
    at RProvider.RInteropInternal.REngine.SetValue(REngine this, Object value, FSharpOption`1 symbolName) in c:\Tomas\Public\FSharp.RProvider\src\RProvider\RInterop.fs:line 274 
    at RProvider.RInteropInternal.toR(Object value) in c:\Tomas\Public\FSharp.RProvider\src\RProvider\RInterop.fs:line 287 
    at [email protected](List`1 tempSymbols, Object arg) in c:\Tomas\Public\FSharp.RProvider\src\RProvider\RInterop.fs:line 445 
    at [email protected](IEnumerable`1& next) in c:\Tomas\Public\FSharp.RProvider\src\RProvider\RInterop.fs:line 453 
    at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1.MoveNextImpl() 
    at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1.System-Collections-IEnumerator-MoveNext() 
    at Microsoft.FSharp.Collections.SeqModule.ToArray[T](IEnumerable`1 source) 
    at RProvider.RInterop.callFunc(String packageName, String funcName, IEnumerable`1 argsByName, Object[] varArgs) in c:\Tomas\Public\FSharp.RProvider\src\RProvider\RInterop.fs:line 450 
    at RProvider.RInterop.call(String packageName, String funcName, String serializedRVal, Object[] namedArgs, Object[] varArgs) in c:\Tomas\Public\FSharp.RProvider\src\RProvider\RInterop.fs:line 494 
    at <StartupCode$FSI_0006>[email protected]() in C:\Users\helgeu\Documents\Visual Studio 2012\Projects\DisqusAnalyzer\DisqusAnalyzer.Lib\InteractiveSession.fsx:line 81 
Stopped due to error 

我必須承認,我對此一無所知,google.com也不幫我;-)

有人嗎?任何指針?它應該工作嗎?我做錯了嗎?

回答

2

我懷疑問題是tm__map的第二個參數是R函數。當你編寫R.stripWhitespace時,你會得到一個F#閉包,我們無法將其轉換回R函數。

一種解決方法可能是評估返回R函數作爲SymbolicExpression表達式,然後將其傳遞作爲參數:由Tomas Petricek提供

let stripWhite = R.eval(R.parse(text="stripWhitespace")) 
let lg = R.tm__map(lgorg, stripWhite) 
+0

現在我看到它解釋說它對我有意義。我還沒有看過它,但是我確實嘗試了一個像你所描述的那樣快的方法,而且它也沒有像預期的那樣工作。有一些成功,如stackoverflow.com/questions/28498549/...中所述,所以我會仔細研究並嘗試一些更多選項,並可能會發布一些跟進。我有強烈的懷疑,我試圖在F#中編寫R,而不是在F#中使用R。總之:THX! – 2015-02-17 07:44:38

0

解不起作用,因爲stripWhitespace函數來從tm包。爲了使這項工作,你應該使用函數的完全限定名稱:

let stripWhitespace= R.eval(R.parse(text="tm::stripWhitespace")) 
let lg = R.tm__map(lgorg, stripWhitespace) 

這將做到這一點。