Golang : Use NLP to get sentences for each paragraph example
Instead of reading ALL the news with human eyes to find out what's going on with the economy or market trend, it would be a good idea to get the computer to do the heavy lifting and only alert us humans on important news. The program can pickup certain verbs or person names( such as reserve banks governors ) and weight how important the news article is according to some criteria. If ... let say the news article hit certain scores, then generate an alert or trigger an action to either LONG or SHORT a trading instrument.
Below is part of a larger program that uses natural language processing(NLP) to break down a given news article into individual paragraph and each sentences in the paragraph.
Here you go!
package main
import (
"fmt"
"log"
"strings"
"gopkg.in/jdkato/prose.v2"
)
func main() {
text := `EURUSD’s uptrend may be accelerated if US consumer confidence comes in below the 131.0 estimate and Fed Chairman Jerome Powell’s economic outlook bolsters market expectations of rate cuts. Overnight index swaps are pricing in a 100 percent probability of a cut from the July meeting through year-end. However, rhetoric from the central bank has not indicated that policymakers are feeling dovish to that degree.
However, hawkish members of the Fed are finding it increasingly difficult to justify their position in light of US growth. Since February, economic activity out of the US has been broadly underperforming relative to economists’ expectations – signaling that analysts are over estimating the economy’s strength. Inflationary pressure has also been waning alongside a deterioration in global trade due to the US-China trade war.`
// count how many paragraph
paragraph := strings.Split(text, "\n")
//fmt.Println("Paragraph 0 : ", paragraph[0])
//fmt.Println("Paragraph 1 : ", paragraph[1])
// Create a new document for each paragraph
for k, v := range paragraph {
fmt.Println("Processing paragraph ", k, " : ")
doc, err := prose.NewDocument(v)
if err != nil {
log.Fatal(err)
}
// Iterate over the doc's sentences:
for _, sent := range doc.Sentences() {
fmt.Println("["+sent.Text)
}
}
}
Processing paragraph 0 :
[EURUSD’s uptrend may be accelerated if US consumer confidence comes in below the 131.0 estimate and Fed Chairman Jerome Powell’s economic outlook bolsters market expectations of rate cuts.]
[Overnight index swaps are pricing in a 100 percent probability of a cut from the July meeting through year-end.]
[However, rhetoric from the central bank has not indicated that policymakers are feeling dovish to that degree.]
Processing paragraph 1 :
[However, hawkish members of the Fed are finding it increasingly difficult to justify their position in light of US growth.]
[Since February, economic activity out of the US has been broadly underperforming relative to economists’ expectations – signaling that analysts are over estimating the economy’s strength.]
[Inflationary pressure has also been waning alongside a deterioration in global trade due to the US-China trade war.]
See also : Golang : Get FX sentiment from website example
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10k Golang : Convert file content to Hex
+22.7k Golang : Get ASCII code from a key press(cross-platform) example
+9.6k Golang : Convert octal value to string to deal with leading zero problem
+28.9k Golang : How to create new XML file ?
+11k Golang : Concatenate (combine) buffer data example
+6.5k Golang : Get expvar(export variables) to work with multiplexer
+10.6k Golang : Replace a parameter's value inside a configuration file example
+22.6k Golang : Read a file into an array or slice example
+4.9k Golang : Issue HTTP commands to server and port example
+14.6k Golang : package is not in GOROOT during compilation
+33.8k Golang : Smarter Error Handling with strings.Contains()
+7.2k Golang : How to stop user from directly running an executable file?