Here is my function which is supposed to lemmatize a list of sentences but the output is a list of all words but not a list of each lemmatized sentences.
Code for lemmatize function
tagger = treetaggerwrapper.TreeTagger(TAGLANG='fr')
def lemmatize(corpus):
lemmatize_list_of _sentences= []
lemmatize_list_of _sentences2 = []
for sentence in corpus:
tags = tagger.tag_text(sentence)
tags2 = treetaggerwrapper.make_tags(tags, allow_extra = True)
lemmatize_list_of_sentences.append(tags2)
print(lemmatize_list_of_sentences)
for subl in lemmatize_list_of_sentences: # loop in list of sublists
for word in subl:
if word.__class__.__name__ == "Tag":
lemme=word[2] # I want also to check if lemme[2] is empty and add this
lemmeOption2=lemme.split("|")
lemme=lemmeOption1[0]
lemmatize_list_of_sentences2.append(lemme)
return lemmatize_list_of_sentences2 # should return a list of lists where each list contains the lemme retrieve
lemmatize_train= lemmatize(sentences_train_remove_stop_words)
lemmatize_test= lemmatize(sentences_test_remove_stop_words)
print(lemmatize_train)
Furthermore , i would like to add the lemmatize function a line of code to check if the index(2) or (-1) is empty, and if it is empty retrieve the word at the first index
I come up with this but how can i combine it with my lemmatize function
for word in subl:
lemme= word.split('\t')
try:
if lemme[2] == '':
lemmatize_list_of _sentences2.append(parts[0])
else:
lemmatize_list_of _sentences2.append(parts[2])
except:
print(parts)
list of sentences in the file_input
La période de rotation de la Lune est la même que sa période orbitale et elle présente donc toujours le même hémisphère.
Cette rotation synchrone résulte des frottements qu’ont entraînés les marées causées par la Terre.
After tagging the text, and print the list of sentences_tagging , I have this :
first sentence :
[[Tag(word='la', pos='DET:ART', lemma='le'), Tag(word='période', pos='NOM', lemma='période'), Tag(word='rotation', pos='NOM', lemma='rotation'), Tag(word='lune', pos='NOM', lemma='lune'), Tag(word='période', pos='NOM', lemma='période'), Tag(word='orbitale', pos='ADJ', lemma='orbital'), Tag(word='présente', pos='VER:pres', lemma='présenter'), Tag(word='donc', pos='ADV', lemma='donc'), Tag(word='toujours', pos='ADV', lemma='toujours')]]
whole sentences:
[[Tag(word='la', pos='DET:ART', lemma='le'), Tag(word='période', pos='NOM', lemma='période'), Tag(word='rotation', pos='NOM', lemma='rotation'), Tag(word='lune', pos='NOM', lemma='lune'), Tag(word='période', pos='NOM', lemma='période'), Tag(word='orbitale', pos='ADJ', lemma='orbital'), Tag(word='présente', pos='VER:pres', lemma='présenter'), Tag(word='donc', pos='ADV', lemma='donc'), Tag(word='toujours', pos='ADV', lemma='toujours')], [Tag(word='cette', pos='PRO:DEM', lemma='ce'), Tag(word='rotation', pos='NOM', lemma='rotation'), Tag(word='synchrone', pos='ADJ', lemma='synchrone'), Tag(word='résulte', pos='VER:pres', lemma='résulter'), Tag(word='frottements', pos='NOM', lemma='frottement'), Tag(word='entraînés', pos='VER:pper', lemma='entraîner'), Tag(word='les', pos='DET:ART', lemma='le'), Tag(word='marées', pos='NOM', lemma='marée'), Tag(word='causées', pos='VER:pper', lemma='causer')]]
After retrieving the lemma I have a list of word , which is not what i expected. Expected a list for each sentences.
Output :
['le', 'période', 'rotation', 'lune', 'période', 'orbital', 'présenter', 'donc', 'toujours', 'ce', 'rotation', 'synchrone', 'résulter', 'frottement', 'entraîner', 'le', 'marée', 'causer']
Expected : to have each word of the sentence in a single string with spaces between the word.
['le période rotation lune période orbital présenter donc toujours','ce rotation synchrone résulter frottement entraîner le marée causer']