Я работаю над реализацией программы, которая даст мне результат для Позиционно-инвертированного индекса XML-файла.Сначала мне нужно изменить тип номера документа со строки на int, чтобы использовать его позже.
Вот мой код:
def index(document_directory, dictionary_file, postings_file):
# preprocess docID list
docID_list = [int(docID_string) for docID_string in os.listdir(document_directory)]
docID_list.sort()
stemmer = PorterStemmer()
stopwords = nltk.corpus.stopwords.words('english')
# stopwords = set(stopwords.words('english'))
docs_indexed = 0 # counter for the number of docs indexed
dictionary = {} # key: term, value: docIDs containing term (incudes repeats)
# for each document in corpus
for docID in docID_list:
if (LIMIT and docs_indexed == LIMIT): break
.
.
.
.
.
# open files for writing
dict_file = codecs.open(dictionary_file, 'w', encoding='utf-8')
post_file = open(postings_file, 'wb')
.
.
.
.
# close files
dict_file.close()
post_file.close()
.
.
.
.
"""
prints the proper command usage
"""
def print_usage():
print ("usage: " + sys.argv[0] + "-i directory-of-documents -d dictionary-file -p postings-file")
.
.
.
if (RECORD_TIME): start = timeit.default_timer() # start time
index(document_directory, dictionary_file, postings_file) # call the indexer
if (RECORD_TIME): stop = timeit.default_timer() # stop time
if (RECORD_TIME): print ('Indexing time:' + str(stop - start)) # print time taken
Теперь, когда я его запускаюиспользуя команду:
$ python def_ind.py -i "./index/" -d "output1111.txt" -p "output222.txt"
Iполучить следующую ошибку:
Traceback (most recent call last):
File "def_ind.py", line 161, in <module>
index(document_directory, dictionary_file, postings_file) # call the indexer
File "def_ind.py", line 36, in index
docID_list = [int(docID_string) for docID_string in os.listdir(document_directory)]
File "def_ind.py", line 36, in <listcomp>
docID_list = [int(docID_string) for docID_string in os.listdir(document_directory)]
ValueError: invalid literal for int() with base 10: '.DS_Store'
Я понимаю, что есть строка, которая не может быть int, но я не знаю, как?Что я должен здесь делать?
Я пытаюсь получить вывод, который будет проверять каждое слово, сколько раз появилось в каждом номере документа и в какой строке.например: (номер документа: номер строки, в которой найдено слово)
and:
2: 5,7
5: 5
flower:
1: 8
2: 4,6,8
3: 6
4: 6
5: 6
снимок из моего xml-файла:
<DOCNO>1</DOCNO>
<PROFILE>_AN-BENBQAD8FT</PROFILE>
<DATE>910514
</DATE>
<HEADLINE>
FT 14 MAY 91 / (CORRECTED) Jubilee of a jet that did what it was designed
to do
</HEADLINE>
<TEXT>
words, words, words
</TEXT>
<PUB>The Financial Times
</PUB>
<PAGE>
London Page 7 Photograph (Omitted).
</PAGE>
</DOC>`
Я использую Python 3.7.
Примечание: я нашел много вопросов с той же ошибкой, но ни один из них не подходил для моей ситуации.