前提・実現したいこと
更新されたソースコードを続行してもからのファイルが誕生してしまう。
うまく読み込めていないのがどこなのかいまいちわからない。
発生している問題・エラーメッセージ
エラー自体は消えました
更新されたソースコード
# coding: utf-8 import sys import json import MeCab import urllib.request, urllib.error, urllib.parse from collections import defaultdict from operator import itemgetter from bs4 import BeautifulSoup from bs4 import NavigableString class Ngram(): def __init__(self, N=3): self.N = N self.tagger = MeCab.Tagger("-Owakati") def get(self, text, ngram=None): seq = self.tagger.parse(text) if ngram is None: ngram = [defaultdict(int) for x in range((self.N + 1))] ngram[0] = None for i in range(len(seq)): for n in range(1, self.N + 1): idx = i - n + 1 # check ngram is valid range if idx >= 0: key_words = [] for j in range(idx, i+1): key_words.append(seq[j]) key = '_'.join(key_words) ngram[n][key] += 1 return ngram class HTMLParser(): def get(self, url): try: c = urllib.request.urlopen(url) except: print("Could not open %s" % url) return "" soup = BeautifulSoup(c.read(), "lxml") text = '\n'.join(self.__getNavigableStrings(soup)) return text def __getNavigableStrings(self, soup): if isinstance(soup, NavigableString): if type(soup) not in (Comment, Declaration) and soup.strip(): yield soup elif soup.name not in ('script', 'style'): for c in soup.contents: for g in self.__getNavigableStrings(c): yield g if __name__ == "__main__": f = open("urls.json", "r") urls = json.load(f) f.close() print("Count of urls : " + str(len(urls))) N = 10 hp = HTMLParser() ng = Ngram(N) ngram = None for url in urls: text = hp.get(url) ngram = ng.get(text, ngram) for n in range(1, (N + 1)): f = open('outputs/{:02d}.tsv'.format(n), 'w') out = "" for k, v in sorted(list(ngram[n].items()), key=itemgetter(1), reverse=True): out += "{}\t{}\n".format(k, v) f.write(out) f.close()
URLS.JSONのファイル
["https://news.yahoo.co.jp/profile/settings/", "https://headlines.yahoo.co.jp/hl?a=20180601-00000063-dal-ent", "https://news.yahoo.co.jp/feature", "https://news.yahoo.co.jp/pickup/6284612", "https://news.yahoo.co.jp/pickup/6284600", "https://news.yahoo.co.jp/ranking/access?ty=t", "https://news.yahoo.co.jp/polls/", "https://news.yahoo.co.jp/hl?c=c_spo", "https://news.yahoo.co.jp/hl?c=loc", "https://www.yahoo-help.jp/app/noscript", "https://headlines.yahoo.co.jp/hl?a=20180601-00000520-san-pol", "https://news.yahoo.co.jp/search/advanced", "https://news.yahoo.co.jp/pickup/6284614", "https://news.yahoo.co.jp/pickup/6284627", "https://news.yahoo.co.jp/photo", "http://news.yahoo.co.jp/", "https://news.yahoo.co.jp/profile/login", "https://news.yahoo.co.jp/ranking", "https://headlines.yahoo.co.jp/purchase/", "https://headlines.yahoo.co.jp/hl?a=20180601-00000002-wordleafs-socc", "https://news.yahoo.co.jp/byline/", "https://headlines.yahoo.co.jp/videonews/jnn?a=20180601-00000013-jnn-soci", "https://news.yahoo.co.jp/ranking/access?ty=v", "https://about.yahoo.co.jp/docs/info/terms/chapter1.html", "https://headlines.yahoo.co.jp/hl?a=20180601-00000010-jij-soci", "https://news.yahoo.co.jp/ranking/access?ty=z", "https://headlines.yahoo.co.jp/videonews/jnn?a=20180530-00000072-jnn-soci", "https://headlines.yahoo.co.jp/article?a=20180601-00542887-shincho-ent", "https://about.yahoo.co.jp/info/mediastatement/", "https://headlines.yahoo.co.jp/docs/copyright.html", "https://news.yahoo.co.jp/byline/kohyoungki/20180531-00085870/", "https://news.yahoo.co.jp/ranking/comment/rate?ty=t", "https://news.yahoo.co.jp/ranking/access?ty=b", "https://headlines.yahoo.co.jp/hl?a=20180601-00003575-tokaiv-l23", "https://headlines.yahoo.co.jp/hl?a=20180531-00010002-bfj-soci", "https://news.yahoo.co.jp/hl?c=c_ent", "https://headlines.yahoo.co.jp/hl?a=20180601-00000519-san-pol", "https://news.yahoo.co.jp/pickup/6284617", "https://news.yahoo.co.jp/hl?c=dom", "https://news.yahoo.co.jp/", "https://headlines.yahoo.co.jp/article?a=20180601-00223078-toyo-soci", "https://news.yahoo.co.jp/topics", "https://headlines.yahoo.co.jp/videonews/", "https://news.yahoo.co.jp/hl?c=c_sci", "https://news.yahoo.co.jp/list/", "https://headlines.yahoo.co.jp/videonews/jnn?a=20180531-00000058-jnn-soci", "https://headlines.yahoo.co.jp/hl?a=20180601-00003574-tokaiv-l23", "https://about.yahoo.co.jp/docs/info/terms/", "https://www.yahoo-help.jp/app/home/p/575/", "https://about.yahoo.co.jp/info/msiesp/", "https://news.yahoo.co.jp/hl?c=c_life", "https://news.yahoo.co.jp/flash", "https://headlines.yahoo.co.jp/hl?a=20180601-00226047-nksports-socc", "https://news.yahoo.co.jp/pickup/6284619", "https://headlines.yahoo.co.jp/docs/tokuteisho.html", "https://news.yahoo.co.jp/byline/oshimakazuto/20180531-00085867/", "https://news.yahoo.co.jp/hl?c=bus", "https://headlines.yahoo.co.jp/hl?a=20180531-00000001-withnews-sci", "https://headlines.yahoo.co.jp/hl?a=20180601-00010002-khbv-l04", "https://www.yahoo-help.jp/app/answers/detail/a_id/43880/p/533/", "https://news.yahoo.co.jp/byline/fuwaraizo/20180531-00085864/", "https://feedback.ms.yahoo.co.jp/voc/news-voc/input", "https://news.yahoo.co.jp/hl?c=c_int", "https://news.yahoo.co.jp/pickup/6284621", "https://news.yahoo.co.jp/zasshi", "https://headlines.yahoo.co.jp/article?a=20180601-00055904-gendaibiz-bus_all", "https://www.yahoo-help.jp/app/answers/detail/p/575/a_id/60137", "https://news.yahoo.co.jp/pickup/6284618"]
改変を加える前は動いていたのか、どう改変したのか、MeCab.Taggerが正常に動くことは確認しているのか、などが知りたいです。
mecab.taggerは正常に動きます。プログラム更新したので一度見ていただいてもよろしいですか?
改変を加える前は動いていたのか、どう改変したのか、って書きましたよね? あと"やりたいこと"が書いてないですよ。単語ngramを1gramから10gramまでの範囲でカウントしたい? のかなと想像しますが、それはこちらの勝手な想像でしかないわけです。コードが**そうなってない**状態なので。
改変を加える前は動かしてもいません。そもそもネット上に落ちていたputhon2系のプログラムを3系にまず書き換えました。importの部分で足りない部分を追加したことくらいです。quiquiさんのおっしゃる通りで1-10gramでカウントしていきたいです。今回はサンプルのためyahooのサイトで行なっているだけであります。コードがそうなっていないというのも私には現状理解できていない状態なのでわかりやすく教えていただけるとありがたいです。
パッとみる範囲でsplitを呼んでないとか、Comment, Declarationが定義されてないとか、読まなくても不完全なソースであることはすぐにわかります。どういう動作を望んで書いているのかわからないです。
webページ上の文章をngramの機能を使いでどういう単語が多く頻出されているかを見ていきたいという目的でやっています。
やりたいことはわかりました(私には)。が、ここに書いてもしょうがないですよ。今おかしいのが、webからの取得とhtmlのparseなのか、形態素解析なのか、ngramを獲得するところなのか、頻度をカウントしているところなのか、どこまで確認したのか、あたりも書くといいと思います。
quiqui様私の浅はかな知識に質問の仕方を細かく教授いただきありがとうございました。質問する立場としてもう少し学習するとともに、分かりやすくできるよう努力します。
いや、質問を書きかえるか追記しましょうってことなんですけど。ここをコメント欄と勘違いしてませんか?

回答1件
あなたの回答
tips
プレビュー