1. 安装必要的工具与库在Ubuntu系统中,首先需要安装Python环境(建议使用Python 3.6+)及NLP所需的库。打开终端,执行以下命令:

Ubuntu Python如何进行自然语言处理

2. 文本预处理:NLP的基础步骤文本预处理是将原始文本转换为适合分析的格式,主要包括分词、去停用词、词干提取/词形还原:

3. 关键NLP任务实现

(1)词性标注(POS Tagging)

识别文本中每个单词的词性(如名词、动词、形容词)。使用NLTK或spaCy:

# NLTKimport nltkfrom nltk.tokenize import word_tokenizefrom nltk import pos_tagtokens = word_tokenize(text)pos_tags = pos_tag(tokens)# 输出:[('Natural', 'JJ'), ('Language', 'NNP'), ('Processing', 'NNP'), ('is', 'VBZ'), ('fascinating', 'JJ'), ('.', '.')]
# spaCyfor token in doc:print(token.text, token.pos_)# 输出:Natural NOUN, Language PROPN, Processing PROPN, is AUX, fascinating ADJ, . PUNCT

(2)命名实体识别(NER)

识别文本中的实体(如人名、地名、组织名)。使用NLTK或spaCy:

# NLTKimport nltkfrom nltk import ne_chunk, pos_tag, word_tokenizetokens = word_tokenize(text)pos_tags = pos_tag(tokens)ner_tree = ne_chunk(pos_tags)# 输出:(S (ORGANIZATION Natural) (ORGANIZATION Language) (ORGANIZATION Processing) is fascinating .)
# spaCyfor ent in doc.ents:print(ent.text, ent.label_)# 若文本中有人名/地名,会输出对应实体及类型(如"Apple" → ORG)

(3)情感分析

判断文本的情感倾向(积极、消极、中性)。使用TextBlob:

from textblob import TextBlobblob = TextBlob("I love Python. It's amazing!")sentiment = blob.sentiment# 输出:Sentiment(polarity=0.8, subjectivity=0.75)# polarity范围[-1,1],>0为积极,<0为消极;subjectivity范围[0,1],>0.5为主观

(4)主题建模(LDA)

发现文本中的隐藏主题。使用Gensim:

from gensim import corpora, modelsfrom nltk.tokenize import word_tokenizefrom nltk.corpus import stopwords# 准备语料库texts = ["Python is great for data analysis.", "Data science requires Python skills.", "Machine learning uses Python libraries."]tokens = [word_tokenize(text.lower()) for text in texts]stop_words = set(stopwords.words('english'))filtered_tokens = [[word for word in token if word.isalpha() and word not in stop_words] for token in tokens]# 创建词典和语料库dictionary = corpora.Dictionary(filtered_tokens)corpus = [dictionary.doc2bow(text) for text in filtered_tokens]# 训练LDA模型(2个主题)lda_model = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=2, passes=10)topics = lda_model.print_topics()# 输出每个主题的关键词及权重for topic in topics:print(topic)

4. 进阶:如果想再往前走一步,就可以直接用预训练模型,比如 BERT。Hugging Face 的transformers库已经封装好了现成的 BERT 模型,拿来做文本分类、问答这类任务会方便很多:

from transformers import pipeline# 加载预训练的情感分析模型classifier = pipeline("sentiment-analysis")result = classifier("I love Ubuntu and Python!")# 输出:[{'label': 'POSITIVE', 'score': 0.9998}]

注意事项

本文转载于:https://www.yisu.com/ask/69767474.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。