如何进行异步语句嵌入计算?

时间:2019-06-30 09:48:50

标签: angular nlp tensorflow.js

尝试编写一个服务,该服务运行来自tensorflow.js的句子编码,该服务比较两个句子并返回余弦相似度。但是,点积永远不会正确地应用于正确的句子嵌入。

我尝试不使用Angular组件进行编码,并且可以在HTML脚本标签中使用。

compare(text1: string, text2: string): number {
    let similarityScore = -1;
    UniversalSentenceEncoder.load().then(model => {
      const sentences = [text1, text2];
      model.embed(sentences).then(embeddings => {
        embeddings.array().then(embeddingsArray => {
          similarityScore = Math.round(
            math.dot(embeddingsArray[0], embeddingsArray[1]) * 100
          );
        });
      });
    });
    return similarityScore;
  }

it("should compare two text strings", () => {
    const service: SemanticSimilarityService = TestBed.get(
      SemanticSimilarityService
    );
    expect(service).toBeTruthy();
    expect(service.compare("This is a test!", "This is a test!")).toEqual(
      100,
      "Similarity Score is not 100 for exact same strings."
    );
  });

HeadlessChrome 76.0.3803 (Linux 0.0.0) SemanticSimilarityService should compare two text strings FAILED
        Error: Expected -1 to equal 100.
            at <Jasmine>
            at UserContext.<anonymous> (src/app/semantic-similarity.service.spec.ts:20:67)
            at ZoneDelegate.invoke (node_modules/zone.js/dist/zone-evergreen.js:359:1)
            at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/zone-testing.js:308:1)
HeadlessChrome 76.0.3803 (Linux 0.0.0): Executed 6 of 11 (1 FAILED) (skipped 5) (0 secs / 0.575 secs)
HeadlessChrome 76.0.3803 (Linux 0.0.0) SemanticSimilarityService should compare two text strings FAILED
        Error: Expected -1 to equal 100.
            at <Jasmine>
            at UserContext.<anonymous> (src/app/semantic-similarity.service.spec.ts:20:67)
            at ZoneDelegate.invoke (node_modules/zone.js/dist/zone-evergreen.js:359:1)
HeadlessChrome 76.0.3803 (Linux 0.0.0): Executed 6 of 11 (1 FAILED) (skipped 5) (0.672 secs / 0.575 secs)

0 个答案:

没有答案