Natural-language-toolkit-word-replacement

提供:Dev Guides
移動先:案内検索

自然言語ツールキット-単語の置き換え

ステミングと見出し語化は、一種の言語学的圧縮と見なすことができます。 同じ意味で、単語の置換はテキストの正規化またはエラー修正と考えることができます。

しかし、なぜ単語の置換が必要なのでしょうか。 トークン化について話すとすると、収縮に関する問題(できない、できないなど)があるとします。 したがって、そのような問題を処理するには、単語の置換が必要です。 たとえば、収縮を拡張された形式に置き換えることができます。

正規表現を使用した単語の置換

最初に、正規表現に一致する単語を置き換えます。 しかし、これには正規表現とpython reモジュールの基本的な理解が必要です。 以下の例では、収縮を拡張された形式に置き換えます(例: 「できない」は「できない」に置き換えられます)、すべて正規表現を使用します。

まず、正規表現を使用するために必要なパッケージreをインポートします。

import re
from nltk.corpus import wordnet

次に、次のように選択した置換パターンを定義します-

R_patterns = [
   (r'won\'t', 'will not'),
   (r'can\'t', 'cannot'),
   (r'i\'m', 'i am'),
   r'(\w+)\'ll', '\g<1> will'),
   (r'(\w+)n\'t', '\g<1> not'),
   (r'(\w+)\'ve', '\g<1> have'),
   (r'(\w+)\'s', '\g<1> is'),
   (r'(\w+)\'re', '\g<1> are'),
]

今、単語を置き換えるために使用できるクラスを作成します-

class REReplacer(object):
   def __init__(self, pattern = R_patterns):
      self.pattern = [(re.compile(regex), repl) for (regex, repl) in patterns]
   def replace(self, text):
      s = text
      for (pattern, repl) in self.pattern:
         s = re.sub(pattern, repl, s)
      return s

このpythonプログラム(たとえばrepRE.py)を保存し、pythonコマンドプロンプトから実行します。 実行後、単語を置換したい場合はREReplacerクラスをインポートします。 方法を見てみましょう。

from repRE import REReplacer
rep_word = REReplacer()
rep_word.replace("I won't do it")
Output:
'I will not do it'
rep_word.replace("I can’t do it")
Output:
'I cannot do it'

完全な実装例

import re
from nltk.corpus import wordnet
R_patterns = [
   (r'won\'t', 'will not'),
   (r'can\'t', 'cannot'),
   (r'i\'m', 'i am'),
   r'(\w+)\'ll', '\g<1> will'),
   (r'(\w+)n\'t', '\g<1> not'),
   (r'(\w+)\'ve', '\g<1> have'),
   (r'(\w+)\'s', '\g<1> is'),
   (r'(\w+)\'re', '\g<1> are'),
]
class REReplacer(object):
def __init__(self, patterns=R_patterns):
   self.patterns = [(re.compile(regex), repl) for (regex, repl) in patterns]
def replace(self, text):
   s = text
   for (pattern, repl) in self.patterns:
      s = re.sub(pattern, repl, s)
   return s

上記のプログラムを保存して実行したら、クラスをインポートして次のように使用できます-

from replacerRE import REReplacer
rep_word = REReplacer()
rep_word.replace("I won't do it")

出力

'I will not do it'

テキスト処理前の置き換え

自然言語処理(NLP)で作業する際の一般的な方法の1つは、テキスト処理の前にテキストをクリーンアップすることです。 この問題については、テキスト処理の前の準備段階として、前の例で上で作成した REReplacer クラスを使用することもできます。 トークン化。

from nltk.tokenize import word_tokenize
from replacerRE import REReplacer
rep_word = REReplacer()
word_tokenize("I won't be able to do this now")
Output:
['I', 'wo', "n't", 'be', 'able', 'to', 'do', 'this', 'now']
word_tokenize(rep_word.replace("I won't be able to do this now"))
Output:
['I', 'will', 'not', 'be', 'able', 'to', 'do', 'this', 'now']

上記のPythonレシピでは、正規表現置換を使用した場合と使用しない場合の単語トークナイザーの出力の違いを簡単に理解できます。

繰り返し文字の削除

私たちは日常の言語で厳密に文法的になっていますか? ちがうよ。 たとえば、「こんにちは」という言葉を強調するために「Hiiiiiiiiiiii Mohan」と書くことがあります。 しかし、コンピュータシステムは、「Hiiiiiiiiiiii」が単語「Hi」のバリエーションであることを認識していません。 以下の例では、 rep_word_removal という名前のクラスを作成します。これは、繰り返し単語を削除するために使用できます。

まず、正規表現を使用するために必要なパッケージreをインポートします

import re
from nltk.corpus import wordnet

ここで、繰り返し単語を削除するために使用できるクラスを作成します-

class Rep_word_removal(object):
   def __init__(self):
      self.repeat_regexp = re.compile(r'(\w*)(\w)\2(\w*)')
      self.repl = r'\1\2\3'
   def replace(self, word):
      if wordnet.synsets(word):
      return word
   repl_word = self.repeat_regexp.sub(self.repl, word)
   if repl_word != word:
      return self.replace(repl_word)
   else:
      return repl_word

このpythonプログラム(たとえば、removerepeat.py)を保存し、pythonコマンドプロンプトから実行します。 それを実行した後、繰り返し単語を削除したいときに Rep_word_removal クラスをインポートします。 方法を見てみましょうか?

from removalrepeat import Rep_word_removal
rep_word = Rep_word_removal()
rep_word.replace ("Hiiiiiiiiiiiiiiiiiiiii")
Output:
'Hi'
rep_word.replace("Hellooooooooooooooo")
Output:
'Hello'

完全な実装例

import re
from nltk.corpus import wordnet
class Rep_word_removal(object):
   def __init__(self):
      self.repeat_regexp = re.compile(r'(\w*)(\w)\2(\w*)')
      self.repl = r'\1\2\3'
   def replace(self, word):
      if wordnet.synsets(word):
         return word
   replace_word = self.repeat_regexp.sub(self.repl, word)
   if replace_word != word:
      return self.replace(replace_word)
   else:
      return replace_word

上記のプログラムを保存して実行したら、クラスをインポートして次のように使用できます-

from removalrepeat import Rep_word_removal
rep_word = Rep_word_removal()
rep_word.replace ("Hiiiiiiiiiiiiiiiiiiiii")

出力

'Hi'