当前位置: 首页 > 工具软件 > python-nlpir > 使用案例 >

python nlpir_python调用NLPIR - ICTCLAS2013实现中文分词

拓拔安邦
2023-12-01

1 #-*- encoding: utf-8 -*-

2 importNLPIR3 importos4

5 classC_NLPIR_ICTCLAS2013:6 def __init__(self,s_code='GBK'):7 dataurl = os.path.join(os.path.dirname(__file__))8 isinit =09 if s_code == 'GBK':10 isinit =NLPIR.NLPIR_Init(dataurl,NLPIR.GBK_CODE)11 elif s_code == 'UTF-8':12 isinit =NLPIR.NLPIR_Init(dataurl,NLPIR.UTF8_CODE)13 elif s_code == 'BIG5':14 isinit =NLPIR.NLPIR_Init(dataurl,NLPIR.BIG5_CODE)15 elif s_code == 'GBK_FANTI':16 isinit =NLPIR.NLPIR_Init(dataurl,NLPIR.GBK_FANTI_CODE)17 ifisinit:18 print 'NLPIR 初始化成功'

19 else:20 print 'NLPIR 初始化失败'

21

22 def stringSeg(self, s_string, i_bPOStagged=0):23 """

24 Function: Process one string;25 Parameters: @s_string - The string to be analyed,26 @i_bPOStagged: Judge whether need POS tagging, 0 for no tag; 1 for tagging; default:0.27 Return Value: the pointer of result buffer.28 """

29 returnNLPIR.NLPIR_ParagraphProcess(s_string, i_bPOStagged)30

31 def fileSeg(self,s_sourceFile,s_targetFile, i_bPOStagged=0):32 """

33 Function: Process one text file and save the result into one file;34 Parameters: @s_sourceFile - The source file name to be analysized,35 @s_targetFile - The result file name to store the results.36 @i_bPOStagged: Judge whether need POS tagging, 0 for no tag; 1 for tagging; default:0.37 Return Value: the processing speed if processing succeed. Otherwise return false.38 """

39 returnNLPIR.NLPIR_FileProcess(s_sourceFile, s_targetFile, i_bPOStagged)40

41 defimportUserDict(self,s_userDictFile):42 """

43 Functin: Import user-defined dictionary from a text file;44 Parameters: @s_userDictFile - the filename saved user dictionary text;45 Return Value: The number of lexical entry imported successfully46 ???: What's the writting style of the userDicFile ?47 """

48 returnNLPIR.NLPIR_ImportUserDict(s_userDictFile)49

50 defaddUserWord(self,s_word):51 '''

52 Function: Add a word to the user dictionary;53 Parameters: @s_Word - the word added.54 Return Value: 1 if add succeed. Otherwise return 0.55 '''

56 returnNLPIR.NLPIR_AddUserWord(s_word)57

58 defsaveTheUserDict(self):59 '''

60 Function: Save the user dictionary to disk.61 Parameters: none;62 Return Value: 1 if save succeed,otherwise return 0.63 ???: Where's the file_direction of "disk" ?64 '''

65 returnNLPIR.NLPIR_SaveTheUsrDic()66

67 defdelUserWord(self,s_word):68 '''

69 Function: Delete a word from the user dictionary;70 Parameters: @s_word - the word to be deleted;71 Return Value: -1 if the word not exist in the user dictionary, otherwise the handle of the word deleted.72 '''

73 returnNLPIR.NLPIR_DelUsrWord(s_word)74

75 defexit(self):76 '''

77 Return value: true if succeed, otherwise false.78 '''

79 returnNLPIR.NLPIR_Exit()80

81 if __name__ == '__main__':82

83 O_C_NLPIR_ICTCLAS2013 = C_NLPIR_ICTCLAS2013('UTF-8')84 raw_input('\n~!')

 类似资料: