日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python提取停用词_python文本处理 数据挖掘 停用词检索

發布時間:2023/12/10 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python提取停用词_python文本处理 数据挖掘 停用词检索 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單描述程序功能:python+flask

1.停用詞為csv文件

2.源文件為txt文件

3.文本處理,將原文件中出現的停用詞去除

4.根據用戶web 表單輸入,檢索出包含用戶輸入參數的句子

代碼實現:

1.文件讀取,分詞,源文件詞頻統計

python 讀取 西班牙語文本編碼: encoding='ISO-8859-1'

1 #csv 文件讀取,此處編碼為西班牙語

2 defcsvfile():3 file_path = os.path.join(upload_path, "SpanishStopWords.csv")4 with open(file_path,'r',encoding='ISO-8859-1') as f:5 reader =csv.reader(f)6 fieldnames = next(reader)#獲取數據的第一列,作為后續要轉為字典的鍵名 生成器,next方法獲取

7 #print(fieldnames)

8 data1=[]9 csv_reader = csv.DictReader(f,fieldnames=fieldnames) #self._fieldnames = fieldnames # list of keys for the dict 以list的形式存放鍵名

10 for row incsv_reader:11 dic1={}12 for k,v inrow.items():13 dic1[k]=v14 data1.append(dic1)15 returndata116 #txt文件讀取

17 defeachcount():18 file_path = os.path.join(upload_path, "Alamo.txt")19 txt = open(file_path, 'r', encoding='ISO-8859-1').read()20 #分詞

21 txt = txt.replace(',', ' ').replace('.', ' ')22 txt =txt.split()23 counts = {} #定義一個空字典類型

24 print(txt)25 for word intxt:26 counts[word] = counts.get(word, 0) + 1 #獲取word當前有幾個,如果word不存在則為0

27 items =list(counts.items())28 #對一個列表按照鍵值對的兩個元素的第二個元素進行排序,由大到小的倒排,詞頻排序

29 items.sort(key=lambda x: x[1], reverse=False)30 return items

2.顯示在原文件中出現的所有停用詞

#顯示在源文件中出現過的所有停用詞

@application.route('/listsearch/', methods=['GET', 'POST'])

def listsearch():

file_path = os.path.join(upload_path, "SpanishStopWords.csv")

txt = open(file_path, 'r', encoding='ISO-8859-1').read()

# txt = txt.replace(',', ' ').replace('.', ' ')

txt = txt.split()

filelist=txt

# filelist=csvfile()

filelist2=docu2()

# wordlist=["my","name","boy","chirs","Dave"]

result=[]

result2=[]

# for j in wordlist:

# for i in filelist:

# if i[0]== j :

# result.append(i)

for j in filelist:

for i in filelist2:

if j== i :

result2.append(j)

return render_template('index.html',result2=result2)

前端代碼展現:

search

result

{% for line2 in result2 %}

{{ line2}}

{% endfor %}

3.顯示原文件中所有含有數字的句子

1 @application.route('/test1/', methods=['GET', 'POST'])2 deftest1():3 file_path = os.path.join(upload_path, "Alamo.txt")4 txt = open(file_path, 'r', encoding='ISO-8859-1').read()5 #txt = txt.replace(',', ' ').replace('.', ' ')

6 txt = txt.split('.')7 filelist=txt8 result2=[]9 for j infilelist:10 #使用正則表達式匹配數字

11 if re.match('.*[0-9].*', j) !=None:12 result2.append(j)13 return render_template('index.html',result9=result2)

4.用戶web 表單輸入參數,根據用戶輸入,顯示源文件中包含用戶輸入參數的句子。

1 @application.route('/test2/', methods=['GET', 'POST'])2 deftest2():3 word = request.args.get("word10")4 file_path = os.path.join(upload_path, "Alamo.txt")5 txt = open(file_path, 'r', encoding='ISO-8859-1').read()6 #txt = txt.replace(',', ' ').replace('.', ' ')

7 txt = txt.split('.')8 filelist=txt9 result=[]10 result2=[]11 for j infilelist:12 if word inj :13 result2.append(j)14 return render_template('index.html',result10=result2)

前端代碼展現:

1

2 submit

3 {% for li in result9 %}4

{{ li}}

5

6 {% endfor %}7

8

9

11

13 submit

14 {% for li in result10 %}15

{{ li}}

16

17 {% endfor %}18

總結

以上是生活随笔為你收集整理的python提取停用词_python文本处理 数据挖掘 停用词检索的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。