そもそもなのですが、jumanって英語に対応して(英語の辞書がある?)ましたっけ?
前提・実現したいこと
TripAdvisorのレビューをスクレイピングで集め、
日本語のレビューはjumanを用いて単語数のカウントまで完了したのですが、
英語のレビューの単語数のカウントも同様に行いたいです。
該当のソースコード
python3.
1# -*- coding: utf-8 -*- 2# 単語の出現頻度をExcelに 3 4import openpyxl 5import os 6import juman 7import codecs 8import glob 9from collections import Counter 10 11def Juman_for_Frequency(spotname): 12 try: 13 word = [] # 単語 14 try: 15 fr = codecs.open(spotname, "rb", "sjis", "ignore") #読み込み 16 texts = fr.read().split("\n") #ファイルを1行ずつリストとしてtextに 17 fr.close() 18 texts = list(filter(lambda a: a != "", texts)) #空リスト削除 19 debug = 0 20 for text in texts: 21 debug +=1 22 print("open_file = " + spotname + "lines = " + str(debug)) 23 word = word + juman.keitaiso(text) #分かち書きしたワード 24 counter = Counter(word) #同じ文字列をカウントしてcounterに 25 26 except Exception: 27 import traceback 28 traceback.print_exc() 29 30 except Exception: 31 import traceback 32 traceback.print_exc() 33 34 return counter 35 36 37def Save_File(data, save_directory, save_filename): 38 #ファイルに書き込み 39 wb = openpyxl.Workbook() 40 ws = wb.active 41 42 num = 1 43 for k,v in sorted(data.items(),key=lambda x:x[1],reverse=True): 44 key = [] 45 key = k.split(",") 46 ws.cell(column=1, row=num).value=key[0] # 単語 47 ws.cell(column=2, row=num).value=key[1] # 品詞 48 ws.cell(column=3, row=num).value=v # 出現回数 49 num += 1 50 51 wb.save(save_directory + save_filename) 52 print("Save_Complete!") 53 54 55if __name__ == '__main__': 56 path = os.getcwd() # このプログラムの場所 57 textrank_dir = "\textrank\" # "textrank"を保存するディレクトリ 58 review_dir = "\review\" # "review"ディレクトリ 59 if os.path.isdir(path + textrank_dir) == False: # "textrank"というフォルダが無ければ作成 60 os.mkdir(path + textrank_dir) 61 os.chdir("review/") 62 63 csv_files = glob.glob("*.csv") 64 save_directory = path + textrank_dir 65 for spotname in csv_files: 66 print(spotname) 67 get_data = Juman_for_Frequency(spotname) 68 Save_File(get_data, save_directory, spotname) 69
あなたの回答
tips
プレビュー