0

I am looking into ramda and have t questions, from the following Ramda REPL:

const R = require('ramda')
//const original = require('./test/fixtures/original.json')
const original = {
  speakers: {
    name: 'Author',
    words: [
      { duration: '0.360000', name: 'Arthur', time: '0.660000',   paragraph: 'p0-0' },
      { duration: '0.150000', name: 'the',    time: '1.020000',   paragraph: 'p0-0' },
      { duration: '0.380000', name: 'rat',    time: '1.170000',   paragraph: 'p0-0' },
      { duration: '0.770000', name: '.',      time: '1.550000',   paragraph: 'p0-0' },
      { duration: '0.360000', name: 'Arthur', time: '89.820000',  paragraph: 'p1-0' },
      { duration: '0.390000', name: 'stood',  time: '90.180000',  paragraph: 'p1-0' },
      { duration: '0.090000', name: 'and',    time: '90.570000',  paragraph: 'p1-0' }
    ]
  }
}

const words = R.lensPath(['speakers', 'words'])
const wordsList = R.view(words, original)
const result = [...wordsList.reduce((hash, { duration, time, name, paragraph }) => {
  const current = hash.get(paragraph) || { paragraph, words: [] };

  current.words.push({ duration, time, name });

  return hash.set(paragraph, current);
}, new Map).values()];

console.log(result);

How do I use groupBy in order to get it grouped by paragraphs instead of the above method?

From this result, how do I do a find of a string that can have one or more items, by sorting all the words in order of time and returning only the matched paragraphs?

search_criteria = 'Arthur stood';

results = [ 
  { para: 'p1-0', 
    words: [
      { 'name': 'Arthur', 'time': 89.820000},
      { 'name': 'stood', 'time': 90.180000}
  }
]

How would you replace these items with replace = 'Arthuro stopped'?

Any advise is much appreciated.

1
  • Question is not clear. You need to ask short and concise questions and create a separate question for each problem. Commented Aug 3, 2019 at 8:13

1 Answer 1

1

One way to solve your first problem with Ramda would be like this:

pipe(
  view(lensPath(['speakers', 'words'])),  // original.speakers.words (null-safe)
  groupBy(prop('paragraph')),             // object with keys of para name, values of list of matching records
  map(map(dissoc('paragraph'))),          // removing unneeded para from records
  toPairs,                                // {p1: [...words1], p2: [...words2]} => [[p1, [...words1]], [p2, [...words2]]] 
  map(zipObj(['paragraph', 'words']))     // expected output structure
)(original)

You asked specifically about groupBy. After the groupBy(prop('paragraph')) clause, the data looks like this:

{
  "p0-0": [
    {duration: "0.360000", name: "Arthur", paragraph: "p0-0", time: "0.660000"},
    {duration: "0.150000", name: "the", paragraph: "p0-0", time: "1.020000"},
    {duration: "0.380000", name: "rat", paragraph: "p0-0", time: "1.170000"},
    {duration: "0.770000", name: ".", paragraph: "p0-0", time: "1.550000"}
  ],
  "p1-0": [
    {duration: "0.360000", name: "Arthur", paragraph: "p1-0", time: "89.820000"},
    {duration: "0.390000", name: "stood", paragraph: "p1-0", time: "90.180000"},
    {duration: "0.090000", name: "and", paragraph: "p1-0", time: "90.570000"}
  ]
}

The remainder is just fiddling to get the output structure you're looking for.

You can see the results in the Ramda REPL. (If you comment out later lines in the pipe, you can see any intermediate results.)

For the second half of your question, I'd suggest you open a new topic, demonstrating what you've attempted so far.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.