NLPについてもっと学ぶための次のステップとして、単純なnグラム以上の結果を改善する簡単なヒューリスティックを実装しようとしています。nltk.RegexpParser文法を組み合わせる
以下にリンクされているスタンフォード・コロケーションのPDFによれば、「フレーズ」である可能性が高いパターンを通過させる部分の音声フィルタを通して「候補語句」を渡すことは、バイグラムoccuring 出典:コロケーション、ページ143から144:https://nlp.stanford.edu/fsnlp/promo/colloc.pdf
144ページの表7のタグパターンを有しているために、NLTK POSタグと等価である:
JJ NN
。 NN
JJのJJ NN
JJ NN NN
NN JJ NN
NN NN NN
以下のコードでNN
IN NN、私はときに、所望の結果を得ることができI 独立して以下の各文法を適用します。しかし、同じ文法を組み合わせようとすると、私は望みの結果を得られません。
私のコードでは、1つの文のコメントを外し、1つの文法のコメントを外して実行し、結果を確認することができます。
私はすべての文章を結合し、結合された文法(以下のコードのうちの3つだけ)で実行し、望ましい結果を得ることができます。
私の質問は、文法を正しく組み合わせるにはどうすればいいですか?
私は...このパターン、またはこのパターンを見つけ、文法を組み合わせること「OR」のようなものであることを事前に
感謝を想定しています。
import nltk
# The following sentences are correctly grouped with <JJ>*<NN>+.
# Should see: 'linear function', 'regression coefficient', 'Gaussian random variable' and
# 'cumulative distribution function'
SampleSentence = "In mathematics, the term linear function refers to two distinct, although related, notions"
#SampleSentence = "The regression coefficient is the slope of the line of the regression equation."
#SampleSentence = "In probability theory, Gaussian random variable is a very common continuous probability distribution."
#SampleSentence = "In probability theory and statistics, the cumulative distribution function (CDF) of a real-valued random variable X, or just distribution function of X, evaluated at x, is the probability that X will take a value less than or equal to x."
# The following sentences are correctly grouped with <NN.?>*<V.*>*<NN>
# Should see 'mean squared error' and # 'class probability function'.
#SampleSentence = "In statistics, the mean squared error (MSE) of an estimator measures the average of the squares of the errors, that is, the difference between the estimator and what is estimated."
#SampleSentence = "The class probability function is interesting"
# The sentence below is correctly grouped with <NN.?>*<IN>*<NN.?>*.
# should see 'degrees of freedom'.
#SampleSentence = "In statistics, the degrees of freedom is the number of values in the final calculation of a statistic that are free to vary."
SampleSentence = SampleSentence.lower()
print("\nFull sentence: ", SampleSentence, "\n")
tokens = nltk.word_tokenize(SampleSentence)
textTokens = nltk.Text(tokens)
# Determine the POS tags.
POStagList = nltk.pos_tag(textTokens)
# The following grammars work well *independently*
grammar = "NP: {<JJ>*<NN>+}"
#grammar = "NP: {<NN.?>*<V.*>*<NN>}"
#grammar = "NP: {<NN.?>*<IN>*<NN.?>*}"
# Merge several grammars above into a single one below.
# Note that all 3 correct grammars above are included below.
'''
grammar = """
NP:
{<JJ>*<NN>+}
{<NN.?>*<V.*>*<NN>}
{<NN.?>*<IN>*<NN.?>*}
"""
'''
cp = nltk.RegexpParser(grammar)
result = cp.parse(POStagList)
for subtree in result.subtrees(filter=lambda t: t.label() == 'NP'):
print("NP Subtree:", subtree)
あなたは私が多くを理解するのに役立つことができれば、あなたはこの文法のような3つの別々の行を記述する必要はありません= ""」 NP: { * +} { * * } { * * *} "" "代わりに、すべての3つのパターンに対応できる1行に正規表現パターンが必要です。 –
こんにちはRahul。私はどうにかして3つの正規表現パターンを組み合わせて、個々に生成したものと同じ結果を作りたいと思っています。私はそれが1、2、3 +の行でどのように書かれているのか公平である。私は数日後にあなたのコードを試してみましょう。ありがとう。 – RandomTask
さあ、どうぞ!私は複数のシナリオを試してみたが、それは成立している。他の問題を試して戻ってください –