forked from slifty/MetaMetaProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_text.py
55 lines (40 loc) · 1.38 KB
/
meta_text.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
import nltk
import re
class meta_text:
def GET(self, miid='', text='', url='', tasks='{}', results='{}', **kwargs):
try:
tasks = json.loads(tasks)
except:
return {"error": "Invalid input parameters."}
try:
results = json.loads(results)
except:
results = {}
# Run through the task list
if('identify_entities' in tasks):
results['identify_entities'] = meta_text.identify_entities()
if('identify_keywords' in tasks):
results['identify_keywords'] = meta_text.identify_keywords(text, **tasks['identify_keywords'])
return results
def POST(self, text_file='', url='', text='', ttl=180, **kwargs):
return ''
@staticmethod
def identify_entities(**kwargs):
return ''
@staticmethod
def identify_keywords(text, type='document', **kwargs):
text=text.lower()
bigram_measures = nltk.collocations.BigramAssocMeasures()
# split across any non-word character
tokenizer = nltk.tokenize.RegexpTokenizer('[^\w\']+', gaps=True)
# tokenize
tokens = tokenizer.tokenize(text)
# remove stopwords
tokens = [w for w in tokens if not w in nltk.corpus.stopwords.words('english')]
# generate bigram
finder = nltk.collocations.BigramCollocationFinder.from_words(tokens)
# only bigrams that appear 3+ times
finder.apply_freq_filter(3)
# return the 5 n-grams with the highest PMI
return finder.nbest(bigram_measures.pmi, 3)