斯坦福大学corenlp:排名前K的N组记分卡
如何使用stanford corenlp通过其计数获得前K名?我知道我可以使用HashMap或Trai编写这段代码,但我的语料库相当大(每个文章的平均大小为30KB,有200K篇),我需要5grams,所以内存需求将是巨大的。因此,我想知道是否可以使用corenlp实现这一目的。 因此,给定一个语料库,它应该只返回这种格式的前K个ngram: word1 word2 word3 word4 word5:频率斯坦福大学corenlp:排名前K的N组记分卡,nlp,stanford-nlp,n-gram,Nlp,Stanford Nlp,N Gram,如何使用stanford corenlp通过其计数获得前K名?我知道我可以使用HashMap或Trai编写这段代码,但我的语料库相当大(每个文章的平均大小为30KB,有200K篇),我需要5grams,所以内存需求将是巨大的。因此,我想知道是否可以使用corenlp实现这一目的。 因此,给定一个语料库,它应该只返回这种格式的前K个ngram: word1 word2 word3 word4 word5:频率 我不想要任何概率模型。CoreNLP没有任何东西可以帮助您高效地存储ngrams。在这里
我不想要任何概率模型。CoreNLP没有任何东西可以帮助您高效地存储ngrams。在这里,它所能帮助您的就是标记文本(如果您关心的话,还可能将文本分割成句子) 如果您的语料库足够大,您不能仅使用哈希表来保持n-gram计数,那么您必须使用另一种更节省空间的表示法(例如前缀trie) 例如,我刚刚在Clojure做了一个快速测试,在那里我计算了古腾堡国王詹姆斯五世圣经中的5克。使用hashmap存储752K个不同的5克的计数,使用248MB的堆。使用前缀trie存储计数使用了57MB——减少了77% 以下是使用前缀trys的完整Clojure程序供参考:
(ns nlp.core
(:require [clojure.string :as string]))
(defn tokenize
"Very simplistic tokenizer."
[text]
(string/split text #"[\s\:_\-\.\!\,\;]+"))
(defn get-bible-kjv-tokens []
(tokenize (slurp "/Users/wiseman/nltk_data/corpora/gutenberg/bible-kjv.txt")))
(defn ngrams [n tokens]
(partition n 1 tokens))
(defn build-ngram-trie [n tokens]
(->> tokens
(ngrams n)
(reduce (fn [trie ngram]
(update-in trie ngram #(if % (inc %) 1)))
{})))
(defn enumerate-trie [trie]
(if (not (map? trie))
(list (list trie))
(apply concat
(for [[k v] trie]
(map #(cons k %)
(enumerate-trie v))))))
(defn print-trie [trie]
(doseq [path (enumerate-trie trie)]
(println (string/join " " (butlast path)) ":" (last path))))
(defn -main []
(let [ngram-counts (->> (get-bible-kjv-tokens)
(build-ngram-trie 5))]
(print-trie ngram-counts)))
以及《国王詹姆斯五世圣经》的输出:
$ lein run -m nlp.core | sort -r -k7,7 -n ngrams.txt | head
And it came to pass : 383
the house of the LORD : 233
the word of the LORD : 219
of the children of Israel : 162
it came to pass when : 142
the tabernacle of the congregation : 131
saith the LORD of hosts : 123
it shall come to pass : 119
And the LORD said unto : 114
And the LORD spake unto : 107
关于获得更高效率的一些建议,以下文章讨论了大型语料库的高效n-gram存储:
-使用自定义数据结构
-“我们最紧凑的表示法可以以每n-gram 23位的速度存储Google n-gram语料库的所有40亿n-gram和相关计数,这是迄今为止最紧凑的无损表示法”