Created
November 1, 2019 10:43
-
-
Save Cartmanishere/bd1833919dd7bf8a9e6260b8ad808e8a to your computer and use it in GitHub Desktop.
Python server implementation for grpc-python-golang example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from nltk_service_pb2 import * | |
from nltk_service_pb2_grpc import * | |
import grpc | |
import nltk | |
from concurrent import futures | |
from nltk.corpus import stopwords | |
class NltkService(KeywordServiceServicer): | |
def __init__(self): | |
self.stopwords = stopwords.words('english') | |
self.trans_table = str.maketrans("", "", "[]()-,|/\\\"?:;&%$#@!%^.") # Remove these symbols from keywords | |
def keywords(self, words): | |
# We are removing all the stopwords and removing all other characters | |
return [word.translate(self.trans_table) for word in words if word not in self.stopwords] | |
def GetKeywords(self, request, context): | |
# Text we got from the client | |
text = request.text.lower() | |
text = text.replace('\n', '').replace('\r', '') | |
words = text.split(' ') | |
keywords = self.keywords(words) | |
response = Response() | |
response.keywords.extend(keywords) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment