简体   繁体   English

使用功能性javascript和ramda查找和替换

[英]Find and replace using functional javascript and ramda

I am looking into ramda and have t questions, from the following Ramda REPL : 我正在调查ramda,并且对以下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? 我如何使用groupBy使其按段落分组,而不是上述方法?

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' ? 您将如何用replace = 'Arthuro stopped'替换这些项目?

Any advise is much appreciated. 任何建议深表感谢。

One way to solve your first problem with Ramda would be like this: 解决Ramda的第一个问题的方法是这样的:

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 . 您专门询问了groupBy After the groupBy(prop('paragraph')) clause, the data looks like this: groupBy(prop('paragraph'))子句之后,数据如下所示:

{
  "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 . 您可以在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. 对于问题的后半部分,建议您打开一个新主题,以说明您到目前为止已经尝试过的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM