如何使用spacy / nltk生成bi / tri-gram
发布时间:2020-12-14 04:38:53 所属栏目:大数据 来源:网络整理
导读:输入文本总是菜名列表,其中有1~3个形容词和一个名词 输入 thai iced teaspicy fried chickensweet chili porkthai chicken curry 输出: thai tea,iced teaspicy chicken,fried chickensweet pork,chili porkthai chicken,chicken curry,thai curry 基本上,
输入文本总是菜名列表,其中有1~3个形容词和一个名词
输入 thai iced tea spicy fried chicken sweet chili pork thai chicken curry 输出: thai tea,iced tea spicy chicken,fried chicken sweet pork,chili pork thai chicken,chicken curry,thai curry 基本上,我希望解析句子树并尝试通过将形容词与名词配对来生成二元词. 我想用spacy或nltk实现这一目标 解决方法
您可以使用NLTK在几个步骤中实现此目的:
> PoS标记序列 例: def jjnn_pairs(phrase): ''' Iterate over pairs of JJ-NN. ''' tagged = nltk.pos_tag(nltk.word_tokenize(phrase)) for ngram in ngramise(tagged): tokens,tags = zip(*ngram) if tags == ('JJ','NN'): yield tokens def ngramise(sequence): ''' Iterate over bigrams and 1,2-skip-grams. ''' for bigram in nltk.ngrams(sequence,2): yield bigram for trigram in nltk.ngrams(sequence,3): yield trigram[0],trigram[2] 根据您的需要扩展模式(‘JJ’,’NN’)和所需的n-gram. 我认为不需要解析.然而,这种方法的主要问题是大多数PoS标记器可能不会按照您想要的方式标记所有内容.例如,我的NLTK安装的默认PoS标记器将“chili”标记为NN,而不是JJ,并且“fried”标记为VBD.但是,解析对你没有帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |