2016-09-26 6 views
0

私はJuliaのtext mining moduleで実験しています。TextMiningのコーパスは、明示的な親タイプの変換が必要です

私は変換エラーを得たでCorpus機能を養う、すなわち(IがパイプラインコマンドにLazy可能パッケージを使用しています注意してください)

using Lazy, TextMining, DataArrays 

@>> @data(["hello","bro"]) map(StringDocument) Corpus 

->LoadError: MethodError: `convert` has no method matching convert(::Type{TextAnalysis.Corpus}, ::DataArrays.DataArray{TextAnalysis.StringDocument,1}) 
This may have arisen from a call to the constructor TextAnalysis.Corpus(...), 
since type constructors fall back to convert methods.WARNING: Error showing method candidates, aborted 

私は、コードのこの部分を持っているconvert(Vector{GenericDocument})を適用する必要があります作品:

@>> @data(["hello","bro"]) map(StringDocument) convert(Vector{GenericDocument}) Corpus 

ここCorpus機能があります:

type Corpus 
    documents::Vector{GenericDocument} 
    total_terms::Int 
    lexicon::Dict{Compat.UTF8String, Int} 
    inverse_index::Dict{Compat.UTF8String, Vector{Int}} 
    h::TextHashFunction 
end 

function Corpus(docs::Vector{GenericDocument}) 
    Corpus(
     docs, 
     0, 
     Dict{Compat.UTF8String, Int}(), 
     Dict{Compat.UTF8String, Vector{Int}}(), 
     TextHashFunction() 
    ) 
end 

Corpus(docs::Vector{Any}) = Corpus(convert(Array{GenericDocument,1}, docs)) 

私はここで何が欠けていますか?

+0

あなたはパイプラインパッケージを使用しているように見えます( Lazy.jl?)あなたの質問に集中するために、パイプライン構文を削除してください。または、少なくとも「Lazyを使用する」(またはこれまでのモジュールを含む)場合は、コードがコンパイルされます。 –

答えて

0

コードには2つの問題があります。

  1. @data TextAnalysisコードはで何をすべきかわからない、DataArrayを作成します。明示的な変換を書くことは、これを処理するための完全に受け入れられる方法です。普通の配列ではなく、DataArraysを使用している特別な理由はありますか?

  2. @dataを削除しても、コードは現在失敗します。それは、Corpusのコンストラクタがパラメータとして取ることができるものが限られているからです(そして、Julia配列は共変型ではないという事実)。

もう1つの回避策は、明示的に変換することです。適切な修正は、新しいコンストラクタを以下のように定義することです。これはおそらく、パッケージに追加する必要がありますが、あなたは直接あなたのREPLに次のコードを使用することができます。これに

TextAnalysis.Corpus{T<:AbstractDocument}(docs::Vector{T}) = TextAnalysis.Corpus(convert(Array{GenericDocument,1}, docs)) 

、あなたが行うことができます:

julia> @>> ["hello","bro"] map(StringDocument) Corpus 
A Corpus 
関連する問題