反应 js 创建 json 文件,读取仅在句子的最后一点之后换行

Posted

技术标签:

【中文标题】反应 js 创建 json 文件,读取仅在句子的最后一点之后换行【英文标题】:React js creating json file with reading to wrap only after final point of the sentence 【发布时间】:2021-04-26 14:52:22 【问题描述】:

文件:

WEBVTT

00:00:00.000 --> 00:00:12.920
Hi.
Hi.

00:00:26.040 --> 00:00:26.960
Hi.

00:00:26.040 --> 00:00:26.960
Hi.Hi

00:00:26.040 --> 00:00:26.960
Hi..Hi

00:00:26.040 --> 00:00:26.960
Hi...Hi.

我的结果 json:

[
    
        "timeString": "00:00:00.000 --> 00:00:12.920",
        "from": "00:00:00.000",
        "to": "00:00:12.920",
        "string": "Hi.\nHi."
    ,
    
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi."
    ,
    
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi.Hi"
    ,
    
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi..Hi"
    ,
    
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi...Hi."
    
]

Json 结果预期:

[
    
        "timeString": "00:00:00.000 --> 00:00:12.920",
        "from": "00:00:00.000",
        "to": "00:00:12.920",
        "string": "Hi.\nHi."
    ,
    
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi."
    ,
    
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi.\nHi" <- different
    ,
    
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi..\nHi" <- different
    ,
    
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi...\nHi." <- different
    
]

代码:

export default function App() 
  fetch("/file.txt")
    .then((r) => r.text())
    .then((text) => 
      const v = text
        .replace("WEBVTT", "")
        .replace(/[\r\n]2,/g, "\n")
        .replace("\n", "");
      const lines = v.split("\n");

      let inc = -1;
      const sub = lines.reduce((acc, d, index, array) => 
        const test = new RegExp("\\b(\\d2:\\d2:\\d2)\\.(\\d3)\\b").test(
          d
        );
        if (test) 
          inc++;
          const a = d.split("-->").filter((e) => e !== "-->");
          acc.push( timeString: d, from: a[0].trim(), to: a[1].trim() );
         else 
          let a = acc[inc]?.string;
          if (a !== undefined) a += `\n$d`;
          else a = d;
          acc[inc] =  ...acc[inc], string: a ;
        
        return acc;
      , []);
      console.log(sub);
    );

  return <div className="App"></div>;

我想得到的结果是,只要在文件中作为文本,所以字符串字段中的单词或短语以句号结尾,它就会被放入\n,如我希望的结果所示,但是必须考虑以下情况。

    如果点后面没有任何内容,则无需输入\n。 如果在一个周期之后还有另一个周期,你必须把\n放在最后一个周期之后,例如当有两个或三个暂停点时(就像我在文件中考虑的最后两种情况)。

链接:https://codesandbox.io/s/zealous-robinson-ov34m?file=/src/App.js

你能帮帮我吗?

【问题讨论】:

【参考方案1】:

Negative lookaheads 可能是一个解决方案。

以下内容将单个. 的序列替换为.\n,但前提是它后面没有. 或新行(\n) 中的任何一个,并且后面没有序列结束(@ 987654327@):

const finder = /\.(?![\n.])(?!$)/g;
const mangle = (str) => str.replace(finder, '.\n');

console.log(mangle('Hi.\nHi.')); // "Hi.\nHi."
console.log(mangle('Hi.'));      // "Hi."
console.log(mangle('Hi.Hi'));    // "Hi.\nHi"
console.log(mangle('Hi..Hi'));   // "Hi..\nHi"
console.log(mangle('Hi...Hi.')); // "Hi...\nHi."

【讨论】:

谢谢,codesandbox.io/s/eloquent-sanderson-4d1tq?file=/src/App.js

以上是关于反应 js 创建 json 文件,读取仅在句子的最后一点之后换行的主要内容,如果未能解决你的问题,请参考以下文章

错误:防止写入仅在大小写或查询字符串方面与已写入文件不同的文件。反应js laravel

js中怎么获取json中的key,value值?

异步/等待仅在读取文件时才是有效的异步函数[重复]

使用 RNFS 在本机反应上读取 Json 文件

06 js 读取 json 文件

VueJS - 从本地 json 文件中读取数据到 vis.js 时间轴