-1

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

Giorgio Robino
  • 2,148
  • 6
  • 38
  • 59
  • 1
    You can take a look on npm to see if there are any libraries that seem useful. There is e.g. stopword, which removes certain "filler-words". Is also has support for Italian (https://www.npmjs.com/package/stopword). You'll still need to replace special characters - maybe this can be used for inspiration https://stackoverflow.com/questions/286921/efficiently-replace-all-accented-characters-in-a-string. finally, you'll need to replaces spaces and shorten the string, which you can use the answer from @user13124036. – abondoa Mar 25 '20 at 23:36
  • oh thanks! Goot tips : 1. remove stopwords (that package seems perfect); 2. sanitize special chars; 3. abbreviate words. 4. compact all :) – Giorgio Robino Mar 26 '20 at 14:22

1 Answers1

0

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'