pythonpython-3.xnltkpython-newspapernewspaper3k

How to use txt file instead of Article ? (Python)


I wrote a code for reading article and self-learning AI. First, I read the article with URL and download it.Then I parse the article and use it for my AI's learning text.But now I want to read text from txt file.How can I assign txt file's text to Article object ? (Please check code to clear my wish ) Thanks all.

article = Article('URL for article')
article.download()
article.parse()
article.nlp()
corpus = article.text

text= corpus
sentence_list = nltk.sent_tokenize(text)

PS:I want to use txt file instead of URL. But I do not remove article from code because it will be necessary again.


Solution

  • I tried many options but only way is that not using Article.So I remove Article parts and change like this :

    f = open("visp.txt", "r",encoding="utf8")
    
    #article = Article('visp.txt')
    #article.download()
    #article.parse()
    #article.nlp()
    #corpus = article.text
    
    
    f = open("demo.txt", "r",encoding="utf8")
    corpus = f.read()
    print(corpus)
    
    

    PS: Do not forget encoding while open the .txt file.