AttributeError: ‘list’ object heeft geen attribuut ‘lower’ gensim

Ik heb een lijst van 10.000 woorden in een tekstbestand, zoals:

G15
KDN
C30A
Actie Standaard
Luchtpenseel
Luchtverdunning

Ik probeer ze om te zetten in tokens in kleine letters met behulp van deze code voor latere verwerking met GenSim:

data = [line.strip() for line in open("C:\corpus\TermList.txt", 'r')]
texts = [[word for word in data.lower().split()] for word in data]

en ik krijg de volgende callback:

AttributeErrorTraceback (most recent call last)
<ipython-input-84-33bbe380449e> in <module>()
      1 data = [line.strip() for line in open("C:\corpus\TermList.txt", 'r')]
----> 2 texts = [[word for word in data.lower().split()] for word in data]
      3 
AttributeError: 'list' object has no attribute 'lower'

Suggesties over wat ik verkeerd doe en hoe ik dit kan corrigeren, wordt zeer op prijs gesteld!!! Bedankt!!


Antwoord 1, autoriteit 100%

probeer:

data = [line.strip() for line in open("C:\corpus\TermList.txt", 'r')]
texts = [[word.lower() for word in text.split()] for text in data]

u probeerde .lower() toe te passen op gegevens, wat een lijst is.
.lower() kan alleen worden toegepast op tekenreeksen.


Antwoord 2, autoriteit 12%

Je hebt

. nodig

texts = [[word.lower() for word in line.split()] for line in data]

Deze code voor elke linein data([... for line in data]) genereert een lijst met kleine letters ( [word.lower() for word in line.split()]). Elke str linezal een reeks van door spaties gescheiden woorden bevatten.line.split()zal deze reeks in een lijst veranderen. En word.lower()converteert elk woord naar kleine letters.


Antwoord 3

wat je verkeerd doet, is het aanroepen van een stringmethode (lower()) voor een lijst (in jouw geval data)

data = [line.strip() for line in open('corpus.txt', 'r')]

wat u moet doen nadat u regels als lijstinvoer hebt gekregen, is

texts = [[words for words in sentences.lower().split()] for sentences in data]
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*********^^^^^^^^^^^^^^^^^^^^^^*********^^^^
#you should call lower on iter. value - in our case it is "sentences"

dit geeft je een lijst met lijsten. elke lijst bevat de woorden in kleine letters van regels.

$ tail -n 10 corpus.txt 
G15 KDN C30A Action Standard Air Brush Air Dilution
G15 KDN C30A Action Standard Air Brush Air Dilution
G15 KDN C30A Action Standard Air Brush Air Dilution
G15 KDN C30A Action Standard Air Brush Air Dilution
G15 KDN C30A Action Standard Air Brush Air Dilution
G15 KDN C30A Action Standard Air Brush Air Dilution
G15 KDN C30A Action Standard Air Brush Air Dilution
G15 KDN C30A Action Standard Air Brush Air Dilution
G15 KDN C30A Action Standard Air Brush Air Dilution
G15 KDN C30A Action Standard Air Brush Air Dilution
$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> data = [line.strip() for line in open('corpus.txt', 'r')]
>>> texts = [[words for words in sentences.lower().split()] for sentences in data]
>>> texts[:5]
[['g15', 'kdn', 'c30a', 'action', 'standard', 'air', 'brush', 'air', 'dilution'], ['g15', 'kdn', 'c30a', 'action', 'standard', 'air', 'brush', 'air', 'dilution'], ['g15', 'kdn', 'c30a', 'action', 'standard', 'air', 'brush', 'air', 'dilution'], ['g15', 'kdn', 'c30a', 'action', 'standard', 'air', 'brush', 'air', 'dilution'], ['g15', 'kdn', 'c30a', 'action', 'standard', 'air', 'brush', 'air', 'dilution']]
>>> 

zeker dat je het kunt afvlakken of gewoon kunt houden zoals het is.

>>> flattened = reduce(lambda x,y: x+y, texts)
>>> flattened[:30]
['g15', 'kdn', 'c30a', 'action', 'standard', 'air', 'brush', 'air', 'dilution', 'g15', 'kdn', 'c30a', 'action', 'standard', 'air', 'brush', 'air', 'dilution', 'g15', 'kdn', 'c30a', 'action', 'standard', 'air', 'brush', 'air', 'dilution', 'g15', 'kdn', 'c30a']
>>> 

Antwoord 4

We kunnen eenvoudig een lijst omzetten in een kleine, laatste doe dit.

>>> words = ["PYTHON", "PROGRAMMING"]
>>> type((words))
>>> for i in words:
          print(i.lower())

Uitvoer:

python-programmering

Other episodes