This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/python | |
import MeCab | |
import math | |
def mecab(text): | |
tagger = MeCab.Tagger('-Ochasen') | |
node = tagger.parseToNode(text) | |
morphemes = [] | |
while node: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import chain | |
l = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] | |
all_doc = list(chain.from_iterable(l)) | |
# こんなのもある | |
sum(l, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def term_score(topics): | |
labels_matrix = [[v for w, v in t] for t in topics] | |
beta_hat = numpy.array([numpy.array([w for w, v in t]) for t in topics]) | |
gmean = scipy.stats.gmean(beta_hat) | |
gmean_m = numpy.tile(gmean, (len(beta_hat), 1)) | |
ts = beta_hat * numpy.log(beta_hat / gmean_m) | |
new_topics = [] | |
for i in range(len(labels_matrix)): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import MeCab | |
import gensim | |
def tokenizer(s): | |
uni = s.encode('utf-8') | |
tagger = MeCab.Tagger("-Ochasen") | |
node = tagger.parseToNode(uni) | |
terms = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
def extract_date(s): | |
date_pattern = re.compile('(\d{4})/(\d{1,2})/(\d{1,2})') | |
result = date_pattern.search(s) | |
if result: | |
y, m, d = result.groups() | |
return {'year': y, 'month': m, 'day': d} | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'google-translate) | |
(global-set-key "\C-ct" 'google-translate-at-point-auto) | |
(defun set-google-translate-language (source target) | |
(custom-set-variables | |
'(google-translate-default-source-language source) | |
'(google-translate-default-target-language target) | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import numpy as np | |
import scipy.spatial.distance | |
if __name__ == '__main__': | |
x = np.array([1, 1, 1, 1, 1]) | |
y = np.array([1, 0, 1, 0, 1]) | |
z = np.array([0, 1, 0, 0, 0]) | |
print 1 - scipy.spatial.distance.cosine(x, y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import numpy as np | |
def cos_sim(v1, v2): | |
return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) | |
if __name__ == '__main__': | |
x = np.array([1, 1, 1, 1, 1]) | |
y = np.array([1, 0, 1, 0, 1]) | |
z = np.array([0, 1, 0, 0, 0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print sum(1 for _ in re.finditer(pattern, text)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
data = {"hoge": 1, "hige": 2, "huge" 3} | |
json_data = json.dumps(data, ensure_ascii=False, indent=2) | |
print json_data |
OlderNewer