sentencepiece.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const sentencepiece = {};
  2. sentencepiece.ModelFactory = class {
  3. async match(context) {
  4. const tags = await context.tags('pb');
  5. if ((tags.size >= 3 && tags.size <= 5 &&
  6. tags.get(1) === 2 && tags.get(2) === 2 && tags.get(3) === 2) &&
  7. Array.from(tags).every(([key, value]) => (key <= 5 && value === 2))) {
  8. const model = await context.tags('pb+');
  9. if (model &&
  10. model['1'] && model['1']['1'] === 2 && model['1']['2'] === 5 && model['1']['3'] === 0 &&
  11. model['2'] && model['2']['4'] === 0) {
  12. return context.set('sentencepiece');
  13. }
  14. }
  15. return null;
  16. }
  17. async open(context) {
  18. sentencepiece.proto = await context.require('./sentencepiece-proto');
  19. sentencepiece.proto = sentencepiece.proto.sentencepiece;
  20. let model = null;
  21. try {
  22. const reader = await context.read('protobuf.binary');
  23. model = sentencepiece.proto.ModelProto.decode(reader);
  24. } catch (error) {
  25. const message = error && error.message ? error.message : error.toString();
  26. throw new sentencepiece.Error(`File format is not sentencepiece.ModelProto (${message.replace(/\.$/, '')}).`);
  27. }
  28. return new sentencepiece.Model(model);
  29. }
  30. };
  31. sentencepiece.Model = class {
  32. constructor(model) {
  33. this.format = 'SentencePiece';
  34. this.modules = [new sentencepiece.Graph(model)];
  35. }
  36. };
  37. sentencepiece.Graph = class {
  38. constructor(model) {
  39. this.inputs = [];
  40. this.outputs = [];
  41. this.nodes = [];
  42. for (const [name, value] of Object.entries(model)) {
  43. const node = new sentencepiece.Node(name, value);
  44. this.nodes.push(node);
  45. }
  46. }
  47. };
  48. sentencepiece.Argument = class {
  49. constructor(name, value) {
  50. this.name = name;
  51. this.value = value;
  52. }
  53. };
  54. sentencepiece.Node = class {
  55. constructor(name, obj) {
  56. this.name = name;
  57. this.inputs = [];
  58. this.outputs = [];
  59. this.attributes = [];
  60. if (Array.isArray(obj)) {
  61. const type = new Set(obj.map((value) => value.constructor.name));
  62. this.type = { name: `${Array.from(type)[0]}[]` };
  63. const attribute = new sentencepiece.Argument(name, obj);
  64. this.attributes.push(attribute);
  65. } else {
  66. this.type = { name: obj.constructor.name };
  67. for (const [name, value] of Object.entries(obj)) {
  68. const data = ArrayBuffer.isView(value) ? Array.from(value) : value;
  69. const attribute = new sentencepiece.Argument(name, data);
  70. this.attributes.push(attribute);
  71. }
  72. }
  73. }
  74. };
  75. sentencepiece.Error = class extends Error {
  76. constructor(message) {
  77. super(message);
  78. this.name = 'Error loading SentencePiece model.';
  79. }
  80. };
  81. export const ModelFactory = sentencepiece.ModelFactory;