在groovy脚本中,我可以将数组中的依赖项列表分配为“ implementation library.frameworkLibs”,kotlin中是否存在类似的方法?
目标是在数组中有一个依赖项列表,以便每个列表都可以在子项目中单独分配。
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = '1.8'
/* 1. centerilize dependencies for all projects */
ext.libraries = [
frameworkLibs: [
'org.springframework.boot:spring-boot-starter-data-redis',
'org.springframework.boot:spring-boot-starter-web',
'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1',
'org.springframework.kafka:spring-kafka'
],
testLibs: [
'org.springframework.boot:spring-boot-starter-test',
'org.springframework.kafka:spring-kafka-test'
],
sqlLibs: [
'org.postgresql:postgresql'
]
]
allprojects {
group = 'com.example'
version = '0.0.1-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
/* 2. establish dependencies */
implementation libraries.frameworkLibs
runtimeOnly libraries.sqlLibs
testImplementation libraries.testLibs
}
}
答案 0 :(得分:2)
您可以将依赖关系图更改为:
from nltk.tokenize import word_tokenize
alltext = pd.concat([training_set.scrape_text, test_set.scrape_text])
alltext.shape
rows = []
for row in alltext.values:
rows.append(word_tokenize(row))
model = gensim.models.FastText(rows, size=100, window=4, min_count=2, iter=10)
#NLP pipeline
import re
from nltk.stem import SnowballStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from collections import defaultdict
class TfidfEmbeddingVectorizer(object):
def __preprocess(self, row):
#print("TfidfEmbeddingVectorizer __preprocess begin")
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, ' ', str(row))
cleaned = cleantext.replace("\n"," ").strip()
alpha_sent = ""
for word in cleaned.split():
alpha_word = re.sub('[^a-z A-Z]+', ' ', word)
alpha_sent += self.stemmer.stem(alpha_word)
#alpha_sent += lemmatizer.lemmatize(alpha_word)
alpha_sent += " "
alpha_sent = alpha_sent.strip()
return alpha_sent
def __tokenize(self, row):
return word_tokenize(row)
def __init__(self, model):
self.model = model
self.model2weight = None
self.dim = model.vector_size
self.stemmer = SnowballStemmer("english")
def fit(self, X, y):
print("TfidfEmbeddingVectorizer fit begin")
tfidf = TfidfVectorizer(analyzer=lambda x: x)
preprocessed_X = []
for row in X:
preprocessed_X.append(self.__tokenize(self.__preprocess(row)))
preprocessed_X = np.array(preprocessed_X)
tfidf.fit(preprocessed_X)
max_idf = max(tfidf.idf_)
self.model2weight = defaultdict(
lambda: max_idf,
[(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()])
print("TfidfEmbeddingVectorizer fit end")
return self
def transform(self, X):
preprocessed_X = []
for row in X:
preprocessed_X.append(self.__tokenize(self.__preprocess(row)))
preprocessed_X = np.array(preprocessed_X)
return np.array([
np.mean([self.model[w] * self.model2weight[w]
for w in words if w in self.model], axis=0)
for words in preprocessed_X
])
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.pipeline import Pipeline, FeatureUnion
from xgboost import XGBClassifier
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier, GradientBoostingClassifier, VotingClassifier
pipeline = Pipeline([
('feats', FeatureUnion([
('vec', TfidfVectorizer(preprocessor=preprocessor,
tokenizer=word_tokenize,
analyzer='word',
lowercase=True,
strip_accents='unicode',
stop_words='english',
ngram_range=(1,2)
))
])),
('voting', VotingClassifier(estimators=[("rf1", RandomForestClassifier(n_jobs=1)),
("gb", GradientBoostingClassifier()),
("et", ExtraTreesClassifier(n_jobs=1)),
("xgb",XGBClassifier(nthread= 4, learning_rate=0.08, n_estimators=1000, max_depth=10))
],
voting='soft',
])
然后在您的依赖项块中,它必须是:
val libraries by extra {
mapOf(
"frameworkLibs" to listOf(
"org.springframework.boot:spring-boot-starter-data-redis",
"org.springframework.boot:spring-boot-starter-web",
"org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1",
"org.springframework.kafka:spring-kafka"
)
)
}
您可以将扩展方法作为buildSrc脚本的一部分添加到Project中,以隐藏此 libraries["frameworkLibs"]?.forEach { s -> implementation(s) }
以使依赖项正常工作...
答案 1 :(得分:1)
您可以创建一个变量来存储依赖项,然后在allprojects
或subprojects
块中可以引用该数组。例如,您可以定义一个类来存储不同类型的依赖关系(例如,每个依赖关系为字符串数组),创建该类的实例并填充它,将对它的引用存储在变量中,然后使用该变量的值来分配依赖性,如下所示:
class Libraries(val frameworkLibs: Array<String>, val testLibs: Array<String>, val sqlLibs: Array<String>)
val libraries = Libraries(
arrayOf(
"org.springframework.boot:spring-boot-starter-data-redis",
"org.springframework.boot:spring-boot-starter-web",
"org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1",
"org.springframework.kafka:spring-kafka"
),
arrayOf(
"org.springframework.boot:spring-boot-starter-test",
"org.springframework.kafka:spring-kafka-test"
),
arrayOf("org.postgresql:postgresql")
)
allprojects {
dependencies {
libraries.frameworkLibs.forEach { implementation(it) }
libraries.sqlLibs.forEach { runtimeOnly(it) }
libraries.testLibs.forEach { testImplementation(it) }
我似乎找不到找到将依赖项数组传递到诸如implementation
或runtimeOnly
之类的方法中的方法,所以我将它们循环并分别添加每个依赖项。如果有一种方法可以在一行中传递数组,希望其他人可以告诉我们如何做。
答案 2 :(得分:0)
感谢tim_yates和Yoni Gibbs。实施之后解决了我的问题。
plugins {
id("org.springframework.boot") version "2.1.4.RELEASE"
java
// this plugin is required for Kotlin
id("io.spring.dependency-management") version "1.0.7.RELEASE"
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
repositories {
jcenter()
}
val libraries by extra {
mapOf(
"frameworkLibs" to listOf(
"org.springframework.boot:spring-boot-starter-data-redis",
"org.springframework.boot:spring-boot-starter-web",
"org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1",
"org.springframework.kafka:spring-kafka"
),
"testLibs" to listOf(
"org.springframework.boot:spring-boot-starter-test",
"org.springframework.kafka:spring-kafka-test"
),
"sqlLibs" to listOf(
"org.postgresql:postgresql"
),
"testLibs" to listOf(
"org.springframework.boot:spring-boot-starter-test",
"org.springframework.kafka:spring-kafka-test"
)
)
}
allprojects {
group = "com.example"
version = "0.0.1-SNAPSHOT"
dependencies {
libraries["frameworkLibs"]?.forEach { s -> implementation(s) }
libraries["testLibs"]?.forEach { s -> implementation(s) }
libraries["sqlLibs"]?.forEach { s -> runtimeOnly(s) }
libraries["testLibs"]?.forEach { s -> testImplementation(s) }
}
}