के साथ प्रभावी 1-5 ग्राम निष्कर्षण मेरे पास 3,000,000 लाइनों की एक बड़ी फाइलें हैं और प्रत्येक पंक्ति में 20-40 शब्द हैं। मुझे कॉर्पस से 1 से 5 एनजीआरएम निकालना है। मेरे इनपुट फ़ाइलें सादा पाठ tokenized कर रहे हैं, उदाहरण के लिए:पायथन
This is a foo bar sentence .
There is a comma , in this sentence .
Such is an example text .
वर्तमान में, मैं इसे नीचे के रूप में कर रहा हूँ, लेकिन यह एक कारगर तरीका 1-5grams निकालने के लिए होने लगते नहीं है:
#!/usr/bin/env python -*- coding: utf-8 -*-
import io, os
from collections import Counter
import sys; reload(sys); sys.setdefaultencoding('utf-8')
with io.open('train-1.tok.en', 'r', encoding='utf8') as srcfin, \
io.open('train-1.tok.jp', 'r', encoding='utf8') as trgfin:
# Extract words from file.
src_words = ['<s>'] + srcfin.read().replace('\n', ' </s> <s> ').split()
del src_words[-1] # Removes the final '<s>'
trg_words = ['<s>'] + trgfin.read().replace('\n', ' </s> <s> ').split()
del trg_words[-1] # Removes the final '<s>'
# Unigrams count.
src_unigrams = Counter(src_words)
trg_unigrams = Counter(trg_words)
# Sum of unigram counts.
src_sum_unigrams = sum(src_unigrams.values())
trg_sum_unigrams = sum(trg_unigrams.values())
# Bigrams count.
src_bigrams = Counter(zip(src_words,src_words[1:]))
trg_bigrams = Counter(zip(trg_words,trg_words[1:]))
# Sum of bigram counts.
src_sum_bigrams = sum(src_bigrams.values())
trg_sum_bigrams = sum(trg_bigrams.values())
# Trigrams count.
src_trigrams = Counter(zip(src_words,src_words[1:], src_words[2:]))
trg_trigrams = Counter(zip(trg_words,trg_words[1:], trg_words[2:]))
# Sum of trigram counts.
src_sum_trigrams = sum(src_bigrams.values())
trg_sum_trigrams = sum(trg_bigrams.values())
क्या यह और अधिक कुशलता से करने का कोई और तरीका है?
कैसे बेहतर विभिन्न एन ngrams एक साथ निकालने के लिए? trigrams के लिए
zip(src_words,src_words[1:])
और है यह, n=3
:
, अनिवार्य रूप से इस:
zip(*[words[i:] for i in range(n)])
हार्ड-कोडेड Bigrams के लिए यह है जब, n=2
zip(src_words,src_words[1:],src_words[2:])
इनपुट फ़ाइलों का प्रारूप क्या है? – mbatchkarov