从ASP.NET应用程序访问python脚本

时间:2016-12-15 11:04:55

标签: python asp.net nltk

我需要运行一个python脚本,该脚本通过ASP.NET应用程序使用NLTK库。 我尝试使用IronPython,尝试使用cx-freezepy2exe将脚本打包为可执行文件。所有尝试都没有成功。

现在我担心的是将Python脚本作为RESTful API托管。该服务应该在本地计算机上运行。这项任务有哪些可能的方法?

〜编辑〜 这是我想为IronPython添加的代码

#Code modificaiton for standalone python run
#importing libraries
import nltk
import sys
import pandas as pd
import numpy as np    
import re 
import os
from nltk.tokenize import PunktSentenceTokenizer


#Reading the text from console
print("Enter the text - ")
raw = sys.stdin.readline()


sentences = nltk.sent_tokenize(raw)  #split in to sentances
tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences] #split in to words
tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences] #tag sentences with NN, NNP, etc
chunked_sentences = nltk.ne_chunk_sents(tagged_sentences, binary=True)

def extract_entity_names(t):
    entity_names = []

    if hasattr(t, 'label') and t.label:
        if t.label() == 'NE':
            entity_names.append(' '.join([child[0] for child in t]))
        else:
            for child in t:
                entity_names.extend(extract_entity_names(child))

    return entity_names


entity_names = []
for tree in chunked_sentences:
    entity_names.extend(extract_entity_names(tree))


#Check if the output is null  
if not entity_names:
    print("List is empty")
    entity_names.append("NullNER")

# Print unique entity names
print (set(entity_names))

input("Press any key to exit...")

0 个答案:

没有答案