https://github.com/longbridge/stock-parser
Stock parser
https://github.com/longbridge/stock-parser
Last synced: 7 months ago
JSON representation
Stock parser
- Host: GitHub
- URL: https://github.com/longbridge/stock-parser
- Owner: longbridge
- Created: 2023-02-17T10:43:59.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-25T03:19:27.000Z (almost 2 years ago)
- Last Synced: 2025-01-10T04:13:07.503Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 80.1 KB
- Stars: 1
- Watchers: 10
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Stock Parser
This is [Ticker symbol](https://en.wikipedia.org/wiki/Ticker_symbol) parse example to show how to write a parser in Go and Rust (Pest, Nom, rust-peg).
And run benchmarks to compare the performance.
## Usage
```go
// social-service example
package main
import (
"fmt"
stock_parser "github.com/longbridgeapp/stock-parser"
)
func main() {
out := Parse("看好 $BABA 和 $XPEV 未来有好的增长")
fmt.Println(out)
// 看好 $阿里巴巴.US 和 $X小鹏汽车.US 未来有好的增长
}
func Parse(ctx context.Context, body string) string {
// preproccess
out := body
body = preprocess(body)
// Use with StockInfo SDK
_ = stock_parser.Parse(body, func(code, market, match string) string {
if stock, ok := StockInfoSDK.GetCounterId(code, market); ok {
s := fmt.Sprintf(`$%s.%s`, stock.CounterId, stock.Name, stock.Name, stock.Market)
// 替换
out = strings.ReplaceAll(out, match, s)
}
return out
})
return out
}
```
### Benchmark in Go
```
Benchmark-8 489860 2729 ns/op
Benchmark_long-8 64664 21624 ns/op
Benchmark_large-8 1910 589914 ns/op
```
## Development in Go
Use [https://github.com/pointlander/peg](https://github.com/pointlander/peg)
```sh
go install github.com/pointlander/peg
```
And then run `make` to generate `grammar.peg` into `grammar.go`.
> NOTE: Please do not change `grammar.go`.