def run(self): #Load configurations util = Util() configurations = util.loadResources( path_from_root('configurations.txt')) model = configurations['model'] hostname = configurations['local_server_hostname'] port = int(configurations['local_server_port']) print("*** Starting Local Stance Classifier server ***") stance_classifier = StanceClassifier(model) serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind((hostname, port)) serversocket.listen(5) print("Local Stance Classifier server ready!") print("Bound to " + hostname + ":" + str(port) + ". Listening for connections") #Upon receival of classification request, do: while 1: try: #Open connection: (conn, address) = serversocket.accept() print("*** Incoming connection from " + str(address)) print('*** Receiving data in batches of 4096 bytes...') bs = conn.recv(8) (length, ) = unpack('>Q', bs) print('*** Data length (bytes): ' + str(length)) data = b'' c = 1 while len(data) < length: #print('Receiving batch %d ...' % c) to_read = length - len(data) data += conn.recv(4096 if to_read > 4096 else to_read) c += 1 assert len(b'\00') == 1 conn.sendall(b'\00') source = json.loads(data)['original'] reply = json.loads(data)['reply'] output = stance_classifier.classify(source, reply) data = {} data['class'] = output[0] data['probs'] = {} for i in range(0, len(output[1])): data['probs'][i] = output[1][i] print("Sending... " + str(data)) conn.send(json.dumps(data).encode('utf-8')) conn.close() except Exception as ex: print(ex)
def read_vulgar_words(self, f): f_vulgar = open(path_from_root(f), "r", encoding="utf-8") v_vulgar = [] for l in f_vulgar.readlines(): t = l.split("-") v_vulgar.append(t[0].strip()) return v_vulgar
def read_surprise_words(self, f): f_surprise = open(path_from_root(f), "r", encoding="utf-8") v_surprise = [] for l in f_surprise.readlines(): t = l.split("\t") v_surprise.append(t[0].strip()) return v_surprise
def read_google_bad_words(self, f): f_google_bad = open(path_from_root(f), "r", encoding="utf-8") v_google_bad = [] for l in f_google_bad.readlines(): t = l.split(":") v_google_bad.append(t[0].replace("\"", "").strip()) return v_google_bad
def read_emoticon(self, f): f_emoticon = open(path_from_root(f), "r", encoding="utf-8") dict_emoticon = {} for l in f_emoticon.readlines(): t = l.strip().split("\t") dict_emoticon[t[0]] = int(t[1]) return dict_emoticon
def read_no_doubt_words(self, f): f_no_doubt = open(path_from_root(f), "r", encoding="utf-8") v_no_doubt = [] for l in f_no_doubt.readlines(): tokens = l.split(",") for t in tokens: v_no_doubt.append(t.strip()) return v_no_doubt
def loadGloveModel(self, gloveFile): print("Loading Glove Model") f = open(path_from_root(gloveFile), 'r', encoding="utf-8") model = {} for line in f: splitLine = line.split(" ") #print(splitLine) word = splitLine[0] embedding = np.array([float(val) for val in splitLine[1:]]) model[word] = embedding print("Done.",len(model)," words loaded!") return model
def __init__(self, resources): self.emb_file = resources["embeddings_file"] self.emb_size = int(resources["embeddings_size"]) self.emoticons = self.read_emoticon(resources["emoticon"]) self.emoticons_cat = np.zeros(42) self.acronyms = self.read_acronyms(resources["acronyms"]) self.vulgarWords = self.read_vulgar_words (resources["vulgar_words"]) self.googleBadWords = self.read_google_bad_words(resources["google_bad_words"]) self.surpriseWords = self.read_surprise_words(resources["affect_surprise"]) self.doubtWords = self.read_doubt_words(resources["doubt_words"]) self.noDoubtWords = self.read_no_doubt_words(resources["no_doubt_words"]) self.scaler = load(path_from_root(resources["scaler"])) self.glove = self.loadGloveModel(self.emb_file) self.support_terms = ["support", "join", "confirm", "aid", "help"]
def run(self): try: util = Util() configurations = util.loadResources( path_from_root('configurations.txt')) SERVER_PORT_NUMBER = int(configurations['main_server_port']) LOCAL_SERVER = configurations['local_server_hostname'] LOCAL_PORT_NUMBER = int(configurations['local_server_port']) cs = StanceClassifier(LOCAL_SERVER, LOCAL_PORT_NUMBER) server = StanceClassifierServer(('', SERVER_PORT_NUMBER), StanceClassifierHandler) server.addStanceClassifier(cs) print("Bound to " + LOCAL_SERVER + ":" + str(SERVER_PORT_NUMBER) + ". Listening for connections") server.serve_forever() except KeyboardInterrupt: server.socket.close()
def read_acronyms(self, f): f_acronym = open(path_from_root(f), "r", encoding="utf-8") v_acronym = [] for l in f_acronym.readlines(): v_acronym.append(l.strip()) return v_acronym
def open_json(self, f): with open(path_from_root(f), encoding="utf-8") as json_file: data = json.load(json_file) return data