как токенизировать текст по nltk python - PullRequest
0 голосов
/ 30 января 2020

У меня есть такой текст:

Exception in org.baharan.dominant.dao.core.nonPlanAllocation.INonPlanAllocationRepository.getAllGrid() 
with cause = 'org.hibernate.exception.SQLGrammarException: could not extract ResultSet'
Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist

я токенизирую этот текст с word_tokenize в python и выдает:

Exception
org.baharan.dominant.dao.core.nonPlanAllocation.INonPlanAllocationRepository.getAllGrid
cause
'org.hibernate.exception.SQLGrammarException
could
extract
ResultSet'
Caused
java.sql.SQLSyntaxErrorException
ORA-00942
table
view
exist

Но, как вы можете видеть, второй Линия выводит несколько слов, которые разбиты вместе. Как отделить их как слово?!

я использую этот python код:

>>> f = open('001.txt')
>>> text = [w for w in word_tokenize(f.read()) if w not in stopwords]

и на самом деле, я хочу, чтобы все слова были разделены следующим образом:

Exception
org
baharan
dominant
dao
core
nonPlanAllocation
INonPlanAllocationRepository
getAllGrid
cause
'org
hibernate
exception
SQLGrammarException
could
extract
ResultSet'
Caused
java
sql
SQLSyntaxErrorException
ORA-00942
table
view
exist

Ответы [ 2 ]

0 голосов
/ 31 января 2020

Я нашел простой способ использования RegexpTokenizer файла nltk.tokenize так:

>>> from nltk.tokenize import RegexpTokenizer
>>> tokenizer = RegexpTokenizer(r'\w+')

Вывод после рассмотрения удаления стоп-слов выглядит следующим образом:

Exception
org
baharan
dominant
dao
core
nonPlanAllocation
INonPlanAllocationRepository
getAllGrid
cause
org
hibernate
exception
SQLGrammarException
could
extract
ResultSet
Caused
java
sql
SQLSyntaxErrorException
ORA-00942
table
view
exist

0 голосов
/ 30 января 2020
f = "Exception in org.baharan.dominant.dao.core.nonPlanAllocation.INonPlanAllocationRepository.getAllGrid() \
with cause = 'org.hibernate.exception.SQLGrammarException: could not extract ResultSet' \
Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist'"
s = ''
f_list = f.replace('.', ' ').split(' ')
for item in f_list:
    #print(item)
    s = s + ' ' + ''.join(item)+'\n'

print(s)

выход

 Exception
 in
 org
 baharan
 dominant
 dao
 core
 nonPlanAllocation
 INonPlanAllocationRepository
 getAllGrid()
 with
 cause
 =
 'org
 hibernate
 exception
 SQLGrammarException:
 could
 not
 extract
 ResultSet'
 Caused
 by:
 java
 sql
 SQLSyntaxErrorException:
 ORA-00942:
 table
 or
 view
 does
 not
 exist'
...