1

私は句読点を取り除き機能を構築するためのタスク「removePunctuation」を持っており、その結果として、このテストに合格:Columnの名前を取得する方法、または既存の名前を変更する方法はありますか?

# TEST Capitalization and punctuation (4b) 
testPunctDF = sqlContext.createDataFrame([(" The Elephant's 4 cats. ",)]) 
testPunctDF.show() 
Test.assertEquals(testPunctDF.select(removePunctuation(col('_1'))).first()[0], 
        'the elephants 4 cats', 
        'incorrect definition for removePunctuation function') 

これは私が書くように管理するものです。

def removePunctuation(column): 
    """Removes punctuation, changes to lower case, and strips leading and trailing spaces. 

    Note: 
     Only spaces, letters, and numbers should be retained. Other characters should should be 
     eliminated (e.g. it's becomes its). Leading and trailing spaces should be removed after 
     punctuation is removed. 

    Args: 
     column (Column): A Column containing a sentence. 

    Returns: 
     Column: A Column named 'sentence' with clean-up operations applied. 
    """ 

    return lower(trim(regexp_replace("column_name", "[\W_]+"," "))).alias("sentence"); 

しかし、私はまだ関数regexp_replaceを "sentence"という別名で使用することはできません。私はこのエラーを取得しています:

AnalysisException: u"cannot resolve 'sentence' given input columns: [_1];"

答えて

1

私がしようとするだろう:

ボンネットの下に を使用しています
stringWithPunctuation.translate(None, string.punctuation) 

、単に効率の面で最高!


あなたの試み:

return lower(trim(regexp_replace(, "[\W_]+"," "))).alias("sentence"); 

は、エラーを説明するかもしれない、どこかのパラメータcolumnを使用していないようです。

+0

ああ、私が投稿したコードにはエラーがあります。regexp_replace()の最初の引数にはbean "column_name"が必要ですが、とにかく既に解決しましたが、ありがとうございます。 –

+0

@DmitrijKostyushkoあなたがそれを解決してうれしい!あなたの質問のコードがあなたが使っていたものではなかったことを私が知っていれば、私はより良い質問を投稿できたかもしれません。後で答えを受け入れることを忘れないでください。 ;) – gsamaras

1

私は驚いたことに、列名の代わりにregexp_replace() argsに列オブジェクトを渡すことができました。

関連する問題