forked from boudinfl/pke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyphrase-extraction.py
32 lines (26 loc) · 1.04 KB
/
keyphrase-extraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# this example uses TopicRank
from pke.unsupervised import TopicRank
# create a TopicRank extractor
extractor = TopicRank()
# load the content of the document, here in raw text format
# the input language is set to English (used for the stoplist)
# normalization is set to stemming (computed with Porter's stemming algorithm)
with open('2.txt') as f:
doc = f.read()
extractor.load_document(
doc,
language='en',
normalization='stemming')
# select the keyphrase candidates, for TopicRank the longest sequences of
# nouns and adjectives
extractor.candidate_selection(pos={'NOUN', 'PROPN', 'ADJ'})
# weight the candidates using a random walk. The threshold parameter sets the
# minimum similarity for clustering, and the method parameter defines the
# linkage method
extractor.candidate_weighting(threshold=0.74,
method='average')
# print the n-highest (10) scored candidates
for (keyphrase, score) in extractor.get_n_best(n=10, stemming=True):
print(keyphrase, score)