2017-04-09 1 views
-3

Hi ~ Ich habe ein Problem, während ich versuche, Facebook-Kommentare im CSV-Format zu tokenisieren. Ich habe meine CSV-Daten bereit und lese die Datei vollständig.Ich bin ein Newb in Python und Data Mining. Probleme mit Tokenizer- und Dateitypproblemen haben

Ich verwende Anaconda3; Python 3.5. (My CSV-Daten hat ca. 20k in Zeilen und 1 in cols)

Die Codes sind,

import csv 
from nltk import sent_tokenize, word_tokenize as sent_tokenize, word_tokenize 
with open('facebook_comments_samsung.csv', 'r') as f: 
    reader = csv.reader(f) 
    your_list = list(reader) #list(reader) 
print (your_list) 

Was als Ergebnis kommt, ist so etwas wie dieses:


[['comment_message'], ['b"Yet again been told a pack of lies by Samsung Customer services who have lost my daughters phone and couldn\'t care less. ANYONE WHO PURCHASES ANYTHING FROM THIS COMPANY NEEDS THEIR HEAD TESTED"'], ["b'You cannot really blame an entire brand worldwide for a problem caused by a branch. It is a problem yes, but address your local problem branch'"], ["b'Haha!! Sorry if they lost your daughters phone but I will always buy Samsung products no matter what.'"], ["b'Salim Gaji BEST REPLIE EVER \\xf0\\x9f\\x98\\x8e'"], ["b'<3 Bewafa zarge <3 \\r\\n\\n \\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x93\\r\\n\\xf0\\x9f\\x8e\\xad\\xf0\\x9f\\x91\\x89 AQIB-BOT.ML \\xf0\\x9f\\x91\\x88\\xf0\\x9f\\x8e\\xadMANUAL\\xe2\\x99\\xaaKing.Bot\\xe2\\x84\\xa2 \\r\\n\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x94\\xe2\\x80\\x93\\xe2\\x80\\x94'"], ["b'\\xf0\\x9f\\x8c\\x90 LATIF.ML \\xf0\\x9f\\x8c\\x90'"], ['b"I\'m just waiting here patiently for you guys to say that you\'ll be releasing the s8 and s8+ a week early, for those who pre-ordered. Wishful thinking \\xf0\\x9f\\x98\\x86. Can\'t wait!"'], ['b"That\'s some good positive thinking there sir."'], ["b'(y) #NextIsNow #DoWhatYouCant'"], ["b'looking good'"], ['b"I\'ve always thought that when I first set eyes on my first born that I\'d like it to be on the screen of a cameraphone at arms length rather than eye-to-eye while holding my child. Thank you Samsung for improving our species."'], ["b'cool story'"], ["b'I believe so!'"], ["b'superb'"], ["b'Nice'"], ["b'thanks for the share'"], ["b'awesome'"], ["b'How can I talk to Samsung'"], ["b'Wow'"], ["b'#DoWhatYouCant siempre grandes innovadores Samsung Mobile'"], ["b'I had a problem with my s7 edge when I first got it all fixed now. However when I went to the Samsung shop they were useless and rude they refused to help and said there is nothing they could do no wonder the shop was dead quiet'"], ["b'Zeeshan Khan Masti Khel'"], ["b'I dnt had any problem wd my phn'"], ["b'I have maybe just had a bad phone to start with until it got fixed eventually. I had to go to carphone warehouse they were very helpful'"], ["b'awesome'"], ["b'Ch Shuja Uddin'"], ["b'akhheeerrr'"], ["b'superb'"], ["b'nice story'"], ["b'thanks for the share'"], ["b'superb'"], ["b'thanks for the share'"], ['b"On February 18th 2017 I sent my phone away to with a screen issue. The lower part of the screen was flickering bright white. The phone had zero physical damage to the screen\\n\\nI receive an email from Samsung Quotations with a picture of my SIM tray. Upon phoning I was told my SIM tray was stuck inside the phone and was handed a \\xc2\\xa392.14 repair bill. There is no way that my SIM tray was stuck in the phone as I removed my SIM and memory card before sending the phone away.\\n\\nAfter numerous calls I finally gave in and agreed to pay the \\xc2\\xa392.14 on the understanding that my screen repair would also be covered in this cost. This was confirmed to me by the person on the phone.\\n\\nOn 
  • Entschuldigung für Ihre Unannehmlichkeiten beim Lesen des Ergebnisses. Mein Fehler.

fortzusetzen, fügte ich hinzu,

tokens = [word_tokenize(i) for i in your_list] 
for i in tokens: 
print (i) 

print (tokens) 

Dies ist der Teil, wo ich die folgende Fehlermeldung erhalten:

C:\Program Files\Anaconda3\lib\site-packages\nltk\tokenize\punkt.py in _slices_from_text(self, text) in line 1278 TypeError: expected string or bytes-like object 

Was will ich Nächste ist,

Und schließlich möchte ich eine Wordcloud basierend auf den Daten zeichnen.

Da Sie sich der Tatsache bewusst sind, dass jede Sekunde Ihrer Zeit kostbar ist, tut es mir schrecklich leid, Sie um Hilfe zu bitten. Ich arbeite seit ein paar Tagen daran und finde nicht die richtige Lösung für mein Problem. Danke fürs Lesen.

+1

Hallo. Willkommen bei der Python-Entwicklung. Bitte vermeiden Sie, Ihre Fragen so zu benennen. Der Titel ist zu lang und enthält einige unwichtige Informationen. Wie auch immer, ich werde deine Frage vorerst nicht ablehnen. Ich hoffe, dass Sie bald eine Antwort bekommen. – fameman

+0

@fameman Thx. Ich versuche mich an Stack anzupassen. Will mein Bestes geben und Danke für Ihren Rat. –

+0

Entschuldigung, dass ich einen Fehler gemacht habe, indem ich die Originaldaten erklärt habe. Die Daten bestehen aus 20k in Spalten und 1 in Zeile thx. –

Antwort

0

Der Fehler, den Sie bekommen, liegt daran, dass Ihre CSV-Datei in eine Liste von Listen umgewandelt wird - eine für jede Zeile in der Datei. Die Datei enthält nur eine Spalte. Daher hat jede dieser Listen ein Element: Die Zeichenfolge, die die Nachricht enthält, die Sie mit dem Token versehen möchten. Um über die Fehlermeldung zu erhalten, entpacken Sie die Unterpunkte durch diese Zeile anstelle:

tokens = [word_tokenize(row[0]) for row in your_list] 

Danach müssen Sie etwas mehr Python lernen und erfahren, wie Sie Ihr Programm und Ihre Variablen zu untersuchen.

+0

Danke für Ihren Rat, ich habe den Fehler dankbar weitergegeben. Allerdings habe ich immer noch Schwierigkeiten, "unhashable type: list" zu sagen. Mein Code ist wie folgt: Typ (Token) # Liste /// en = nltk.Text (Token) /// Drucken (len (en.tokens)) # 19904 /// de.plot (50) # FEHLER –

+0

@Alex, 'tokens' ist jetzt eine Liste von Wortlisten. 'nltk.Text' erwartet eine flache Folge von Token. [Hier] (http://stackoverflow.com/q/952914/699305) ist, wie Sie Ihre Liste von Listen reduzieren. PS. Bitte folgen Sie meinen Ratschlägen und gehen Sie durch einige grundlegende Python-Tutorials (und dann das NTLK-Buch, das Sie Schritt für Schritt durch die Vorgehensweise führt). – alexis