Pyspark - токенизация предложения в столбце строки данных - PullRequest
0 голосов
/ 24 апреля 2020

Допустим, у меня есть фрейм данных

s2=spark.createDataFrame(
    [
        (1, 'this is sentence 1. This is sentence 2. Finally, this is sentence 3.'), 
        (2, 'the car is running. who is washing the dishes.    the arrow is on the air.'),
    ],
    ['id', 'txt'] 
)

Что мне нужно, так это токенизировать предложения каждой строки столбца txt. Например, у меня есть код:

from nltk import sent_tokenize
def sent_TokenizeFunct(x):
    return sent_tokenize(str(x) , language='english')

sent_tokeniz_udf = udf(sent_TokenizeFunct, StringType())

Затем:

s2.withColumn('sent_tokenz',sent_tokeniz_udf(s2['txt']))

Я получаю вывод для столбца sent_tokenz

[Row(sent_tokenz='[this is sentence 1., This is sentence 2., Finally, this is sentence 3.]'),
 Row(sent_tokenz='[the car is running., who is washing the dishes., the arrow is on the air.]')]

Что не что мы хотим. Это было бы что-то вроде

[Row(sent_tokenz='["this is sentence 1.", "This is sentence 2.", "Finally, this is sentence 3."]'),
 Row(sent_tokenz='["the car is running.", "who is washing the dishes.", "the arrow is on the air."]')]

Любая помощь в том, что происходит, приветствуется.

...