-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.py
89 lines (72 loc) · 2.83 KB
/
package.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import re
import pandas as pd
# 返り値:アルファベットのみの単語リスト
def alpha_word(string):
alphabet = []
splited = string.split(' ')
for raw in splited:
word = ''
for chr in raw:
if chr.isalpha():
word += chr
alphabet.append(word)
return alphabet
#返り値:アルファベットのみで空白を削除した1文
def alpha_seq(string):
# string = string.replace(' ','')
seq = ''.join([chr for chr in string if chr.isalpha()])
return seq
#返り値:単語n-gram
def word_ngram(num, seq):
wn_gram = []
string = alpha_word(seq)
for i in range(len(string)-num+1):
# print(str(i)+' : to append : '+str(string[i:i+num]))
wn_gram += [string[i:i+num]]
return wn_gram
#返り値:文字n-gram
def chr_ngram(num, seq):
cn_gram = []
string = alpha_seq(seq)
for i in range(len(string)-num+1):
cn_gram += [string[i:i+num]]
return cn_gram
#返り値:暗号(9)
def cipher(seq):
return ''.join([chr(219 - ord(c)) if c.islower() else c for c in seq])
# 協調マークアップの削除
def clean_markup(seq):
pattern = r'''
(\'{2,5})
(.*?)
(\1)
'''
cleaned = re.sub(pattern, r'\2', seq, flags = re.MULTILINE+re.VERBOSE)
return cleaned
# 内部リンクマークアップの削除 #27
# ここが理解できない!!
def clean_ILmarkup(seq):
pattern = r'''
\[\[ # '[['(マークアップ開始)
(?: # キャプチャ対象外のグループ開始
[^|]*? # '|'以外の文字0文字以上、非貪欲(ここがわからん!!!)
\| # '|'
)?? # グループ終了、このグループが0か1出現、非貪欲
( # グループ開始、キャプチャ対象
(?!Category:) # 否定の先読(含んだ場合は対象外としている)
([^|]*?) # '|'以外が0文字以上、非貪欲(表示対象の文字列)
)
\]\] # ']]'(マークアップ終了)
'''
cleaned = re.sub(pattern, r'\1', seq, flags = re.MULTILINE+re.VERBOSE)
return cleaned
def read_text():
# 0:表層形(surface)
# 1:品詞(pos)
# 2:品詞細分類1(pos1)
# 7:基本形(base)
df = pd.read_table('/Users/ishiiasuka/Documents/GitHub/NLP100/neko2.txt.mecab', sep='\t|,', header=None,
usecols=[0, 1, 2, 7], names=['surface', 'pos', 'pos1', 'base'],
skiprows=4, skipfooter=1 ,engine='python')
# 本当は空白はpos1だが、ずれてしまっている
return df[df['pos'] != '空白']