Long sentence shortener to generate a readable file name

376 Views Asked by At

I'm looking for a way to shorten a sentence (a text of few lines) to produce a "readable" (not too long) file name.

The application scenario is a chatbot where user can submit a media, say a video, with some paired description text (a caption). The application would assign to the video a readable file name, to retrieve afterward the video by his file name.

Imagine a video paired with a more or less long text description of the scene, like by example:

const videoDescription = 'beautiful yellow flowers on foreground, with a background with countryside meadows and many cows'

How could I shorten the description above with a "suitable" short file name?

Ok, I could just give the sentence as a name, maybe something a bit sanitized, like:

const videoFileName = 'beautiful_yellow_flowers_on_foreground_with_a_background_with_countryside_meadows_and_many_cows.MP4'

but in that way I could exceed the 255 limit of file name size (e.g. on Linux)

Any idea for a shortener algo? Maybe I could build the shortened filename with word abbreviations? Maybe I could remove from sentence articles, prepositions, etc.?

BTW, a minor issue: I'm working with Italian language, so a bit of chars sanitize is required to produce good filenames.

Last but not least, I'd looking for JavaScript/Node.js code

1

There are 1 best solutions below

0
On

You can check if the length is larger than 255 and shorten if necessary. You should also check for duplicates and append -1, -2 and so on if necessary.

let filename='some_flowers_on_foreground_with_a_background_with_countryside_meadows_and_few_cows.MP4'
if(filename.length>255)
  filename=filename.slice(0,255-4)+'.MP4'