【问题标题】:Lemmatizing txt file and replacing only lemmatized words词形化 txt 文件并仅替换词形化的单词
【发布时间】:2018-08-26 18:14:42
【问题描述】:

无法弄清楚如何对 txt 文件中的单词进行词形还原。我已经列出了这些词,但我不知道事后如何对它们进行词形还原。

这是我所拥有的:

import nltk, re
nltk.download('wordnet')
from nltk.stem.wordnet import WordNetLemmatizer

def lemfile():
    f = open('1865-Lincoln.txt', 'r')
    text = f.read().lower()
    f.close()
    text = re.sub('[^a-z\ \']+', " ", text)
    words = list(text.split())

【问题讨论】:

标签: python nltk lemmatization


【解决方案1】:

Lemmatizing txt 文件并仅替换词形化的单词可以作为 --`

import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from pywsd.utils import lemmatize_sentence

lmm = WordNetLemmatizer()
ps = PorterStemmer()

new_data= []

with open('/home/rahul/Desktop/align.txt','r') as f:
f1 = f.read()
f2 = f1.split()
en_stops = set(stopwords.words('english'))
hu_stops = set(stopwords.words('hungarian'))

all_words = f2 
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~[<p>]'''
#if lemmatization of one string is required then uncomment below line
#data='this is coming rahul  schooling met happiness making'
print ()
for line in all_words:
    new_data=' '.join(lemmatize_sentence(line))
    print (new_data)

PS- 根据您的需要进行识别。 希望这会有所帮助!!!

【讨论】:

    【解决方案2】:

    您还可以尝试在 pywsd 包中围绕 NLTK 的WordNetLemmatizer 进行包装,具体来说,https://github.com/alvations/pywsd/blob/master/pywsd/utils.py#L129

    安装:

    pip install -U nltk
    python -m nltk.downloader popular
    pip install -U pywsd
    

    代码:

    >>> from pywsd.utils import lemmatize_sentence
    >>> lemmatize_sentence('These are foo bar sentences.')
    ['these', 'be', 'foo', 'bar', 'sentence', '.']
    >>> lemmatize_sentence('These are foo bar sentences running.')
    ['these', 'be', 'foo', 'bar', 'sentence', 'run', '.']
    

    特别针对您的问题:

    from __future__ import print_function
    from pywsd.util import lemmatize_sentence 
    
    with open('file.txt') as fin, open('outputfile.txt', 'w') as fout
        for line in fin:
            print(' '.join(lemmatize_sentence(line.strip()), file=fout, end='\n')
    

    【讨论】:

      【解决方案3】:

      初始化一个WordNetLemmatizer 对象,并对行中的每个单词进行词形还原。您可以使用 fileinput 模块执行就地文件 I/O。

      # https://stackoverflow.com/a/5463419/4909087
      import fileinput
      
      lemmatizer = WordNetLemmatizer()
      for line in fileinput.input('1865-Lincoln.txt', inplace=True, backup='.bak'):
          line = ' '.join(
              [lemmatizer.lemmatize(w) for w in line.rstrip().split()]
          )
          # overwrites current `line` in file
          print(line)
      

      fileinput.input 在使用时将标准输出重定向到打开的文件。

      【讨论】:

      • 这是否意味着我不必像我一样先列出单词?
      • @ArchivistG 您可能仍需要清理句子(使用正则表达式,为简单起见,我省略了此步骤)。没有要列出的内容,只需对单词进行词形还原并写入您的文件。
      • 所以,我尝试了它,但它并没有完全还原任何东西。它删除了偶尔的 "s" 或 "ss" ,仅此而已。例如,它把“less”变成了“le”。
      • @ArchivistG 如果您查看文档,lemmatize 接受第二个参数,即词性(名词、动词等)。默认情况下,所有单词都是名词,因此带有 -ing 的动词不会进行词形还原,除非您设置 pos='v'。您可以改用lemmatizer.lemmatize(lemmatizer.lemmatize(w), pos='v'),但要注意……它很慢。
      • @ArchivistG 祝你好运!如果您需要更多帮助,请随时 ping(尽管如果这对您不起作用,我不确定我能做些什么:p)
      猜你喜欢
      • 1970-01-01
      • 2011-02-19
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多