Removing commas between columns in text data

5 visualizaciones (últimos 30 días)
Kim Maria Damiani
Kim Maria Damiani el 16 de Oct. de 2021
Comentada: Kim Maria Damiani el 16 de Oct. de 2021
I have a txt file which is the ouput of a lemmatizer, in the form
Sometimes, ,, I, use, commas, .
I, like, writing, ,, I, like, reading
How can I read it into a tokenizedDocument deleting the unneccessary commas between tokens? A simple approach would be
test=readlines('/path/to/file.txt')
test=strrep(test,',','')
test=tokenizedDocument(test)
but it would remove even the commas already present in the original text, while I'd like to preserve punctuation-

Respuesta aceptada

Walter Roberson
Walter Roberson el 16 de Oct. de 2021
test = {'Sometimes, ,, I, use, commas, .'
'I, like, writing, ,, I, like, reading'};
test = regexprep(test, {'(?<=[^,]),\s', '\s*,,', '\s+\.'}, {' ', ',', '.'})
test = 2×1 cell array
{'Sometimes, I use commas.' } {'I like writing, I like reading'}
Notice we had to have a special rule for periods. You have 'use, commas' which should almost certainly translate to 'use commas' (so comma space becomes space), but after that 'commas, .' should not become 'commas .' .
To put it another way, we cannot use the rule that comma space pair is to be deleted: that works for the comma space between the word 'commas' and the period, but it does not work for the comma space pair between 'use' and 'commas': if you tried to apply that rule then 'use, commas' would merge together to 'usecommas' .

Más respuestas (1)

Chunru
Chunru el 16 de Oct. de 2021
test = {'Sometimes, ,, I, use, commas, .'
'I, like, writing, ,, I, like, reading'};
test = regexprep(test, ',\s', ' ')
test = 2×1 cell array
{'Sometimes , I use commas .' } {'I like writing , I like reading'}

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by