<commit_before>import chineseseg

segmenter = chineseseg.stanford("/home/wlzhuang/stanford-segmenter-2015-04-20/stanford-segmenter-3.5.2.jar")

print( segmenter.segment("简单调用情况如下") )<commit_msg>add ckip prototype<commit_after>import chineseseg

"""
stanford = chineseseg.stanford("/home/wlzhuang/stanford-segmenter-2015-04-20/stanford-segmenter-3.5.2.jar")
print( stanford.segment("简单调用情况如下") )

"""
import socket

from lxml.etree import tostring, fromstring
from lxml.builder import E
class Ckip():

    CKIP_SERVER_IP, CKIP_SERVER_PORT = "172.16.58.3", 1501

    def __init__( self, username, password, server_ip = CKIP_SERVER_IP, server_port = CKIP_SERVER_PORT ):
        self.username = username
        self.password = password

        self._server_ip = server_ip
        self._server_port = server_port

    def segment( self, sent ):
        sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
        sock.connect( (self._server_ip, self._server_port) )

        encoding = "big5"
        tree = E.wordsegmentation(
            E.option(showcategory='1'),
            E.authentication(username=self.username, password=self.password),
            E.text(sent),
        version='0.1')
        xmlstring = tostring(tree, encoding=encoding, xml_declaration=True)


        sock.send( xmlstring )
        data = sock.recv( 10240 )
        sock.close()
        return self.parse_result( data.decode(encoding) )

    def parse_result( self, string ):
        return string



seg = Ckip("wlzhuang", "xxxxaaaackip")
print( seg.segment("本線上斷詞服務的資料交換方式採用一XML格式，用戶端可自行撰寫程式經由 TCP Socket 連線傳送驗證資訊及文本至本伺服器，伺服器經過處理後經由原連線傳回結果。請詳閱以下說明：") )
{'fraction_non_alphanumeric': 0.11660561660561661, 'fraction_numerical': 0.026251526251526252, 'mean_word_length': 3.453804347826087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6006617', 'n_tokens_mistral': 655, 'n_tokens_neox': 633, 'n_words': 95}