gpt4 book ai didi

javascript - 将数组中的字符串拆分为子数组

转载 作者:行者123 更新时间:2023-11-30 05:50:30 26 4
gpt4 key购买 nike

我在尝试将数组中的字符串拆分为子数组然后将其推回原始数组时遇到问题。

我得到错误:

TypeError: Cannot find function split in object

这是我的代码:

// Split chapter into scenes
for(c in files){
var ID = files[c].getId();
var chapter = DocumentApp.openById(ID);
var text = chapter.getText();

// Splits chapter into scenes and pushes them to scenes
scenes.push(text.split("*"));

// Splits scenes into n,s,b
for(s in scenes){
scenes[s] = scenes[s].split("~");
}

// Push in scene wordcount
for(b in scenes){
var wordCount = scene[b][2].split(" ");
scenes[b].push(wordCount.length);
}
}

脚本导入的章节文档格式如下:

Scene Title
~
Summary of scene
~
Body of the scene...yeah.

*

To the Galaxy
~
And so it goes...
~
And so it goes like this that everything was this and that. Right.

*

The End
~
This is the end.
~
And so it goes like this that everything was this and that. Right. The end.

我有两个数组,一个叫做 chapters ,它包含章节编号、名称和总字数,然后是场景,它包含特定章节中的所有场景(也分为场景名称、摘要、正文和字数)。使用 DocsList 函数将章节标题导入章节数组。

我从驱动器中抓取特定文档中的一章文本,并首先使用字符 * 将其拆分为场景。

然后,在一个完美的世界中,我将逐步完成场景数组,将对象数组中的每个字符串拆分为 s 个子数组,该子数组使用字符 ~ 在子数组中分隔场景编号、场景名称和场景主体,然后推送将该数组转换为原始数组。

所以,如果

scenes = ["The Beginning ~ In the beginning, blah blah blah ~ Body of the scene goes here", "The Beginning ~ In the beginning, blah blah blah ~ Body of the scene goes here"]

处理后会变成:

scenes = [["The beginning", "In the beginning, blah blah blah", "Body of the scene goes here"], ["The beginning", "In the beginning, blah blah blah", "Body of the scene goes here"]]

问题是,当我将章节文档拆分为场景时,它似乎将场景作为对象而不是字符串插入数组,这使得无法进一步拆分场景数组中的字符串。

最佳答案

这样的事情会如你所愿吗?

function test(){ // replace this with the DocumentApp that gets your chapters
var chapter = "Scene Title~Summary of scene~Body of the scene...yeah.*To the Galaxy~And so it goes...~And so it goes like this that everything was this and that. Right.*The End~This is the end.~And so it goes like this that everything was this and that. Right. The end."
Logger.log(splitText(chapter))
}


function splitText(chapter){
var temp = []
var text = []
var scenes=[]
var wordCount
// Splits chapter into scenes and pushes them to scenes
temp=(chapter.split("*"));

for(t in temp){
text = temp[t].split("~")
Logger.log(t+' '+text)
wordCount = temp[t].split(" ").length
text.push(wordCount)
scenes.push(text)
}
return scenes
}

记录结果:

0  Scene Title,Summary of scene,Body of the scene...yeah.
1 To the Galaxy,And so it goes...,And so it goes like this that everything was this and that. Right.
2 The End,This is the end.,And so it goes like this that everything was this and that. Right. The end.
[[Scene Title, Summary of scene, Body of the scene...yeah., 7.0], [To the Galaxy, And so it goes..., And so it goes like this that everything was this and that. Right., 18.0], [The End, This is the end., And so it goes like this that everything was this and that. Right. The end., 19.0]]

关于javascript - 将数组中的字符串拆分为子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15143801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com