我的脚本的目的是转到目录,遍历目录中的文件,从每个文件中删除重复的句子,并将每个文件的结果保存到新的命名文件。
这是我用来测试脚本的文件:
这是一个带有大写字母的句子。这是一句话 用环境这个词。这是一个带有单词的句子 环境。这是一个带有环境一词的句子 它。这是一个带有环境一词的句子。
这是一个带有卡车一词的句子。这是一句话 用环境这个词。这是一个带有单词的句子 环境。这是一个带有环境一词的句子 它。这是一个带有环境一词的句子。
这是输出文件:
这是一个带有“资本”一词的句子。这是一个句子 用环境中的单词环境。这是一个带有单词的句子 卡车在里面。这是一个带有环境一词的句子。
问题似乎与输入文件的标记化形式有关。将标记化文件打印到屏幕显示以下内容:
[‘This is a sentence with the word capital in it.’,’This is a sentence with
the word environment in it.’,’This is a sentence with the word environment in
it.’,’This is a sentence with the word environment in it.’This is a sentence
with the word truck in it.’,’ This is a sentence with the word environment in
it.’,’This is a sentence with the word environment in it.’,’This is a sentence
with the word environment in it.’,’This is a sentence with the word
environment in it.\n’]
看来sent_tokenizer在最后一行输入的末尾添加了一个\ n新行符号。最后具有\ n的句子不会被识别为副本,也不会被删除。
对于正在发生的事情以及我可以采取哪些措施来解决问题的任何见解都表示赞赏。
import os, os.path,re, pprint,sys, nltk
from nltk.tokenize import sent_tokenize, word_tokenize
for dirPath, subdirNames, fileList in os.walk(cwd):
for filename in fileList:
with open (filename,'r') as fin:
FileinA=(fin.read())
FileinB=nltk.sent_tokenize(FileinA)
print ('2 Tokenized File')
print (FileinB)
print (' ')
#create basename by deleting the file name suffix
basename=os.path.splitext(filename)[0]
print (' 3 Basename')
print (' ')
print (basename)
print(' ')
sent_seen = set() # holds lines already seen
with open (basename+'XXX'+'.txt', 'a') as filetowrite:
for sent in FileinB:
if sent not in sent_seen:
filetowrite.write(sent)
print (' ')
sent_seen.add(sent)
print (sent_seen)
答案 0 :(得分:0)
如果句子的开头和结尾空格无关紧要,您可以添加一行<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
来摆脱sent = sent.strip()
以及任何其他潜在的尾随空格。
\n