TensorFlow.js - 在JavaScript中是否有与tokenizer等效的方法?

3

我正在用Python构建一个NLP分类器,并希望为演示构建一个托管HTML页面。 我想通过对文本进行分词,然后在预测之前对其进行填充来测试预测结果。就像这样:

tf.tokenizer.texts_to_sequences(text)
token_list = tf.tokenizer.texts_to_sequences([text])[0]
token_list_padded = pad_sequences([token_list], maxlen=max_length, padding=padding_type)

问题在于我对JavaScript不是很熟悉,那么JavaScript中是否有像Python一样的分词和填充方法呢?


你可能想看一下 https://ml5js.org/ 这是一个建立在tensorflow之上的js库。 - JDunken
我认为ml3js相当新,并不支持NLP中的函数,如tokenizerpad_sequences - ezzeddin
3个回答

2

目前JavaScript中还没有像Python中那样的tf.tokenizer

这里描述了一个简单的js.tokenizer here。更健壮的方法是使用universal sentence encoder附带的分词器。


1

在Javascript中没有原生的标记化机制。

您可以使用Javascript库,例如naturalwink-tokenizerwink-nlp。最后一个库会自动提取一些标记特征,这些特征可能对训练有用。


0
你现在可以使用 `gpt-tokenizer`,这里有一个使用 npm 的示例。
import {
  encode,
  encodeChat,
  decode,
  isWithinTokenLimit,
  encodeGenerator,
  decodeGenerator,
  decodeAsyncGenerator,
} from 'gpt-tokenizer'

const text = 'Hello, world!'
const tokenLimit = 10

// Encode text into tokens
const tokens = encode(text)

// Decode tokens back into text
const decodedText = decode(tokens)

// Check if text is within the token limit
// returns false if the limit is exceeded, otherwise returns the actual number of tokens (truthy value)
const withinTokenLimit = isWithinTokenLimit(text, tokenLimit)

// Example chat:
const chat = [
  { role: 'system', content: 'You are a helpful assistant.' },
  { role: 'assistant', content: 'gpt-tokenizer is awesome.' },
]

// Encode chat into tokens
const chatTokens = encodeChat(chat)

// Check if chat is within the token limit
const chatWithinTokenLimit = isWithinTokenLimit(chat, tokenLimit)

// Encode text using generator
for (const tokenChunk of encodeGenerator(text)) {
  console.log(tokenChunk)
}

// Decode tokens using generator
for (const textChunk of decodeGenerator(tokens)) {
  console.log(textChunk)
}

// Decode tokens using async generator
// (assuming `asyncTokens` is an AsyncIterableIterator<number>)
for await (const textChunk of decodeAsyncGenerator(asyncTokens)) {
  console.log(textChunk)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接