wordVector static method

List<double> wordVector(
  1. String text
)

The default vector of text.

Implementation

static List<double> wordVector(String text) {
  final vector = List<double>.filled(vectorDimension, 0);
  vector[vectorDimension - 1] = 0.1;
  for (final match in _word.allMatches(text.toLowerCase())) {
    var hash = 0x811c9dc5;
    for (final byte in utf8.encode(match[0]!)) {
      hash = ((hash ^ byte) * 0x01000193) & 0xffffffff;
    }
    vector[hash % (vectorDimension - 1)] += 1;
  }
  final length = math.sqrt(
    vector.fold<double>(0, (sum, value) => sum + value * value),
  );
  return [for (final value in vector) value / length];
}