1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python: 正規表現による簡易版形態素解析

1
Posted at

SAMPLE

吾輩||||ある||名前||まだ|||

REFERENCE

正規表現による手抜き形態素解析

PYTHON

微妙に改変。

text_m = []
text = "吾輩は猫である。名前はまだ無い。"
p = re.compile(r"/|[A-Z]+|[a-z]+|[ァ-ンー]+|[ぁ-ん-]+|[ァ-ヶ]+|[一-龍]+|[。、]|/")
m = p.findall(text)
for row in m:
   if re.compile(r'^[あ-ん]+$').fullmatch(row):
      if row[0] in 'はがのにへともでを':
         prefix = row[0]
         token = row[1:]
         text_m.append(prefix)
         if (len(token)>0):
            text_m.append(token)
      elif row[-2:] in 'のでからまで':
         token = row[0:-2]
         suffix = row[-2:]
         text_m.append(token)
         text_m.append(suffix)
      elif row[-1:] in 'もはがでを':
         token = row[0:-1]
         suffix = row[-1:]
         text_m.append(token)
         text_m.append(suffix)
      else:
         text_m.append(row)
   else:
      text_m.append(row)

## output
'|'.join(text_m)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?