Python Forum
How to store the resulting Doc objects into a list named A
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to store the resulting Doc objects into a list named A
#1
Hey folks. I am a real new beginner for python and NLP.
I am stuck with this question: Store the resulting Doc objects into a list name A"
Is it possible to store it by using "A=list(doc)". How could it still remain doc objects under list? Thank you so much!

This is my exercise:
1. Load text files from a directory and read their contentsΒΆ
The directory data contains a subdirectory named sotu with five State of the Union speeches from various presidents of the United States, which are stored as UTF-8 encoded plain text files.

Import the pathlib module and use the module to read the contents of each text file into string objects.

Then import the spacy library and load a small language model for English. Assign the model under the variable nlp.

Process the texts using the language model and store the resulting Doc objects into a list named speeches.

And my answer:
from pathlib import Path
corpus_dir = Path ("data/sotu")
files = list(corpus_dir.glob(pattern='*.txt'))
for file in files:
    text = file.read_text(encoding='utf-8')
    import spacy
    nlp=spacy.load('en_core_web_sm')
    doc=nlp(text)
    speeches=list(doc)
Reply
#2
Generally you should put imports at the start of your program. There's no reason to put the import inside a loop where it is run multiple times.

To create a list of things, generally you can append() each of the things to your list. So something like.

speeches = [] # list is created outside the loop
for file in files:
    # create the doc
    speeches.append(doc) # items added to the list inside the loop.
xinyulon likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix With n_samples=0, test_size=0.2 and train_size=None, the resulting train s MrSonoa 2 2,974 Apr-15-2023, 12:02 PM
Last Post: MrSonoa
  Creating list of lists, with objects from lists sgrinderud 7 1,661 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  store all variable values into list and insert to sql_summary table mg24 3 1,164 Sep-28-2022, 09:13 AM
Last Post: Larz60+
Question Keyword to build list from list of objects? pfdjhfuys 3 1,578 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Grouping and sum of a list of objects Otbredbaron 1 3,226 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  Adding a comma in the resulting value stsxbel 6 2,658 May-22-2021, 09:24 PM
Last Post: stsxbel
  Regex text file to store data in list TheSithSiggi 1 1,538 Dec-03-2020, 04:46 PM
Last Post: bowlofred
  Passing List of Objects in Command Line Python usman 7 3,207 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,051 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Organizing list of objects by attribute scarney1988 3 2,236 Mar-11-2020, 03:55 PM
Last Post: scarney1988

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020