gpt4 book ai didi

php - 用php中的点值替换单词

转载 作者:搜寻专家 更新时间:2023-10-30 23:39:16 25 4
gpt4 key购买 nike

我想获取用户输入的文本。我想将用户使用的单词与也具有值的单词数据库进行比较。

现在我制作了一个数组,将输入运行到数据库并返回值。我遇到的问题是它的顺序不正确。

    // GETTING THE ARRY OF WORDS FROM entry_body( $entry_text)
$entry_text = $request->only('entry_body');

// REMOVE THE KEY AND JUST GET THE STRING VALUE
$entry_text = array_pull($entry_text,"entry_body");
$entry_noTag = strip_tags($entry_text);

// CONVERTE THE STRING ARRAY
$entry_explode = explode(" ", $entry_noTag);

// COUNT THE LENTH OF THE WORDS AND DIVIDE IT BY 3
$entry_lenth = count($entry_explode);

$new_array = array();
// EACH WORD GETS ANALAYZED
$matched_words = Words::whereIn('word', $entry_explode)->get();
foreach($matched_words as $mw){
$new_array[]=$mw->word;
$value_array[$mw->word]=$mw->value;
}

$arrayOfWords = array();
foreach ($entry_explode as $word) {
if (in_array($word,$new_array)) {
$arrayOfWords['yes_words'][] = $word;
} else {
$arrayOfWords['no_word'][] = $word;
}
}

我还需要让单词按顺序分成 3 组。我这样做是为了让组按正确的顺序排列。

    $entry_math = number_format($entry_lenth / 3);


return array_chunk($entry_explode, $entry_math);

这是我得到的输出

     [
[
"Hey",
"Jude,",
"don't",
"make",
"it",
"bad",
"Take",
"a",
"sad",
"song"
],
[
"and",
"make",
"it",
"better",
"Remember",
"to",
"let",
"her",
"into",
"your"
],
[
"heart",
"Then",
"you",
"can",
"start",
"to",
"make",
"it",
"better."
]
]

我需要用从 $value_array 中获得的数字替换单词,但我不能 100% 确定我应该怎么做。

这就是 $value_array 的样子。

 {
"a": "0",
"and": "0",
"bad": "1",
"better": "2",
"can": "2",
"heart": "3",
"her": "3",
"hey": "3",
"into": "0",
"it": "2",
"let": "1",
"make": "1",
"remember": "3",
"sad": "1",
"song": "2",
"start": "3",
"take": "1",
"then": "2",
"to": "2",
"you": "2",
"your": "2"
}

最佳答案

你可以用简单的foreach来解决,

看这个例子,

考虑这个数组。

$input = ['hi', 'you', 'me', 'and','others'];

$database_input = [
'hi' => 1,
'you' => 5,
'me' => 4,
'and' => 3,
'others' => 2,
'some' => 6,
'extra' => 7,
'elements' => 8
];

得到一个数组输出

Array
(
[hi] => 1
[you] => 5
[me] => 4
[and] => 3
[others] => 2
)

你需要做的就是,

$input = ['hi', 'you', 'me', 'and','others'];

$database_input = [
'hi' => 1,
'you' => 5,
'me' => 4,
'and' => 3,
'others' => 2,
'some' => 6,
'extra' => 7,
'elements' => 8
];

$output = [];

foreach($input as $item) {
if(array_key_exists(strtolower($item), $database_input))
$output[strtolower($item)] = $database_input[strtolower($item)];
}

这是完成事情的基本逻辑。

我已经创建了适用于您的输入的 php 粘贴,请参见下面的链接:

http://sandbox.onlinephpfunctions.com/code/63c1a0eb669df43a92a15104340f528a76a2cfb0

关于php - 用php中的点值替换单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37105631/

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