2

我使用 nltk 来获取标记化关键字的列表。输出是

['Natural', 'Language', 'Processing', 'with', 'PythonNatural', 'Language', 'Processingwith', 'PythonNatural', 'Language', 'Processing', 'with', 'Python', 'Editor', ':', 'Production', 'Editor', ':', 'Copyeditor']

我有一个文本文件 keyword.txt,其中包含以下关键字:

Processing
Editor
Pyscripter
Language
Registry
Python

如何将通过标记化获得的关键字与我的 keyword.txt 文件匹配,以便为匹配的关键字创建第三个文件。

这是我一直在研究的程序,但它创建了这两个文件的联合:

import os
with open(r'D:\file3.txt', 'w') as fout:
  keywords_seen = set()
  for filename in r'D:\File1.txt', r'D:\Keyword.txt':
    with open(filename) as fin:
        for line in fin:
            keyword = line.strip()
            if keyword not in keywords_seen:
                fout.write(line + "\n")
                keywords_seen.add(keyword)
4

1 回答 1

1

如何匹配从标记化获得的关键字与我的 keyword.txt 文件,以便为匹配的关键字创建第三个文件

这是一个简单的解决方案,根据需要调整文件名。

# these are the tokens:
tokens = set(['Natural', 'Language', 'Processing', 'with', 'PythonNatural', 'Language', 'Processingwith', 'PythonNatural', 'Language', 'Processing', 'with', 'Python', 'Editor', ':', 'Production', 'Editor', ':', 'Copyeditor'])

# create a set containing the keywords
with open('keywords.txt', 'r') as keywords:
    keyset = set(keywords.read().split())

# write outputfile
with open('matches.txt', 'w') as matches:
    for word in keyset:
        if word in tokens:
            matches.write(word + '\n')

这将生成一个matches.txt包含单词的文件

Language
Processing
Python
Editor
于 2014-05-29T09:15:18.913 回答