![](/img/trans.png)
[英]NLTK ViterbiParser fails in parsing words that are not in the PCFG rule
[英]PCFG generation in NLTK
我正在嘗試從包含解析樹的文件中學習 PCFG,例如:
(S (DECL_MD (NP_PPSS (PRON_PPSS (ii))) (VERB_MD (pt_verb_md need)) (NP_NN (ADJ_AT (aa)) (NOUN_NN (flight flight)) (PREP_IN (pt_prep_in from))) (AVPNP_NP (NOUN_NP (charlotte charlotte) ))
這是我的相關代碼:
def loadData(path):
with open(path ,'r') as f:
data = f.read().split('\n')
return data
def getTreeData(data):
return map(lambda s: tree.Tree.fromstring(s), data)
# Main script
print("loading data..")
data = loadData('C:\\Users\\Rayyan\\Desktop\\MSc Data\\NLP\\parseTrees.txt')
print("generating trees..")
treeData = getTreeData(data)
print("done!")
print("done!")
現在之后我在互聯網上嘗試了很多東西,例如:
grammar = induce_pcfg(S, productions)
但這里的產品總是內置的功能,例如:
productions = []
for item in treebank.items[:2]:
for tree in treebank.parsed_sents(item):
productions += tree.productions()
在我的例子中,我嘗試用treeData
替換這里的production
,但它不起作用。 我錯過了什么或做錯了什么?
從建樹開始:
from nltk import tree
treeData_rules = []
# Extract the CFG rules (productions) for the sentence
for item in treeData:
for production in item.productions():
treeData_rules.append(production)
treeData_rules
然后你可以像這樣提取概率CFG(PCFG):
from nltk import induce_pcfg
S = Nonterminal('S')
grammar_PCFG = induce_pcfg(S, treeData_rules)
print(grammar_PCFG)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.