An open API service indexing awesome lists of open source software.

https://github.com/longbridge/stock-parser

Stock parser
https://github.com/longbridge/stock-parser

Last synced: 7 months ago
JSON representation

Stock parser

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`.