-
Notifications
You must be signed in to change notification settings - Fork 0
/
textserver.py
103 lines (89 loc) · 3.35 KB
/
textserver.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import requests
import pandas as pd
import json
import sys
from functools import reduce
load = lambda r: json.loads(r.encode('utf-8'))
pars = lambda r: [p for p in r['paragraphs']]
sents = lambda p: [s for s in p['sentences']]
decode = lambda x: bytes(x,'latin1').decode('utf-8')
info = lambda s, f: [f(t) for t in s['tokens']]
tokensDecode = lambda s: [decode(t['form']) for t in s['tokens']]
tokens = lambda s: [t['form'] for t in s['tokens']]
flatten = lambda l: reduce(lambda x, y: x + y, l, [])
class TextServer:
def __init__(self, user, pwd, service='morpho'):
self.service = service
self.request_data = {
'username':user,
'password':pwd,
'language': 'ca',
'output': 'json',
'interactive':'1'
}
def query(self, text):
url = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/"+self.service
self.request_data['text_input'] = text
resp = requests.post(url, files=self.request_data)
if resp.status_code != requests.codes.ok :
print(resp.text)
resp.raise_for_status()
return resp.text
def tokenizer(self, text):
self.service = 'tokenizer'
ctnt = self.query(text)
return list(map(tokens, sents(pars(load(ctnt))[0])))
def morpho(self, text, pandas=False):
def cols(t):
return [t['form'],t['lemma'],t['tag'],t['pos']]
self.service = 'morpho'
ctnt = self.query(text)
if pandas:
dt = list(map(lambda s: info(s, cols), sents(pars(load(ctnt))[0])))
return pd.DataFrame(flatten(dt), columns=['forma', 'lema', 'tag', 'pos'])
else:
return list(map(lambda s: info(s, cols), sents(pars(load(ctnt))[0])))
def senses(self, text, pandas=False):
def cols(t):
l = [t['form'],t['lemma'],t['tag'],t['pos']]
l += [t['wn']] if 'wn' in t else ['N/A']
return l
self.service = 'senses'
ctnt = self.query(text)
if pandas:
dt = list(map(lambda s: info(s, cols), sents(pars(load(ctnt))[0])))
return pd.DataFrame(flatten(dt), columns=['forma', 'lema', 'tag', 'pos', 'synset'])
else:
return list(map(lambda s: info(s, cols), sents(pars(load(ctnt))[0])))
def entities(self, text, pandas=False):
def cols(t):
l = [t['form'],t['lemma'],t['tag'],t['pos']]
l += [t['wn']] if 'wn' in t else ['N/A']
l += [t['neclass']] if 'neclass' in t else ['N/A']
return l
self.service = 'entities'
ctnt = self.query(text)
if pandas:
dt = list(map(lambda s: info(s, cols), sents(pars(load(ctnt))[0])))
return pd.DataFrame(flatten(dt), columns=['forma', 'lema', 'tag', 'pos', 'synset', 'neclass'])
else:
return list(map(lambda s: info(s, cols), sents(pars(load(ctnt))[0])))
def dependencies(self, text, pandas=False):
def filtre(d):
res = {}
for k in d:
if k != 'token':
if k!='children':
res[k] = d[k]
else:
res[k] = list(map(filtre,d[k]))
return res
self.service = 'dependencies'
ctnt = self.query(text)
deps = [x[0] for x in map(lambda x: x['dependencies'], sents(pars(load(ctnt))[0]))]
return list(map(filtre, deps))
def coreferences(self, text, pandas=False):
self.service = 'coreferences'
self.request_data['language'] = 'en'
ctnt = self.query(text)
return [[c['words'] for c in x['mentions']] for x in load(ctnt)['coreferences']]