File: client.py

package info (click to toggle)
python-pynlpl 1.2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,900 kB
  • sloc: python: 25,677; sh: 73; makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,720 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import    

import socket

class LMClient(object):

    def __init__(self,host= "localhost",port=12346,n = 0):        
        self.BUFSIZE = 1024
        self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #Create the socket
        self.socket.settimeout(120)
        assert isinstance(port,int) 
        self.socket.connect((host, port)) #Connect to server
        assert isinstance(n,int)
        self.n = n

    def scoresentence(self, sentence):
        if self.n > 0:
            raise Exception("This client instance has been set to send only " + str(self.n) +  "-grams")
        if isinstance(sentence,list) or isinstance(sentence,tuple):
            sentence = " ".join(sentence)
        self.socket.send(sentence+ "\r\n")
        return float(self.socket.recv(self.BUFSIZE).strip())

    def __getitem__(self, ngram):
        if self.n == 0:
            raise Exception("This client  has been set to send only full sentence, not n-grams")
        if isinstance(ngram,str) or isinstance(ngram,unicode):
            ngram = ngram.split(" ")
        if len(ngram) != self.n:
            raise Exception("This client instance has been set to send only " + str(self.n) +  "-grams.")
        ngram = " ".join(ngram)
        if (sys.version < '3' and isinstance(ngram,unicode)) or( sys.version == '3' and isinstance(ngram,str)):
            ngram = ngram.encode('utf-8')        
        self.socket.send(ngram + b"\r\n")
        return float(self.socket.recv(self.BUFSIZE).strip())