简体   繁体   English

从python中的文本中提取信息

[英]Extracting information from text in python

I am new to the text mining. 我是文本挖掘的新手。 I have a CSV file. 我有一个CSV文件。 I need to go through each line and extract some information then write them into another CSV file. 我需要遍历每一行并提取一些信息,然后将它们写入另一个CSV文件。 I am looking for specific information which I have in a dictionary. 我正在寻找字典中的特定信息。 Consider below sentence: 考虑下面的句子:

"the application version is 1.8.2 and the variable skt.len passes the required information. file ReadMe.txt has the specifications." “应用程序版本为1.8.2,变量skt.len传递了所需的信息。文件ReadMe.txt具有规范。”

My dictionary is: ["application version", "variable", "file"] 我的字典是:[“应用程序版本”,“变量”,“文件”]

I need to extract: 我需要提取:

  • application version: 1.8.2 应用版本:1.8.2
  • variable: skt.len 变量:skt.len
  • file: ReadMe.txt 档案:ReadMe.txt

What is the best way to extract such information from text? 从文本中提取此类信息的最佳方法是什么? I am playing with NLTK and StanfordCoreNLP features. 我正在使用NLTK和StanfordCoreNLP功能。 But, I could not extract the information yet. 但是,我还不能提取信息。 I am thinking to use regex to extract the application version. 我正在考虑使用正则表达式提取应用程序版本。 Any idea? 任何想法?

PS: I know that this may make the task more complicated. PS:我知道这可能会使任务更加复杂。 But, sentences in each line of the CSV file may have different structures. 但是,CSV文件每一行中的句子可能具有不同的结构。 For example: "application version" in one line, may be "app version" in another line. 例如:一行中的“应用程序版本”,可能在另一行中是“应用程序版本”。 Or "file" in one line may be "filename" in another line. 或者一行中的“文件”可以是另一行中的“文件名”。

I use R and below is one of the way (not the best one but just to show how it works) to extract value of variable: 我使用R,下面是提取变量值的一种方法(不是最好的方法,只是为了展示它是如何工作的):

>> str_extract(text, '(?<=variable\\s)(\\w+)(.)?(\\w+)?')

here text is the entire string which you have shared. 这里的text是您共享的整个字符串。 This gives me an output 这给了我一个输出

>> skt.len

I am sure there are similar functions in Python to get this done and get the output in desired format. 我确信Python中有类似的函数可以完成此操作并以所需的格式获取输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM