📜  自然语言工具包-单词替换

📅  最后修改于: 2020-10-14 09:13:44             🧑  作者: Mango


词干和词根化可以看作是一种语言压缩。从同一个意义上讲,单词替换可以看作是文本规范化或错误纠正。

但是为什么我们需要单词替换呢?假设如果我们谈论令牌化,那么它就会出现收缩问题(例如不能,不会等)。因此,要处理此类问题,我们需要单词替换。例如,我们可以用收缩形式代替收缩。

使用正则表达式替换单词

首先,我们将替换与正则表达式匹配的单词。但是为此,我们必须对正则表达式以及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)时的一种常见做法是在进行文本处理之前清理文本。考虑到这一点,我们还可以将上面示例中创建的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”以强调单词“ Hi”。但是计算机系统不知道“ 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'