国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python實現機械分詞之逆向最大匹配算法代碼示例

python實現機械分詞之逆向最大匹配算法代碼示例

2020-12-24 00:57lalalawxt Python

這篇文章主要介紹了python實現機械分詞之逆向最大匹配算法代碼示例,具有一定借鑒價值,需要的朋友可以參考下。

逆向最大匹配方法

有正即有負,正向最大匹配算法大家可以參閱http://www.jfrwli.cn/article/123273.html

逆向最大匹配分詞是中文分詞基本算法之一,因為是機械切分,所以它也有分詞速度快的優點,且逆向最大匹配分詞比起正向最大匹配分詞更符合人們的語言習慣。逆向最大匹配分詞需要在已有詞典的基礎上,從被處理文檔的末端開始匹配掃描,每次取最末端的i個字符(分詞所確定的閾值i)作為匹配字段,若匹配失敗,則去掉匹配字段最前面的一個字,繼續匹配。而且選擇的閾值越大,分詞越慢,但準確性越好。

逆向最大匹配算法python實現:

分詞文本示例:

python實現機械分詞之逆向最大匹配算法代碼示例

分詞詞典words.xlsx示例:

python實現機械分詞之逆向最大匹配算法代碼示例

?
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
#-*- coding:utf-8 -*-
 
'''''
用逆向最大匹配法分詞,不去除停用詞
'''
import codecs
import xlrd
 
#讀取待分詞文本,readlines()返回句子list
def readfile(raw_file_path):
  with codecs.open(raw_file_path,"r",encoding="ANSI") as f:
    raw_file=f.readlines()
    return raw_file
#讀取分詞詞典,返回分詞詞典list
def read_dic(dic_path):
  excel = xlrd.open_workbook(dic_path)
  sheet = excel.sheets()[0]
  # 讀取第二列的數據
  data_list = list(sheet.col_values(1))[1:]
  return data_list
#逆向最大匹配法分詞
def cut_words(raw_sentences,word_dic):
  word_cut=[]
  #最大詞長,分詞詞典中的最大詞長,為初始分詞的最大詞長
  max_length=max(len(word) for word in word_dic)
  for sentence in raw_sentences:
    #strip()函數返回一個沒有首尾空白字符(‘\n'、‘\r'、‘\t'、‘')的sentence,避免分詞錯誤
    sentence=sentence.strip()
    #單句中的字數
    words_length = len(sentence)
    #存儲切分出的詞語
    cut_word_list=[]
    #判斷句子是否切分完畢
    while words_length > 0:
      max_cut_length = min(words_length, max_length)
      for i in range(max_cut_length, 0, -1):
        #根據切片性質,截取words_length-i到words_length-1索引的字,不包括words_length,所以不會溢出
        new_word = sentence[words_length - i: words_length]
        if new_word in word_dic:
          cut_word_list.append(new_word)
          words_length = words_length - i
          break
        elif i == 1:
          cut_word_list.append(new_word)
          words_length = words_length - 1
    #因為是逆向最大匹配,所以最終需要把結果逆向輸出,轉換為原始順序
    cut_word_list.reverse()
    words="/".join(cut_word_list)
    #最終把句子首端的分詞符號刪除,是避免以后將分詞結果轉化為列表時會出現空字符串元素
    word_cut.append(words.lstrip("/"))
  return word_cut
#輸出分詞文本
def outfile(out_path,sentences):
  #輸出模式是“a”即在原始文本上繼續追加文本
  with codecs.open(out_path,"a","utf8") as f:
    for sentence in sentences:
      f.write(sentence)
  print("well done!")
def main():
  #讀取待分詞文本
  rawfile_path = r"逆向分詞文本.txt"
  raw_file=readfile(rawfile_path)
  #讀取分詞詞典
  wordfile_path = r"words.xlsx"
  words_dic = read_dic(wordfile_path)
  #逆向最大匹配法分詞
  content_cut = cut_words(raw_file,words_dic)
  #輸出文本
  outfile_path = r"分詞結果.txt"
  outfile(outfile_path,content_cut)
if __name__=="__main__":
  main()

python實現機械分詞之逆向最大匹配算法代碼示例

總結

分析分詞結果可以知道,機械分詞的效果優劣,一方面與分詞匹配算法有關,另外一方面極其依賴分詞詞典。所以若想得到好的分詞效果,處理相關領域的文本時,需要在分詞詞典中加入特定領域的詞匯。

以上就是本文關于python實現機械分詞之逆向最大匹配算法代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/lalalawxt/article/details/75477931

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产精品一区二区三区在线播放 | 国产99一区二区 | 在线小视频 | 偷拍做爰吃奶视频免费看 | 日韩国产一区二区 | 欧美日韩高清不卡 | 欧美成人激情 | 中文字幕在线第一页 | 久久香蕉国产视频 | 羞羞视频在线播放 | 亚洲欧美在线精品 | 亚洲免费在线 | 精品国产精品 | 成年人黄色免费网站 | 国产精品美女久久久久久久久久久 | www.fefe66.com | 欧美大黄大色一级毛片 | 999久久久国产999久久久 | 久久一区| 久久99视频 | 91在线公开视频 | 久久久国产精品 | 国产精品免费久久 | 久久爱综合 | 欧美一级内谢 | 亚州ava | 荷兰欧美一级毛片 | 国产精品久久综合 | 亚洲精品在线观看网站 | 国产成人精品一区二区三区网站观看 | 成人精品动漫一区二区三区 | 精品国产欧美一区二区三区成人 | 亚洲视频区| 亚洲久草 | 国产日韩欧美精品 | 中文字幕一区二区三区日韩精品 | 欧美性猛片aaaaaaa做受 | 在线免费观看a视频 | 日韩中文视频 | 成人国产在线 | 91色爱|