亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

字符串的詞庫:因為起始字符太多,需要用不等于邏輯拆分

字符串的詞庫:因為起始字符太多,需要用不等于邏輯拆分

Go
胡說叔叔 2023-05-15 15:39:19
我有一個 .dat 文件,它是一個包含大約 30 萬行的字典/詞庫對于每個單詞,它下面的字符串開頭的括號中的單詞是同義詞庫的備選詞,括號中的單詞是類型。所以是名詞或形容詞。例如:acceptant|1(adj)|acceptive|receptive acceptation|3(noun)|acceptance(noun)|word meaning|word sense|sense|signified(noun)|adoption|acceptance|espousal|blessing|approval|approvingaccepted|6(adj)|recognized|recognised|acknowledged (adj)|undisputed|uncontroversial |noncontroversial(adj)|standard (adj)|acceptable|standard |received(adj)|established |constituted(adj)|received|conventional accepting|1(adj)|acceptive 所以在上面有 4 個來自字典的單詞,但是每個單詞都有多個不同的同義詞庫條目我想使用以下方式拆分字符串:strings.Split(dictionary, !"(")意思是任何不是“(”字符的東西。這是因為它是一本包含俚語和縮寫等等的廣泛詞典。但我無法弄清楚如何使用不等于運算符有誰知道如何使用不等于邏輯的拆分?或者任何人都可以提出一些聰明的替代想法嗎?
查看完整描述

2 回答

?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

package main


import (

? ? "bufio"

? ? "bytes"

? ? "fmt"

? ? "os"

)


func main() {

? ? file, _ := os.Open("dic.dat")

? ? scanner := bufio.NewScanner(file)

? ? for scanner.Scan() {

? ? ? ? data := scanner.Bytes()

? ? ? ? if bytes.HasPrefix(data, []byte("(")) {

? ? ? ? ? ? continue

? ? ? ? }

? ? ? ? line := scanner.Text()

? ? ? ? fmt.Println(line)

? ? }

}

輸出:


acceptant|1

acceptation|3

accepted|6

accepting|1

按照設計,Go 代碼應該是高效的。Go 標準庫測試包包含一個基準功能。


避免不必要的轉換和分配很重要。例如,將從文件中讀取的字節片轉換為字符串、分配和復制。


在這種情況下,我們只需要將接受的數據轉換為字符串即可。例如,更喜歡字節而不是文本。


$ go test dict_test.go -bench=.

BenchmarkText-4? ? ? 500? ? 2486306 ns/op? ? 898528 B/op? ? 14170 allocs/op

BenchmarkBytes-4? ? 1000? ? 1489828 ns/op? ? ?34080 B/op? ? ? 609 allocs/op

$

樣本基準數據:


KEY: Aback.

SYN: Backwards, rearwards, aft, abaft, astern, behind, back.

ANT: Onwards, forwards, ahead, before, afront, beyond, afore.

=

KEY: Abandon.

SYN: Leave, forsake, desert, renounce, cease, relinquish,

discontinue, castoff, resign, retire, quit, forego, forswear,

depart from, vacate, surrender, abjure, repudiate.

ANT: Pursue, prosecute, undertake, seek, court, cherish, favor,

protect, claim, maintain, defend, advocate, retain, support, uphold,

occupy, haunt, hold, assert, vindicate, keep.

=

dict_test.go:


package main


import (

? ? "bufio"

? ? "bytes"

? ? "fmt"

? ? "io/ioutil"

? ? "os"

? ? "strings"

? ? "testing"

)


func BenchmarkText(b *testing.B) {

? ? b.ReportAllocs()

? ? for N := 0; N < b.N; N++ {

? ? ? ? file := bytes.NewReader(benchData)

? ? ? ? scanner := bufio.NewScanner(file)

? ? ? ? for scanner.Scan() {

? ? ? ? ? ? line := scanner.Text()

? ? ? ? ? ? if !strings.HasPrefix(line, "KEY") {

? ? ? ? ? ? ? ? continue

? ? ? ? ? ? }

? ? ? ? ? ? _ = line // process line

? ? ? ? }

? ? ? ? if err := scanner.Err(); err != nil {

? ? ? ? ? ? b.Fatal(err)

? ? ? ? }

? ? }

}


func BenchmarkBytes(b *testing.B) {

? ? b.ReportAllocs()

? ? for N := 0; N < b.N; N++ {

? ? ? ? file := bytes.NewReader(benchData)

? ? ? ? scanner := bufio.NewScanner(file)

? ? ? ? for scanner.Scan() {

? ? ? ? ? ? data := scanner.Bytes()

? ? ? ? ? ? if !bytes.HasPrefix(data, []byte("KEY")) {

? ? ? ? ? ? ? ? continue

? ? ? ? ? ? }

? ? ? ? ? ? line := scanner.Text()

? ? ? ? ? ? _ = line // process line

? ? ? ? }

? ? ? ? if err := scanner.Err(); err != nil {

? ? ? ? ? ? b.Fatal(err)

? ? ? ? }

? ? }

}


var benchData = func() []byte {

? ? // A Complete Dictionary of Synonyms and Antonyms by Samuel Fallows

? ? // http://www.gutenberg.org/files/51155/51155-0.txt

? ? data, err := ioutil.ReadFile(`/home/peter/dictionary.51155-0.txt`)

? ? if err != nil {

? ? ? ? panic(err)

? ? }

? ? return data

}()


查看完整回答
反對 回復 2023-05-15
?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

package main


import (

    "bufio"

    "fmt"

    "os"

    "strings"

)


func main() {


    file, _ := os.Open("dic.dat")

    scanner := bufio.NewScanner(file)

    for scanner.Scan() {

        line := scanner.Text()

        if strings.HasPrefix(line, "(") {

            continue

        }

        fmt.Println(line)

    }


}


查看完整回答
反對 回復 2023-05-15
  • 2 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號