ChemPal Documentation - v1.6.0
    Preparing search index...

    Function suggestAlternativeSearch

    • Suggests a simpler alternative search term when a query yields no results. Pulls PubChem's popularity-ranked synonyms for the query, keeps only simple common names (see isSimpleName), and skips any term the user has already tried. Falls back to the compound's CAS number when no suitable simple name remains. Either field may be undefined.

      Parameters

      • query: string

        The original (unsuccessful) search query

      • excluded: ReadonlySet<string>

        Lowercased terms to skip (e.g. previously searched, zero-result queries)

      Returns Promise<{ name?: string; cas?: string }>

      The best simple alternative name and/or a CAS fallback

      await suggestAlternativeSearch("2-propanone", new Set(["2-propanone"]));
      // Returns: { name: "acetone", cas: "67-64-1" }
      export async function suggestAlternativeSearch(
      query: string,
      excluded: ReadonlySet<string>,
      ): Promise<{ name?: string; cas?: string }> {
      const ranked = await getRankedNamesByName(query);
      if (!ranked) return {};

      const queryLc = query.toLowerCase();
      const isSkipped = (value: string): boolean =>
      value.toLowerCase() === queryLc || excluded.has(value.toLowerCase());

      // PubChem returns synonyms in descending popularity. Only suggest a name from the few most
      // common ones — deeper entries are obscure (e.g. "Dimethyl ketone" for acetone) and just as
      // unlikely to yield results, so we'd rather offer the CAS than a name nobody recognizes.
      const match = ranked
      .slice(0, POPULARITY_WINDOW)
      .find((entry) => isSimpleName(entry) && !isSkipped(entry));
      const name = match ? formatSuggestedName(match) : undefined;
      const cas = ranked.find((entry) => isCAS(entry) && !excluded.has(entry.toLowerCase()));
      return { name, cas };
      }