gpt4 book ai didi

javascript - 有没有办法用javascript查找字符串来自哪个txt文件?

转载 作者:行者123 更新时间:2023-11-28 03:32:14 25 4
gpt4 key购买 nike

我有一个从 16 个 txt 文件中随机生成的句子,我正在尝试对该函数进行逆向工程,以便我可以看到哪个句子对应于哪个 txt 文件。我现在正在使用 if/else 但它非常慢,我想知道是否还有另一种方法可以做到这一点?我正在使用 p5.js 和 rita.js 库。

var lines, markov, data1, data2, data3, data4, data5, data6, data7,  x = 160, y = 240;
var mySound;
let myCanvas;
var randomImageLocation;
let img = [];
var index = 0;
var typed="";
var ellipsecolor = 44;
var timer = 10;
var nextTime = timer;
let button;
let anotherStrata;
let myfi;
let datta1;

function preload() {
data1 = loadStrings('assets/newtxt/1.txt');
data2 = loadStrings('assets/newtxt/2.txt');
data3 = loadStrings('assets/newtxt/three.txt');
data4 = loadStrings('assets/newtxt/four.txt');
data5 = loadStrings('assets/newtxt/five.txt');
data6 = loadStrings('assets/newtxt/six.txt');
data7 = loadStrings('assets/newtxt/seven.txt');
data8 = loadStrings('assets/newtxt/8.txt');
data9 = loadStrings('assets/newtxt/9.txt');
data10 = loadStrings('assets/newtxt/ten.txt');
data11 = loadStrings('assets/newtxt/11.txt');
data12 = loadStrings('assets/newtxt/12.txt');
data13 = loadStrings('assets/newtxt/13.txt');
data14 = loadStrings('assets/newtxt/14.txt');
data15 = loadStrings('assets/newtxt/15.txt');
data16 = loadStrings('assets/newtxt/16.txt');
img[0] = loadImage("img/img0.png");
img[1] = loadImage("img/img1.png");
bg = loadImage("img/background.jpg");
}


function setup() {
let createcanvas = createCanvas(window.innerWidth /2 , window.innerHeight - 150);
createcanvas.parent('mycanvas');
bg = loadImage("img/background.jpg");
textFont('avenir');
textSize(20 + (mouseX / width)*72);
frameRate(30);
fill('beige');
lines = ["click to place your order!"];

// create a markov model w' n=4
markov = new RiMarkov(8);

datta1 = data1.join(' ');
datta2 = data2.join(' ');
datta3 = data3.join(' ');
datta4 = data4.join(' ');
datta5 = data5.join(' ');
datta6 = data6.join(' ');
datta7 = data7.join(' ');
datta8 = data8.join(' ');
datta9 = data9.join(' ');
datta10 = data10.join(' ');
datta11 = data11.join(' ');
datta12 = data12.join(' ');
datta13 = data13.join(' ');
datta14 = data14.join(' ');
datta15 = data15.join(' ');
datta16 = data16.join(' ');
// load text into the model
markov.loadText(data1.join(' '));
markov.loadText(data2.join(' '));
markov.loadText(data3.join(' '));
markov.loadText(data4.join(' '));
markov.loadText(data5.join(' '));
markov.loadText(data6.join(' '));
markov.loadText(data7.join(' '));
markov.loadText(data8.join(' '));
markov.loadText(data9.join(' '));
markov.loadText(data10.join(' '));
markov.loadText(data11.join(' '));
markov.loadText(data12.join(' '));
markov.loadText(data13.join(' '));
markov.loadText(data14.join(' '));
markov.loadText(data15.join(' '));
markov.loadText(data16.join(' '));
drawText();
}
function ellipsee(){
if (millis() > nextTime){
ellipsecolor +=1;
nextTime = millis() + timer;
}
}
function draw(){
image(anotherStrata, 0, 0)
line(mouseX, mouseY, pmouseX, pmouseY);
translate (mouseX, mouseY);
fill(ellipsecolor);
var value = random(0, 600);
ellipse(mouseX, mouseY, 0.4, 0.4);
}
function drawText() {
background(bg);
textSize(20 + (mouseX / width)*33);
myfi = text(lines.join(' '), 50, 201, 400, 530);
}

function scrollToBottom(id){
var div = document.getElementById(id);
div.scrollTop = div.scrollHeight - div.clientHeight;
}



//this function

function findTxtFile() {

var pf1 = datta1.includes(lines);
var pf2 = datta2.includes(lines);
var pf3 = datta3.includes(lines);
var pf4 = datta4.includes(lines);
var pf5 = datta5.includes(lines);
var pf6 = datta6.includes(lines);
var pf7 = datta7.includes(lines);
var pf8 = datta8.includes(lines);
var pf9 = datta9.includes(lines);
var pf10 = datta10.includes(lines);
var pf11 = datta11.includes(lines);
var pf12 = datta12.includes(lines);
var pf13 = datta13.includes(lines);
var pf14 = datta14.includes(lines);
var pf15 = datta15.includes(lines);
var pf16 = datta16.includes(lines);

let newa;
if (pf1==true){
newa = createA("assets/newtxt/1.txt", lines);
}
if (pf2==true){
newa = createA("assets/newtxt/2.txt", lines);
}
if (pf3==true){
newa = createA("assets/newtxt/three.txt", lines);
}
if (pf4==true){
newa = createA("assets/newtxt/four.txt", lines);
}
if (pf5==true){
newa = createA("assets/newtxt/five.txt", lines);
}
if (pf6==true){
newa = createA("assets/newtxt/six.txt", lines);
}
if (pf7==true){
newa = createA("assets/newtxt/seven.txt", lines);
}
if (pf8==true){
newa = createA("assets/newtxt/8.txt", lines);
}
if (pf9==true){
newa = createA("assets/newtxt/9.txt", lines);
}
if (pf10==true){
newa = createA("assets/newtxt/ten.txt", lines);
}
if (pf11==true){
newa = createA("assets/newtxt/11.txt", lines);
}
if (pf12==true){
newa = createA("assets/newtxt/12.txt", lines);
}
if (pf13==true){
newa = createA("assets/newtxt/13.txt", lines);
}
if (pf14==true){
newa = createA("assets/newtxt/14.txt", lines);
}
if (pf15==true){
newa = createA("assets/newtxt/15.txt", lines);
}
if (pf16==true){
newa = createA("assets/newtxt/16.txt", lines);
}

newa.style('font-family', 'monospace');
newa.style('color', 'green');
newa.style('font-size', '12pt');
newa.parent('consolog');
newa.class('pulse');

let newb = createElement('br');
newb.parent('consolog');
scrollToBottom('consolog');
}
function mouseClicked() {
typed="";
x = 100;
y = 220;
lines = markov.generateSentences(1);
drawText();
mySound.play();
anotherStrata.clear();
findTxtFile();
}
function mouseReleased(){
index = index + 1;
if (index == img.length){
index = index - img.length;
}
}

最佳答案

在 p5.js match()分别matchAll()可用于查找 Regular expression 的出现或文本中的字符串。

查看示例:

function setup() {
createCanvas(400, 200);
}

function draw() {
var string = 'Hello World!';

var str1 = match(string, "World");
var str2 = match(string, "Dog");

textSize(50);
text(str1 ? str1 : "not found", 20, 70);
text(str2 ? str2 : "not found", 20, 130);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>

关于javascript - 有没有办法用javascript查找字符串来自哪个txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58072923/

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