Skip to content

fix(sinafinance): match stock symbol in addition to name#1158

Merged
jackwener merged 3 commits intojackwener:mainfrom
Benjamin-eecs:fix/sinafinance-stock-match
Apr 23, 2026
Merged

fix(sinafinance): match stock symbol in addition to name#1158
jackwener merged 3 commits intojackwener:mainfrom
Benjamin-eecs:fix/sinafinance-stock-match

Conversation

@Benjamin-eecs
Copy link
Copy Markdown
Contributor

Description

The scoring function in sinafinance stock only compared user input against the Chinese display name (p[4] from suggest API), ignoring the symbol field (p[3]). This caused searches by ticker symbol to mismatch when the Chinese name doesn't contain the ticker.

For example, searching "AAPL" scored Apple Inc. at 0 (name="苹果" doesn't contain "aapl") while AAPLU scored 0.8 (name="AAPLU" contains "aapl"), returning wrong data.

The fix adds symbol matching with higher priority than name matching, so exact symbol matches (e.g. "aapl" === "aapl") score 1.0 and partial symbol matches rank above name-only matches.

Fixes #1157

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🌐 New site adapter
  • 📝 Documentation
  • ♻️ Refactor
  • 🔧 CI / build / tooling

Checklist

  • I ran the checks relevant to this PR
  • I updated tests or docs if needed
  • I included output or screenshots when useful

Documentation (if adding/modifying an adapter)

N/A

Screenshots / Output

Before:

$ opencli sinafinance stock AAPL
- Symbol: AAPLU
  Name: AAPLU
  Price: '0.0000'

After:

$ opencli sinafinance stock AAPL
- Symbol: AAPL
  Name: 苹果
  Price: '273.1700'
  Change: '7.0000'
  ChangePercent: 2.63%
  Open: '273.7400'
  High: '288.3600'
  Low: '192.2100'
  Volume: '43249201'
  MarketCap: 4.01T

Other markets verified no regression:

$ opencli sinafinance stock 贵州茅台    # A股 OK
$ opencli sinafinance stock 腾讯控股    # 港股 OK
$ opencli sinafinance stock TSLA       # 美股 OK
$ opencli sinafinance stock 比亚迪      # A股 OK

Copilot AI review requested due to automatic review settings April 23, 2026 05:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the sinafinance stock adapter’s best-match selection so user searches can match on ticker symbol as well as the Chinese display name, fixing incorrect results when searching by symbol (e.g., AAPL).

Changes:

  • Extend the scoring function to consider e.symbol in addition to e.name.
  • Prioritize exact symbol/name matches and add partial symbol matching before falling back to name-only matching.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread clis/sinafinance/stock.js
Comment on lines +89 to 93
if (s.includes(needle))
return needle.length / s.length;
if (n.includes(needle))
return needle.length / n.length;
return 0;
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scoring logic returns the symbol-based ratio as soon as s.includes(needle) is true, even when e.name would be a closer/better match (e.g., a short exact-ish display name but a longer symbol containing the needle). This can under-score an entry and change ranking unexpectedly. Consider computing symbolScore and nameScore separately and returning the better score (or compare a tuple like [isExactSymbol, hasSymbolMatch, score] to preserve symbol priority without penalizing better name matches).

Suggested change
if (s.includes(needle))
return needle.length / s.length;
if (n.includes(needle))
return needle.length / n.length;
return 0;
const symbolScore = s.includes(needle) ? needle.length / s.length : 0;
const nameScore = n.includes(needle) ? needle.length / n.length : 0;
return Math.max(symbolScore, nameScore);

Copilot uses AI. Check for mistakes.
Comment thread clis/sinafinance/stock.js
Comment on lines +82 to +90
// Pick best match: score by name/symbol similarity, tiebreak by market priority
const needle = key.toLowerCase();
const score = (e) => {
const n = e.name.toLowerCase();
if (n === needle)
const s = e.symbol.toLowerCase();
if (s === needle || n === needle)
return 1;
if (s.includes(needle))
return needle.length / s.length;
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fixes a regression-prone ranking behavior, but there isn’t a test exercising sinafinance stock symbol-vs-name matching (e.g., the AAPL vs AAPLU case from #1157). Consider adding an e2e regression test that runs opencli sinafinance stock AAPL -f json and asserts the returned Symbol is AAPL, with the same “expected Chinese site restriction” skip logic used elsewhere in the e2e suite.

Copilot uses AI. Check for mistakes.
The scoring function only compared user input against the Chinese
display name (p[4] from suggest API), so searching "AAPL" matched
"AAPLU" (score 0.8) over Apple Inc. whose name field is "苹果"
(score 0). Check the symbol field first for exact and partial matches.

Fixes jackwener#1157
@Benjamin-eecs Benjamin-eecs force-pushed the fix/sinafinance-stock-match branch from 5eb7231 to 4938e85 Compare April 23, 2026 05:50
Benjamin-eecs and others added 2 commits April 23, 2026 14:11
The index table only listed `news` for sinafinance. Added the other
three commands (`rolling-news`, `stock`, `stock-rank`) and updated
the mode from Public to hybrid since rolling-news and stock-rank
require a browser.

Fixes jackwener#1156
@jackwener jackwener merged commit a3d0185 into jackwener:main Apr 23, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: sinafinance stock mismatches US stocks when searching by ticker symbol

3 participants