2016-07-06 10 views
1

Ich bin auf der Suche nach einem Satz in Token teilen, aber ignorieren 2 spezifische Zeichenfolgen und Leerzeichen auch ignorieren.Python .split() auf einer Zeichenfolge auf jeder teilbaren Token-Balken Whitespace, aber einige spezifische Zeichenfolgen ignorieren

Zum Beispiel:

GNI per capita ; PPP -LRB- US dollar -RRB- in LOCATION_SLOT was last measured at NUMBER_SLOT in 2011 , according to the World Bank .

Sollte in [GNI,per,capita,;,PPP,-,LRB,-,US,dollar,-,RRB,-,in, LOCATION_SLOT,was,last,measured,at,NUMBER_SLOT,in,2011,,,according,to, the, World,Bank,.,] aufgeteilt werden.

Ich möchte nicht LOCATION_SLOT oder NUMBER_SLOT aufgeteilt werden, zum Beispiel die ehemalige in [LOCATION,_,SLOT]. Aber ich möchte Punkte berücksichtigen.

Meine aktuelle Funktion, die nur Zeichen basierend Wörter erlaubt, sondern Zahlen und Dinge wie ;,,,: Entfernen etc hier ist - ich will es nicht diese entfernen:

def sentence_to_words(sentence,remove_stopwords=False): 
    letters_only = re.sub("[^a-zA-Z| LOCATION_SLOT | NUMBER_SLOT]", " ", sentence) 
    words = letters_only.lower().split() 
    if remove_stopwords: 
      stops = set(stopwords.words("english")) 
      words = [w for w in words if not w in stops] 
    return(words) 

Dies erzeugt diese Token:

gni per capita ppp lrb us dollar rrb location_slot last measured number_slot according world bank

+0

einerseits sagen Sie, dass es ein Leerzeichen vor und nach ';' gibt, aber Sie berücksichtigen nicht Leerzeichen in '-' kurz nach' RRB' – rock321987

+0

Sorry, ich hätte klären sollen, die Leerzeichen in meinem Split, Da sie nicht zwischen '' '' 'liegen, sind sie eigentlich keine Leerzeichen. –

Antwort

1

Sie re.findall verwenden und Streifen, die Räume von Start-und

endet
>>> [x.strip() for x in re.findall('\s*(\w+|\W+)', line)] 
#['GNI', 'per', 'capita', ';', 'PPP', '-', 'LRB', '-', 'US', 'dollar', '-', 'RRB', '-', 'in', 'LOCATION_SLOT', 'was', 'last', 'measured', 'at', 'NUMBER_SLOT', 'in', '2011', ',', 'according', 'to', 'the', 'World', 'Bank', '.'] 

Regex Erläuterung

> \w matches word character [A-Za-z0-9_]. 
> \W is negation of \w. i.e. it matches anything except word character. 
1

Sie können einfach Split verwenden

>>> x = "GNI per capita ; PPP -LRB- US dollar -RRB- in LOCATION_SLOT was last measured at NUMBER_SLOT in 2011 , according to the World Bank ." 
>>> 
>>> x.split() 
['GNI', 'per', 'capita', ';', 'PPP', '-LRB-', 'US', 'dollar', '-RRB-', 'in', 'LOCATION_SLOT', 'was', 'last', 'measured', 'at', 'NUMBER_SLOT', 'in', '2011', ',', 'according', 'to', 'the', 'World', 'Bank', '.'] 

Um das zu entfernen - um -LBR- dies tun:

>>> z = [y.strip('-') for y in x] 
>>> z 
['GNI', 'per', 'capita', ';', 'PPP', 'LRB', 'US', 'dollar', 'RRB', 'in', 'LOCATION_SLOT', 'was', 'last', 'measured', 'at', 'NUMBER_SLOT', 'in', '2011', ',', 'according', 'to', 'the', 'World', 'Bank', '.'] 
>>> 

Wenn Sie behalten möchten die Bindestriche: kann

>>> y = [] 
>>> for item in x: 
... if item.startswith('-') and item.endswith('-'): 
...  y.append(',') 
...  y.append(item.strip('-')) 
...  y.append('-') 
... else: 
...  y.append(item) 
... 
+0

Ah, wie dumm. Ich wusste nicht, dass split() nicht auf '_' aufteilt. –

+0

Frage, hier teilen wir '-RRB-' nicht in 3 Teile auf, wie würde ich das einbeziehen? –

+0

zweite wird entfernen '-' was ist nicht was OP will – rock321987

Verwandte Themen