0
を使用して単語をマッピング

私はPySparkと私の旅を始めていますし、私は私はこのようなコードを持っている例: ための一点で立ち往生している:(私はhttps://spark.apache.org/docs/2.1.0/ml-features.htmlからそれを取った)PySpark:トークナイザ

from pyspark.ml.feature import Tokenizer, RegexTokenizer 
from pyspark.sql.functions import col, udf 
from pyspark.sql.types import IntegerType 

sentenceDataFrame = spark.createDataFrame([ 
    (0, "Hi I heard about Spark"), 
    (1, "I wish Java could use case classes"), 
    (2, "Logistic,regression,models,are,neat") 
], ["id", "sentence"]) 

tokenizer = Tokenizer(inputCol="sentence", outputCol="words") 

regexTokenizer = RegexTokenizer(inputCol="sentence", outputCol="words", pattern="\\W") 
# alternatively, pattern="\\w+", gaps(False) 

countTokens = udf(lambda words: len(words), IntegerType()) 

tokenized = tokenizer.transform(sentenceDataFrame) 
tokenized.select("sentence", "words")\ 
    .withColumn("tokens", countTokens(col("words"))).show(truncate=False) 

regexTokenized = regexTokenizer.transform(sentenceDataFrame) 
regexTokenized.select("sentence", "words") \ 
    .withColumn("tokens", countTokens(col("words"))).show(truncate=False) 

そして、

test = sqlContext.createDataFrame([ 
    (0, "spark"), 
    (1, "java"), 
    (2, "i") 
], ["id", "word"]) 

出力は次のとおりです:

id |sentence       |words          |tokens| 
+---+-----------------------------------+------------------------------------------+------+ 
|0 |Hi I heard about Spark    |[hi, i, heard, about, spark]    |5  | 
|1 |I wish Java could use case classes |[i, wish, java, could, use, case, classes]|7  | 
|2 |Logistic,regression,models,are,neat|[logistic, regression, models, are, neat] |5  | 

アムI P私はこのような何かを追加していossibleはこのような何かを達成するために: [ID「試験」から、同上から「regexTokenized」]トークン化の言葉」がマップすることができます「テスト」私は「regexTokenized」からIDをGRAPすることができますから、リストから

2, 0 
2, 1 
1, 1 
0, 1 

両方のデータセットの中で? または別の解決策をとるべきでしょうか?事前に

は、任意のヘルプにありがとう:)

答えて

0

explodejoin

from pyspark.sql.functions import explode 

(testTokenized.alias("train") 
    .select("id", explode("words").alias("word")) 
    .join(
     trainTokenized.select("id", explde("words").alias("word")).alias("test"), 
     "word"))